mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Yonggang Luo <luoyonggang@gmail.com>
To: Jens Gustedt <jens.gustedt@inria.fr>, musl@lists.openwall.com
Cc: Yonggang Luo <luoyonggang@gmail.com>
Subject: [musl] [PATCH 4/4] c2y: Add monotonic timed wait support for threads mtx cnd
Date: Tue, 20 Jun 2023 08:25:07 +0800	[thread overview]
Message-ID: <20230620002507.796-5-luoyonggang@gmail.com> (raw)
In-Reply-To: <20230620002507.796-1-luoyonggang@gmail.com>

Add two monotonic functions:
mtx_timedlock_monotonic
cnd_timedwait_monotonic

to achieve that

Signed-off-by: Yonggang Luo <luoyonggang@gmail.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


  parent reply	other threads:[~2023-06-20  0:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-20  0:25 [musl] [PATCH 0/4] Add posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait c2y/mtx_timedlock_monotonic c2y/cnd_timedwait_monotonic Yonggang Luo
2023-06-20  0:25 ` [musl] [PATCH 1/4] trim spaces of pthread_cond_timedwait.c and pthread_mutex_timedlock.c Yonggang Luo
2023-06-20  0:25 ` [musl] [PATCH 2/4] add pthread_mutex_clocklock and pthread_cond_clockdwait Yonggang Luo
2023-06-20  0:25 ` [musl] [PATCH 3/4] c23: Implement newly base for timespec_get Yonggang Luo
2023-06-20  7:06   ` [musl] " Jₑₙₛ Gustedt
2023-06-20  0:25 ` Yonggang Luo [this message]
2023-06-20  7:53   ` [musl] Re: [PATCH 4/4] c2y: Add monotonic timed wait support for threads mtx cnd 罗勇刚(Yonggang Luo)
2023-06-20  8:51     ` Jₑₙₛ Gustedt
2023-06-20 14:18 ` [musl] [PATCH 0/4] Add posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait c2y/mtx_timedlock_monotonic c2y/cnd_timedwait_monotonic enh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230620002507.796-5-luoyonggang@gmail.com \
    --to=luoyonggang@gmail.com \
    --cc=jens.gustedt@inria.fr \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).