|
|
Message-ID:
<LV8PR06MB9789369AF8B37951A575B60FF9362@LV8PR06MB9789.namprd06.prod.outlook.com>
Date: Mon, 27 Apr 2026 20:05:20 +0000
From: "Fradella, Ben" <Ben.Fradella@...app.com>
To: "musl@...ts.openwall.com" <musl@...ts.openwall.com>
Subject: Re: [PATCH] add sem_clockwait
... though, on a second look, I did make a dumb mistake in sem_timedwait:
---
- return sem_clockwait(sem, CLOCK_REALTIME, 0);
+ return sem_clockwait(sem, CLOCK_REALTIME, at);
---
________________________________________
From: Fradella, Ben <Ben.Fradella@...app.com>
Sent: Monday, April 27, 2026 2:51 PM
To: musl@...ts.openwall.com <musl@...ts.openwall.com>
Subject: [PATCH] add sem_clockwait
Confirmed that it builds and the symbol is in the lib. Haven't tested functionality yet, but it seems dead simple.
---
include/semaphore.h | 2 ++
src/thread/sem_clockwait.c | 33 +++++++++++++++++++++++++++++++++
src/thread/sem_timedwait.c | 30 ++----------------------------
3 files changed, 37 insertions(+), 28 deletions(-)
create mode 100644 src/thread/sem_clockwait.c
diff --git a/include/semaphore.h b/include/semaphore.h
index 3690f496..d69224ca 100644
--- a/include/semaphore.h
+++ b/include/semaphore.h
@@ -8,6 +8,7 @@ extern "C" {
#define __NEED_time_t
#define __NEED_struct_timespec
+#define __NEED_clockid_t
#include <bits/alltypes.h>
#include <fcntl.h>
@@ -25,6 +26,7 @@ int sem_init(sem_t *, int, unsigned);
sem_t *sem_open(const char *, int, ...);
int sem_post(sem_t *);
int sem_timedwait(sem_t *__restrict, const struct timespec *__restrict);
+int sem_clockwait(sem_t *__restrict, clockid_t clock, const struct timespec *__restrict);
int sem_trywait(sem_t *);
int sem_unlink(const char *);
int sem_wait(sem_t *);
diff --git a/src/thread/sem_clockwait.c b/src/thread/sem_clockwait.c
new file mode 100644
index 00000000..62260c62
--- /dev/null
+++ b/src/thread/sem_clockwait.c
@@ -0,0 +1,33 @@
+#include <semaphore.h>
+#include <limits.h>
+#include "pthread_impl.h"
+
+static void cleanup(void *p)
+{
+ a_dec(p);
+}
+
+int sem_clockwait(sem_t *restrict sem, clockid_t clock, const struct timespec *restrict at)
+{
+ pthread_testcancel();
+
+ if (!sem_trywait(sem)) return 0;
+
+ int spins = 100;
+ while (spins-- && !(sem->__val[0] & SEM_VALUE_MAX) && !sem->__val[1])
+ a_spin();
+
+ while (sem_trywait(sem)) {
+ int r, priv = sem->__val[2];
+ a_inc(sem->__val+1);
+ a_cas(sem->__val, 0, 0x80000000);
+ pthread_cleanup_push(cleanup, (void *)(sem->__val+1));
+ r = __timedwait_cp(sem->__val, 0x80000000, clock, at, priv);
+ pthread_cleanup_pop(1);
+ if (r) {
+ errno = r;
+ return -1;
+ }
+ }
+ return 0;
+}
diff --git a/src/thread/sem_timedwait.c b/src/thread/sem_timedwait.c
index aa67376c..9790ab72 100644
--- a/src/thread/sem_timedwait.c
+++ b/src/thread/sem_timedwait.c
@@ -1,33 +1,7 @@
#include <semaphore.h>
-#include <limits.h>
-#include "pthread_impl.h"
-
-static void cleanup(void *p)
-{
- a_dec(p);
-}
+#include <time.h>
int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at)
{
- pthread_testcancel();
-
- if (!sem_trywait(sem)) return 0;
-
- int spins = 100;
- while (spins-- && !(sem->__val[0] & SEM_VALUE_MAX) && !sem->__val[1])
- a_spin();
-
- while (sem_trywait(sem)) {
- int r, priv = sem->__val[2];
- a_inc(sem->__val+1);
- a_cas(sem->__val, 0, 0x80000000);
- pthread_cleanup_push(cleanup, (void *)(sem->__val+1));
- r = __timedwait_cp(sem->__val, 0x80000000, CLOCK_REALTIME, at, priv);
- pthread_cleanup_pop(1);
- if (r) {
- errno = r;
- return -1;
- }
- }
- return 0;
+ return sem_clockwait(sem, CLOCK_REALTIME, 0);
}
--
2.51.0
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.