mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Yonggang Luo <luoyonggang@gmail.com>
To: Jens Gustedt <jens.gustedt@inria.fr>, enh <enh@google.com>,
	musl@lists.openwall.com
Cc: Yonggang Luo <luoyonggang@gmail.com>
Subject: [musl] [PATCH v3 5/5] c2y: Add monotonic timedlock/timedwait support for threads mtx/cnd
Date: Tue, 20 Jun 2023 22:37:03 +0800	[thread overview]
Message-ID: <20230620143703.1415-6-luoyonggang@gmail.com> (raw)
In-Reply-To: <20230620143703.1415-1-luoyonggang@gmail.com>

Add two functions:
mtx_timedlock_base
cnd_timedwait_base

That support for both TIME_UTC and TIME_MONOTONIC

to achieve that

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 compat/time32/cnd_timedwait_base_time32.c |  9 ++++++++
 compat/time32/mtx_timedlock_base_time32.c |  9 ++++++++
 compat/time32/time32.h                    |  2 ++
 include/threads.h                         |  4 ++++
 src/thread/cnd_timedwait.c                |  8 +------
 src/thread/cnd_timedwait_base.c           | 26 +++++++++++++++++++++++
 src/thread/mtx_timedlock.c                |  7 +-----
 src/thread/mtx_timedlock_base.c           | 25 ++++++++++++++++++++++
 8 files changed, 77 insertions(+), 13 deletions(-)
 create mode 100644 compat/time32/cnd_timedwait_base_time32.c
 create mode 100644 compat/time32/mtx_timedlock_base_time32.c
 create mode 100644 src/thread/cnd_timedwait_base.c
 create mode 100644 src/thread/mtx_timedlock_base.c

diff --git a/compat/time32/cnd_timedwait_base_time32.c b/compat/time32/cnd_timedwait_base_time32.c
new file mode 100644
index 00000000..433f9841
--- /dev/null
+++ b/compat/time32/cnd_timedwait_base_time32.c
@@ -0,0 +1,9 @@
+#include "time32.h"
+#include <time.h>
+#include <threads.h>
+
+int __cnd_timedwait_base_time32(cnd_t *restrict c, mtx_t *restrict m, int time_base, const struct timespec32 *restrict ts32)
+{
+	return cnd_timedwait_base(c, m, time_base, ts32 ? (&(struct timespec){
+		.tv_sec = ts32->tv_sec, .tv_nsec = ts32->tv_nsec}) : 0);
+}
diff --git a/compat/time32/mtx_timedlock_base_time32.c b/compat/time32/mtx_timedlock_base_time32.c
new file mode 100644
index 00000000..8c3f27a1
--- /dev/null
+++ b/compat/time32/mtx_timedlock_base_time32.c
@@ -0,0 +1,9 @@
+#include "time32.h"
+#include <time.h>
+#include <threads.h>
+
+int __mtx_timedlock_base_time32(mtx_t *restrict m, int time_base, const struct timespec32 *restrict ts32)
+{
+	return mtx_timedlock_base(m, time_base, !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 1267f255..45ee6205 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_base_time32() __asm__("cnd_timedwait_base");
 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_base_time32() __asm__("mtx_timedlock_base");
 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..2c5f24f8 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_base(mtx_t *__restrict, int, 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_base(cnd_t *__restrict, mtx_t *__restrict, int, 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_base, __mtx_timedlock_base_time64);
 __REDIR(cnd_timedwait, __cnd_timedwait_time64);
+__REDIR(cnd_timedwait_base, __cnd_timedwait_base_time64);
 #endif
 
 #ifdef __cplusplus
diff --git a/src/thread/cnd_timedwait.c b/src/thread/cnd_timedwait.c
index 2802af52..60839fa0 100644
--- a/src/thread/cnd_timedwait.c
+++ b/src/thread/cnd_timedwait.c
@@ -4,11 +4,5 @@
 
 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;
-	}
+	return cnd_timedwait_base(c, m, TIME_UTC, ts);
 }
diff --git a/src/thread/cnd_timedwait_base.c b/src/thread/cnd_timedwait_base.c
new file mode 100644
index 00000000..b0b37cac
--- /dev/null
+++ b/src/thread/cnd_timedwait_base.c
@@ -0,0 +1,26 @@
+#include <threads.h>
+#include <pthread.h>
+#include <errno.h>
+
+int cnd_timedwait_base(cnd_t *restrict c, mtx_t *restrict m, int time_base, const struct timespec *restrict ts)
+{
+	clockid_t clock = CLOCK_REALTIME;
+	switch (time_base)
+	{
+	case TIME_UTC:
+		clock = CLOCK_REALTIME;
+		break;
+	case TIME_MONOTONIC:
+		clock = CLOCK_MONOTONIC;
+		break;
+	default:
+		return thrd_error;
+	}
+	int ret = pthread_cond_clockwait((pthread_cond_t *)c, (pthread_mutex_t *)m, clock, 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..49a28623 100644
--- a/src/thread/mtx_timedlock.c
+++ b/src/thread/mtx_timedlock.c
@@ -4,10 +4,5 @@
 
 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;
-	}
+	return mtx_timedlock_base(m, TIME_UTC, ts);
 }
diff --git a/src/thread/mtx_timedlock_base.c b/src/thread/mtx_timedlock_base.c
new file mode 100644
index 00000000..3dda19ab
--- /dev/null
+++ b/src/thread/mtx_timedlock_base.c
@@ -0,0 +1,25 @@
+#include <threads.h>
+#include <pthread.h>
+#include <errno.h>
+
+int mtx_timedlock_base(mtx_t *restrict m, int time_base, const struct timespec *restrict ts)
+{
+	clockid_t clock = CLOCK_REALTIME;
+	switch (time_base)
+	{
+	case TIME_UTC:
+		clock = CLOCK_REALTIME;
+		break;
+	case TIME_MONOTONIC:
+		clock = CLOCK_MONOTONIC;
+		break;
+	default:
+		return thrd_error;
+	}
+	int ret = pthread_mutex_clocklock((pthread_mutex_t *)m, clock, 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 14:38 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-20 14:36 [musl] [PATCH v3 0/5] Add posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait c2y/mtx_timedlock_base c2y/cnd_timedwait_base Yonggang Luo
2023-06-20 14:36 ` [musl] [PATCH v3 1/5] trim spaces of pthread_cond_timedwait.c and pthread_mutex_timedlock.c Yonggang Luo
2023-06-20 14:37 ` [musl] [PATCH v3 2/5] Rename files for implement pthread_mutex_clocklock and pthread_cond_clockwait Yonggang Luo
2023-06-20 14:37 ` [musl] [PATCH v3 3/5] add pthread_mutex_clocklock and pthread_cond_clockdwait Yonggang Luo
2023-06-21 15:04   ` Rich Felker
2023-06-20 14:37 ` [musl] [PATCH v3 4/5] c23: Implement newly base for timespec_get Yonggang Luo
2023-06-20 14:37 ` Yonggang Luo [this message]
2023-06-20 22:47 ` [musl] [PATCH v3 0/5] Add posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait c2y/mtx_timedlock_base c2y/cnd_timedwait_base Rich Felker
2023-06-21  6:10   ` 罗勇刚(Yonggang Luo)
2023-06-21 14:54     ` Rich Felker
2023-06-21 18:43       ` 罗勇刚(Yonggang Luo)
2023-06-21 18:58         ` enh
2023-06-21  6:25   ` 罗勇刚(Yonggang Luo)
2023-06-21 14:44     ` Rich Felker

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=20230620143703.1415-6-luoyonggang@gmail.com \
    --to=luoyonggang@gmail.com \
    --cc=enh@google.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).