Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230620002507.796-5-luoyonggang@gmail.com>
Date: Tue, 20 Jun 2023 08:25:07 +0800
From: Yonggang Luo <luoyonggang@...il.com>
To: Jens Gustedt <jens.gustedt@...ia.fr>,
	musl@...ts.openwall.com
Cc: Yonggang Luo <luoyonggang@...il.com>
Subject: [PATCH 4/4] c2y: Add monotonic timed wait support for threads mtx cnd

Add two monotonic functions:
mtx_timedlock_monotonic
cnd_timedwait_monotonic

to achieve that

Signed-off-by: Yonggang Luo <luoyonggang@...il.com>
---
 .../time32/cnd_timedwait_monotonic_time32.c   |  9 ++++++
 .../time32/mtx_timedlock_monotonic_time32.c   |  9 ++++++
 compat/time32/time32.h                        |  2 ++
 include/threads.h                             |  4 +++
 src/thread/cnd_timedwait.c                    |  2 +-
 ..._timedwait.c => cnd_timedwait_monotonic.c} | 28 +++++++++----------
 src/thread/mtx_timedlock.c                    |  2 +-
 ..._timedlock.c => mtx_timedlock_monotonic.c} | 26 ++++++++---------
 8 files changed, 53 insertions(+), 29 deletions(-)
 create mode 100644 compat/time32/cnd_timedwait_monotonic_time32.c
 create mode 100644 compat/time32/mtx_timedlock_monotonic_time32.c
 copy src/thread/{cnd_timedwait.c => cnd_timedwait_monotonic.c} (52%)
 copy src/thread/{mtx_timedlock.c => mtx_timedlock_monotonic.c} (74%)

diff --git a/compat/time32/cnd_timedwait_monotonic_time32.c b/compat/time32/cnd_timedwait_monotonic_time32.c
new file mode 100644
index 00000000..a61cc944
--- /dev/null
+++ b/compat/time32/cnd_timedwait_monotonic_time32.c
@@ -0,0 +1,9 @@
+#include "time32.h"
+#include <time.h>
+#include <threads.h>
+
+int __cnd_timedwait_monotonic_time32(cnd_t *restrict c, mtx_t *restrict m, const struct timespec32 *restrict ts32)
+{
+	return cnd_timedwait_monotonic(c, m, ts32 ? (&(struct timespec){
+		.tv_sec = ts32->tv_sec, .tv_nsec = ts32->tv_nsec}) : 0);
+}
diff --git a/compat/time32/mtx_timedlock_monotonic_time32.c b/compat/time32/mtx_timedlock_monotonic_time32.c
new file mode 100644
index 00000000..fb610ab8
--- /dev/null
+++ b/compat/time32/mtx_timedlock_monotonic_time32.c
@@ -0,0 +1,9 @@
+#include "time32.h"
+#include <time.h>
+#include <threads.h>
+
+int __mtx_timedlock_monotonic_time32(mtx_t *restrict m, const struct timespec32 *restrict ts32)
+{
+	return mtx_timedlock_monotonic(m, !ts32 ? 0 : (&(struct timespec){
+		.tv_sec = ts32->tv_sec, .tv_nsec = ts32->tv_nsec}));
+}
diff --git a/compat/time32/time32.h b/compat/time32/time32.h
index e0af9bff..c98c4a64 100644
--- a/compat/time32/time32.h
+++ b/compat/time32/time32.h
@@ -34,6 +34,7 @@ int __clock_gettime32() __asm__("clock_gettime");
 int __clock_nanosleep_time32() __asm__("clock_nanosleep");
 int __clock_settime32() __asm__("clock_settime");
 int __cnd_timedwait_time32() __asm__("cnd_timedwait");
+int __cnd_timedwait_monotonic_time32() __asm__("cnd_timedwait_monotonic");
 char *__ctime32() __asm__("ctime");
 char *__ctime32_r() __asm__("ctime_r");
 double __difftime32() __asm__("difftime");
@@ -56,6 +57,7 @@ time32_t __mktime32() __asm__("mktime");
 ssize_t __mq_timedreceive_time32() __asm__("mq_timedreceive");
 int __mq_timedsend_time32() __asm__("mq_timedsend");
 int __mtx_timedlock_time32() __asm__("mtx_timedlock");
+int __mtx_timedlock_monotonic_time32() __asm__("mtx_timedlock_monotonic");
 int __nanosleep_time32() __asm__("nanosleep");
 int __ppoll_time32() __asm__("ppoll");
 int __pselect_time32() __asm__("pselect");
diff --git a/include/threads.h b/include/threads.h
index 52ec3100..9439d530 100644
--- a/include/threads.h
+++ b/include/threads.h
@@ -62,6 +62,7 @@ void mtx_destroy(mtx_t *);
 
 int mtx_lock(mtx_t *);
 int mtx_timedlock(mtx_t *__restrict, const struct timespec *__restrict);
+int mtx_timedlock_monotonic(mtx_t *__restrict, const struct timespec *__restrict);
 int mtx_trylock(mtx_t *);
 int mtx_unlock(mtx_t *);
 
@@ -72,6 +73,7 @@ int cnd_broadcast(cnd_t *);
 int cnd_signal(cnd_t *);
 
 int cnd_timedwait(cnd_t *__restrict, mtx_t *__restrict, const struct timespec *__restrict);
+int cnd_timedwait_monotonic(cnd_t *__restrict, mtx_t *__restrict, const struct timespec *__restrict);
 int cnd_wait(cnd_t *, mtx_t *);
 
 int tss_create(tss_t *, tss_dtor_t);
@@ -83,7 +85,9 @@ void *tss_get(tss_t);
 #if _REDIR_TIME64
 __REDIR(thrd_sleep, __thrd_sleep_time64);
 __REDIR(mtx_timedlock, __mtx_timedlock_time64);
+__REDIR(mtx_timedlock_monotonic, __mtx_timedlock_monotonic_time64);
 __REDIR(cnd_timedwait, __cnd_timedwait_time64);
+__REDIR(cnd_timedwait_monotonic, __cnd_timedwait_monotonic_time64);
 #endif
 
 #ifdef __cplusplus
diff --git a/src/thread/cnd_timedwait.c b/src/thread/cnd_timedwait.c
index 2802af52..8c951e0f 100644
--- a/src/thread/cnd_timedwait.c
+++ b/src/thread/cnd_timedwait.c
@@ -4,7 +4,7 @@
 
 int cnd_timedwait(cnd_t *restrict c, mtx_t *restrict m, const struct timespec *restrict ts)
 {
-	int ret = __pthread_cond_timedwait((pthread_cond_t *)c, (pthread_mutex_t *)m, ts);
+	int ret = pthread_cond_clockdwait((pthread_cond_t *)c, (pthread_mutex_t *)m, CLOCK_REALTIME, ts);
 	switch (ret) {
 	/* May also return EINVAL or EPERM. */
 	default:        return thrd_error;
diff --git a/src/thread/cnd_timedwait.c b/src/thread/cnd_timedwait_monotonic.c
similarity index 52%
copy from src/thread/cnd_timedwait.c
copy to src/thread/cnd_timedwait_monotonic.c
index 2802af52..a7fe2fd6 100644
--- a/src/thread/cnd_timedwait.c
+++ b/src/thread/cnd_timedwait_monotonic.c
@@ -1,14 +1,14 @@
-#include <threads.h>
-#include <pthread.h>
-#include <errno.h>
-
-int cnd_timedwait(cnd_t *restrict c, mtx_t *restrict m, const struct timespec *restrict ts)
-{
-	int ret = __pthread_cond_timedwait((pthread_cond_t *)c, (pthread_mutex_t *)m, ts);
-	switch (ret) {
-	/* May also return EINVAL or EPERM. */
-	default:        return thrd_error;
-	case 0:         return thrd_success;
-	case ETIMEDOUT: return thrd_timedout;
-	}
-}
+#include <threads.h>
+#include <pthread.h>
+#include <errno.h>
+
+int cnd_timedwait_monotonic(cnd_t *restrict c, mtx_t *restrict m, const struct timespec *restrict ts)
+{
+	int ret = pthread_cond_clockdwait((pthread_cond_t *)c, (pthread_mutex_t *)m, CLOCK_MONOTONIC, ts);
+	switch (ret) {
+	/* May also return EINVAL or EPERM. */
+	default:        return thrd_error;
+	case 0:         return thrd_success;
+	case ETIMEDOUT: return thrd_timedout;
+	}
+}
diff --git a/src/thread/mtx_timedlock.c b/src/thread/mtx_timedlock.c
index d22c8cf4..30dba40e 100644
--- a/src/thread/mtx_timedlock.c
+++ b/src/thread/mtx_timedlock.c
@@ -4,7 +4,7 @@
 
 int mtx_timedlock(mtx_t *restrict m, const struct timespec *restrict ts)
 {
-	int ret = __pthread_mutex_timedlock((pthread_mutex_t *)m, ts);
+	int ret = pthread_mutex_clocklock((pthread_mutex_t *)m, CLOCK_REALTIME, ts);
 	switch (ret) {
 	default:        return thrd_error;
 	case 0:         return thrd_success;
diff --git a/src/thread/mtx_timedlock.c b/src/thread/mtx_timedlock_monotonic.c
similarity index 74%
copy from src/thread/mtx_timedlock.c
copy to src/thread/mtx_timedlock_monotonic.c
index d22c8cf4..a788e877 100644
--- a/src/thread/mtx_timedlock.c
+++ b/src/thread/mtx_timedlock_monotonic.c
@@ -1,13 +1,13 @@
-#include <threads.h>
-#include <pthread.h>
-#include <errno.h>
-
-int mtx_timedlock(mtx_t *restrict m, const struct timespec *restrict ts)
-{
-	int ret = __pthread_mutex_timedlock((pthread_mutex_t *)m, ts);
-	switch (ret) {
-	default:        return thrd_error;
-	case 0:         return thrd_success;
-	case ETIMEDOUT: return thrd_timedout;
-	}
-}
+#include <threads.h>
+#include <pthread.h>
+#include <errno.h>
+
+int mtx_timedlock(mtx_t *restrict m, const struct timespec *restrict ts)
+{
+	int ret = pthread_mutex_clocklock((pthread_mutex_t *)m, CLOCK_MONOTONIC, ts);
+	switch (ret) {
+	default:        return thrd_error;
+	case 0:         return thrd_success;
+	case ETIMEDOUT: return thrd_timedout;
+	}
+}
-- 
2.39.0.windows.1

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.