mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH 0/4] Add posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait c2y/mtx_timedlock_monotonic c2y/cnd_timedwait_monotonic
@ 2023-06-20  0:25 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
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Yonggang Luo @ 2023-06-20  0:25 UTC (permalink / raw)
  To: Jens Gustedt, musl; +Cc: Yonggang Luo

Currently, musl doesn't have pthread_mutex_clocklock pthread_cond_clockdwait, but
glibc, android bionic, qnx libc already have these two functions, so implement them in
musl.

And for c11 threads, the mtx and cnd doesn't support for monotonic timedlock and timedwait,
so add a proposaled function mtx_timedlock_monotonic cnd_timedwait_monotonic to do that.

And indeed mtx_timedlock_monotonic and cnd_timedwait_monotonic can be implemented ontop of
posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait, so I implemented posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait
first in musl.

I think mtx_timedlock_monotonic cnd_timedwait_monotonic is reasonable because it's newly added
function and won't affect existing c11 threads functions.
And it's can be implementd with glibc/qnx libc/android bionic without burden.
For OS X it's can be implemented with pthread_cond_timedwait_relative_np
For Windows it's can be implemented with SleepConditionVariableCS
For platform have none of these, it's still can fallback to TIME_UTC

When these

Yonggang Luo (4):
  trim spaces of pthread_cond_timedwait.c and pthread_mutex_timedlock.c
  add pthread_mutex_clocklock and pthread_cond_clockdwait
  c23: Implement newly base for timespec_get
  c2y: Add monotonic timed wait support for threads mtx cnd

 .../time32/cnd_timedwait_monotonic_time32.c   |  9 ++++
 .../time32/mtx_timedlock_monotonic_time32.c   |  9 ++++
 compat/time32/pthread_cond_clockwait_time32.c |  9 ++++
 .../time32/pthread_mutex_clocklock_time32.c   |  9 ++++
 compat/time32/time32.h                        |  4 ++
 include/pthread.h                             |  4 ++
 include/threads.h                             |  4 ++
 include/time.h                                | 13 +++++-
 src/include/pthread.h                         |  2 +
 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 +++++------
 src/thread/pthread_cond_clockwait.c           |  3 ++
 src/thread/pthread_cond_timedwait.c           | 18 +++++---
 src/thread/pthread_mutex_clocklock.c          |  3 ++
 src/thread/pthread_mutex_timedlock.c          | 15 ++++---
 src/time/timespec_get.c                       | 44 ++++++++++++++++++-
 18 files changed, 161 insertions(+), 43 deletions(-)
 create mode 100644 compat/time32/cnd_timedwait_monotonic_time32.c
 create mode 100644 compat/time32/mtx_timedlock_monotonic_time32.c
 create mode 100644 compat/time32/pthread_cond_clockwait_time32.c
 create mode 100644 compat/time32/pthread_mutex_clocklock_time32.c
 copy src/thread/{cnd_timedwait.c => cnd_timedwait_monotonic.c} (52%)
 copy src/thread/{mtx_timedlock.c => mtx_timedlock_monotonic.c} (74%)
 create mode 100644 src/thread/pthread_cond_clockwait.c
 create mode 100644 src/thread/pthread_mutex_clocklock.c

-- 
2.39.0.windows.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [musl] [PATCH 1/4] trim spaces of pthread_cond_timedwait.c and pthread_mutex_timedlock.c
  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 ` Yonggang Luo
  2023-06-20  0:25 ` [musl] [PATCH 2/4] add pthread_mutex_clocklock and pthread_cond_clockdwait Yonggang Luo
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Yonggang Luo @ 2023-06-20  0:25 UTC (permalink / raw)
  To: Jens Gustedt, musl; +Cc: Yonggang Luo

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 src/thread/pthread_cond_timedwait.c  | 6 +++---
 src/thread/pthread_mutex_timedlock.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/thread/pthread_cond_timedwait.c b/src/thread/pthread_cond_timedwait.c
index 6b761455..c5b35a6c 100644
--- a/src/thread/pthread_cond_timedwait.c
+++ b/src/thread/pthread_cond_timedwait.c
@@ -121,12 +121,12 @@ int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restri
 		 * via the futex notify below. */
 
 		lock(&c->_c_lock);
-		
+
 		if (c->_c_head == &node) c->_c_head = node.next;
 		else if (node.prev) node.prev->next = node.next;
 		if (c->_c_tail == &node) c->_c_tail = node.prev;
 		else if (node.next) node.next->prev = node.prev;
-		
+
 		unlock(&c->_c_lock);
 
 		if (node.notify) {
@@ -156,7 +156,7 @@ relock:
 		if (val>0) a_cas(&m->_m_lock, val, val|0x80000000);
 		unlock_requeue(&node.prev->barrier, &m->_m_lock, m->_m_type & (8|128));
 	} else if (!(m->_m_type & 8)) {
-		a_dec(&m->_m_waiters);		
+		a_dec(&m->_m_waiters);
 	}
 
 	/* Since a signal was consumed, cancellation is not permitted. */
diff --git a/src/thread/pthread_mutex_timedlock.c b/src/thread/pthread_mutex_timedlock.c
index 9279fc54..87f89287 100644
--- a/src/thread/pthread_mutex_timedlock.c
+++ b/src/thread/pthread_mutex_timedlock.c
@@ -66,7 +66,7 @@ int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec
 	if (r != EBUSY) return r;
 
 	if (type&8) return pthread_mutex_timedlock_pi(m, at);
-	
+
 	int spins = 100;
 	while (spins-- && m->_m_lock && !m->_m_waiters) a_spin();
 
-- 
2.39.0.windows.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [musl] [PATCH 2/4] add pthread_mutex_clocklock and pthread_cond_clockdwait
  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 ` Yonggang Luo
  2023-06-20  0:25 ` [musl] [PATCH 3/4] c23: Implement newly base for timespec_get Yonggang Luo
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Yonggang Luo @ 2023-06-20  0:25 UTC (permalink / raw)
  To: Jens Gustedt, musl; +Cc: Yonggang Luo

These two functions are already implemented in glibc, android bionic libc, qnx libc

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 compat/time32/pthread_cond_clockwait_time32.c  |  9 +++++++++
 compat/time32/pthread_mutex_clocklock_time32.c |  9 +++++++++
 compat/time32/time32.h                         |  2 ++
 include/pthread.h                              |  4 ++++
 src/include/pthread.h                          |  2 ++
 src/thread/pthread_cond_clockwait.c            |  3 +++
 src/thread/pthread_cond_timedwait.c            | 12 +++++++++---
 src/thread/pthread_mutex_clocklock.c           |  3 +++
 src/thread/pthread_mutex_timedlock.c           | 13 +++++++++----
 9 files changed, 50 insertions(+), 7 deletions(-)
 create mode 100644 compat/time32/pthread_cond_clockwait_time32.c
 create mode 100644 compat/time32/pthread_mutex_clocklock_time32.c
 create mode 100644 src/thread/pthread_cond_clockwait.c
 create mode 100644 src/thread/pthread_mutex_clocklock.c

diff --git a/compat/time32/pthread_cond_clockwait_time32.c b/compat/time32/pthread_cond_clockwait_time32.c
new file mode 100644
index 00000000..1a18114f
--- /dev/null
+++ b/compat/time32/pthread_cond_clockwait_time32.c
@@ -0,0 +1,9 @@
+#include "time32.h"
+#include <time.h>
+#include <pthread.h>
+
+int __pthread_cond_clockwait_time32(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, clockid_t clock, const struct timespec32 *restrict ts32)
+{
+	return pthread_cond_clockdwait(c, m, clock, !ts32 ? 0 : (&(struct timespec){
+		.tv_sec = ts32->tv_sec, .tv_nsec = ts32->tv_nsec}));
+}
diff --git a/compat/time32/pthread_mutex_clocklock_time32.c b/compat/time32/pthread_mutex_clocklock_time32.c
new file mode 100644
index 00000000..d54e85a7
--- /dev/null
+++ b/compat/time32/pthread_mutex_clocklock_time32.c
@@ -0,0 +1,9 @@
+#include "time32.h"
+#include <time.h>
+#include <pthread.h>
+
+int __pthread_mutex_clocklock_time32(pthread_mutex_t *restrict m, clockid_t clk, const struct timespec32 *restrict ts32)
+{
+	return pthread_mutex_clocklock(m, clk, !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 fdec17c3..e0af9bff 100644
--- a/compat/time32/time32.h
+++ b/compat/time32/time32.h
@@ -59,7 +59,9 @@ int __mtx_timedlock_time32() __asm__("mtx_timedlock");
 int __nanosleep_time32() __asm__("nanosleep");
 int __ppoll_time32() __asm__("ppoll");
 int __pselect_time32() __asm__("pselect");
+int __pthread_cond_clockdwait_time32() __asm__("pthread_cond_clockdwait");
 int __pthread_cond_timedwait_time32() __asm__("pthread_cond_timedwait");
+int __pthread_mutex_clocklock_time32() __asm__("pthread_mutex_clocklock");
 int __pthread_mutex_timedlock_time32() __asm__("pthread_mutex_timedlock");
 int __pthread_rwlock_timedrdlock_time32() __asm__("pthread_rwlock_timedrdlock");
 int __pthread_rwlock_timedwrlock_time32() __asm__("pthread_rwlock_timedwrlock");
diff --git a/include/pthread.h b/include/pthread.h
index 89fd9ff7..4062ff9b 100644
--- a/include/pthread.h
+++ b/include/pthread.h
@@ -107,6 +107,7 @@ int pthread_mutex_init(pthread_mutex_t *__restrict, const pthread_mutexattr_t *_
 int pthread_mutex_lock(pthread_mutex_t *);
 int pthread_mutex_unlock(pthread_mutex_t *);
 int pthread_mutex_trylock(pthread_mutex_t *);
+int pthread_mutex_clocklock(pthread_mutex_t *__restrict, clockid_t, const struct timespec *__restrict);
 int pthread_mutex_timedlock(pthread_mutex_t *__restrict, const struct timespec *__restrict);
 int pthread_mutex_destroy(pthread_mutex_t *);
 int pthread_mutex_consistent(pthread_mutex_t *);
@@ -117,6 +118,7 @@ int pthread_mutex_setprioceiling(pthread_mutex_t *__restrict, int, int *__restri
 int pthread_cond_init(pthread_cond_t *__restrict, const pthread_condattr_t *__restrict);
 int pthread_cond_destroy(pthread_cond_t *);
 int pthread_cond_wait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict);
+int pthread_cond_clockdwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, clockid_t, const struct timespec *__restrict);
 int pthread_cond_timedwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, const struct timespec *__restrict);
 int pthread_cond_broadcast(pthread_cond_t *);
 int pthread_cond_signal(pthread_cond_t *);
@@ -229,7 +231,9 @@ int pthread_timedjoin_np(pthread_t, void **, const struct timespec *);
 #endif
 
 #if _REDIR_TIME64
+__REDIR(pthread_mutex_clocklock, __pthread_mutex_clocklock_time64);
 __REDIR(pthread_mutex_timedlock, __pthread_mutex_timedlock_time64);
+__REDIR(pthread_cond_clockdwait, __pthread_cond_clockdwait_time64);
 __REDIR(pthread_cond_timedwait, __pthread_cond_timedwait_time64);
 __REDIR(pthread_rwlock_timedrdlock, __pthread_rwlock_timedrdlock_time64);
 __REDIR(pthread_rwlock_timedwrlock, __pthread_rwlock_timedwrlock_time64);
diff --git a/src/include/pthread.h b/src/include/pthread.h
index 7167d3e1..ec533264 100644
--- a/src/include/pthread.h
+++ b/src/include/pthread.h
@@ -12,9 +12,11 @@ hidden int __pthread_join(pthread_t, void **);
 hidden int __pthread_mutex_lock(pthread_mutex_t *);
 hidden int __pthread_mutex_trylock(pthread_mutex_t *);
 hidden int __pthread_mutex_trylock_owner(pthread_mutex_t *);
+hidden int __pthread_mutex_clocklock(pthread_mutex_t *restrict, clockid_t, const struct timespec *restrict);
 hidden int __pthread_mutex_timedlock(pthread_mutex_t *restrict, const struct timespec *restrict);
 hidden int __pthread_mutex_unlock(pthread_mutex_t *);
 hidden int __private_cond_signal(pthread_cond_t *, int);
+hidden int __pthread_cond_clockdwait(pthread_cond_t *restrict, pthread_mutex_t *restrict, clockid_t, const struct timespec *restrict);
 hidden int __pthread_cond_timedwait(pthread_cond_t *restrict, pthread_mutex_t *restrict, const struct timespec *restrict);
 hidden int __pthread_key_create(pthread_key_t *, void (*)(void *));
 hidden int __pthread_key_delete(pthread_key_t);
diff --git a/src/thread/pthread_cond_clockwait.c b/src/thread/pthread_cond_clockwait.c
new file mode 100644
index 00000000..1273f06d
--- /dev/null
+++ b/src/thread/pthread_cond_clockwait.c
@@ -0,0 +1,3 @@
+#include "pthread_impl.h"
+
+weak_alias(__pthread_cond_clockdwait, pthread_cond_clockdwait);
diff --git a/src/thread/pthread_cond_timedwait.c b/src/thread/pthread_cond_timedwait.c
index c5b35a6c..61df6673 100644
--- a/src/thread/pthread_cond_timedwait.c
+++ b/src/thread/pthread_cond_timedwait.c
@@ -59,10 +59,10 @@ enum {
 	LEAVING,
 };
 
-int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
+int __pthread_cond_clockdwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, clockid_t clk, const struct timespec *restrict ts)
 {
 	struct waiter node = { 0 };
-	int e, seq, clock = c->_c_clock, cs, shared=0, oldstate, tmp;
+	int e, seq, cs, shared=0, oldstate, tmp;
 	volatile int *fut;
 
 	if ((m->_m_type&15) && (m->_m_lock&INT_MAX) != __pthread_self()->tid)
@@ -97,7 +97,7 @@ int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restri
 	__pthread_setcancelstate(PTHREAD_CANCEL_MASKED, &cs);
 	if (cs == PTHREAD_CANCEL_DISABLE) __pthread_setcancelstate(cs, 0);
 
-	do e = __timedwait_cp(fut, seq, clock, ts, !shared);
+	do e = __timedwait_cp(fut, seq, clk, ts, !shared);
 	while (*fut==seq && (!e || e==EINTR));
 	if (e == EINTR) e = 0;
 
@@ -210,4 +210,10 @@ int __private_cond_signal(pthread_cond_t *c, int n)
 	return 0;
 }
 
+int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
+{
+	int clock = c->_c_clock;
+	return __pthread_cond_clockdwait(c, m, clock, ts);
+}
+
 weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait);
diff --git a/src/thread/pthread_mutex_clocklock.c b/src/thread/pthread_mutex_clocklock.c
new file mode 100644
index 00000000..2b869f4f
--- /dev/null
+++ b/src/thread/pthread_mutex_clocklock.c
@@ -0,0 +1,3 @@
+#include "pthread_impl.h"
+
+weak_alias(__pthread_mutex_clocklock, pthread_mutex_clocklock);
diff --git a/src/thread/pthread_mutex_timedlock.c b/src/thread/pthread_mutex_timedlock.c
index 87f89287..f9c8fc56 100644
--- a/src/thread/pthread_mutex_timedlock.c
+++ b/src/thread/pthread_mutex_timedlock.c
@@ -18,7 +18,7 @@ static int __futex4(volatile void *addr, int op, int val, const struct timespec
 	return __syscall(SYS_futex, addr, op, val, to);
 }
 
-static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, const struct timespec *restrict at)
+static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, clock_id_t clk, const struct timespec *restrict at)
 {
 	int type = m->_m_type;
 	int priv = (type & 128) ^ 128;
@@ -48,12 +48,12 @@ static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, const struct
 	case EDEADLK:
 		if ((type&3) == PTHREAD_MUTEX_ERRORCHECK) return e;
 	}
-	do e = __timedwait(&(int){0}, 0, CLOCK_REALTIME, at, 1);
+	do e = __timedwait(&(int){0}, 0, clk, at, 1);
 	while (e != ETIMEDOUT);
 	return e;
 }
 
-int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
+int __pthread_mutex_clocklock(pthread_mutex_t *restrict m, clock_id_t clk, const struct timespec *restrict at)
 {
 	if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
 	    && !a_cas(&m->_m_lock, 0, EBUSY))
@@ -82,11 +82,16 @@ int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec
 		a_inc(&m->_m_waiters);
 		t = r | 0x80000000;
 		a_cas(&m->_m_lock, r, t);
-		r = __timedwait(&m->_m_lock, t, CLOCK_REALTIME, at, priv);
+		r = __timedwait(&m->_m_lock, t, clk, at, priv);
 		a_dec(&m->_m_waiters);
 		if (r && r != EINTR) break;
 	}
 	return r;
 }
 
+int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
+{
+	return __pthread_mutex_clocklock(m, CLOCK_REALTIME, at);
+}
+
 weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock);
-- 
2.39.0.windows.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [musl] [PATCH 3/4] c23: Implement newly base for timespec_get
  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 ` Yonggang Luo
  2023-06-20  7:06   ` [musl] " Jₑₙₛ Gustedt
  2023-06-20  0:25 ` [musl] [PATCH 4/4] c2y: Add monotonic timed wait support for threads mtx cnd Yonggang Luo
  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
  4 siblings, 1 reply; 9+ messages in thread
From: Yonggang Luo @ 2023-06-20  0:25 UTC (permalink / raw)
  To: Jens Gustedt, musl; +Cc: Yonggang Luo

These are:
#define TIME_MONOTONIC          2
#define TIME_PROCESS_CPUTIME_ID 3
#define TIME_THREAD_CPUTIME_ID  4
#define TIME_MONOTONIC_RAW      5
#define TIME_REALTIME_COARSE    6
#define TIME_MONOTONIC_COARSE   7
#define TIME_BOOTTIME           8
#define TIME_REALTIME_ALARM     9
#define TIME_BOOTTIME_ALARM     10
#define TIME_SGI_CYCLE          11
#define TIME_TAI                12

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 include/time.h          | 13 +++++++++++-
 src/time/timespec_get.c | 44 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/include/time.h b/include/time.h
index 3d948372..de6d9f38 100644
--- a/include/time.h
+++ b/include/time.h
@@ -64,7 +64,18 @@ int timespec_get(struct timespec *, int);
 
 #define CLOCKS_PER_SEC 1000000L
 
-#define TIME_UTC 1
+#define TIME_UTC                1
+#define TIME_MONOTONIC          2
+#define TIME_PROCESS_CPUTIME_ID 3
+#define TIME_THREAD_CPUTIME_ID  4
+#define TIME_MONOTONIC_RAW      5
+#define TIME_REALTIME_COARSE    6
+#define TIME_MONOTONIC_COARSE   7
+#define TIME_BOOTTIME           8
+#define TIME_REALTIME_ALARM     9
+#define TIME_BOOTTIME_ALARM     10
+#define TIME_SGI_CYCLE          11
+#define TIME_TAI                12
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
  || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
diff --git a/src/time/timespec_get.c b/src/time/timespec_get.c
index 40ea9c1c..bfc753ee 100644
--- a/src/time/timespec_get.c
+++ b/src/time/timespec_get.c
@@ -4,7 +4,47 @@
  * are considered erroneous. */
 int timespec_get(struct timespec * ts, int base)
 {
-	if (base != TIME_UTC) return 0;
-	int ret = __clock_gettime(CLOCK_REALTIME, ts);
+	clockid_t clockid = -1;
+	switch (base) {
+	default:
+		return 0;
+	case TIME_UTC:
+		clockid = CLOCK_REALTIME;
+		break;
+	case TIME_MONOTONIC:
+		clockid = CLOCK_MONOTONIC;
+		break;
+	case TIME_PROCESS_CPUTIME_ID:
+		clockid = CLOCK_PROCESS_CPUTIME_ID;
+		break;
+	case TIME_THREAD_CPUTIME_ID:
+		clockid = CLOCK_THREAD_CPUTIME_ID;
+		break;
+	case TIME_MONOTONIC_RAW:
+		clockid = CLOCK_MONOTONIC_RAW;
+		break;
+	case TIME_REALTIME_COARSE:
+		clockid = CLOCK_REALTIME_COARSE;
+		break;
+	case TIME_MONOTONIC_COARSE:
+		clockid = CLOCK_MONOTONIC_COARSE;
+		break;
+	case TIME_BOOTTIME:
+		clockid = CLOCK_BOOTTIME;
+		break;
+	case TIME_REALTIME_ALARM:
+		clockid = CLOCK_REALTIME_ALARM;
+		break;
+	case TIME_BOOTTIME_ALARM:
+		clockid = CLOCK_BOOTTIME_ALARM;
+		break;
+	case TIME_SGI_CYCLE:
+		clockid = CLOCK_SGI_CYCLE;
+		break;
+	case TIME_TAI:
+		clockid = CLOCK_TAI;
+		break;
+	}
+	int ret = __clock_gettime(clockid, ts);
 	return ret < 0 ? 0 : base;
 }
-- 
2.39.0.windows.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [musl] [PATCH 4/4] c2y: Add monotonic timed wait support for threads mtx cnd
  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
                   ` (2 preceding siblings ...)
  2023-06-20  0:25 ` [musl] [PATCH 3/4] c23: Implement newly base for timespec_get Yonggang Luo
@ 2023-06-20  0:25 ` Yonggang Luo
  2023-06-20  7:53   ` [musl] " 罗勇刚(Yonggang Luo)
  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
  4 siblings, 1 reply; 9+ messages in thread
From: Yonggang Luo @ 2023-06-20  0:25 UTC (permalink / raw)
  To: Jens Gustedt, musl; +Cc: Yonggang Luo

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [musl] Re: [PATCH 3/4] c23: Implement newly base for timespec_get
  2023-06-20  0:25 ` [musl] [PATCH 3/4] c23: Implement newly base for timespec_get Yonggang Luo
@ 2023-06-20  7:06   ` Jₑₙₛ Gustedt
  0 siblings, 0 replies; 9+ messages in thread
From: Jₑₙₛ Gustedt @ 2023-06-20  7:06 UTC (permalink / raw)
  To: Yonggang Luo; +Cc: musl

[-- Attachment #1: Type: text/plain, Size: 1257 bytes --]

Hello Yonggang,

On Tue, 20 Jun 2023 08:25:06 +0800, Yonggang Luo wrote:

> These are:
> #define TIME_MONOTONIC          2
> #define TIME_PROCESS_CPUTIME_ID 3
> #define TIME_THREAD_CPUTIME_ID  4

C23 has these as optional time bases TIME_MONOTONIC, TIME_ACTIVE and
TIME_THREAD_ACTIVE so you should stick to these names.

> #define TIME_MONOTONIC_RAW      5
> #define TIME_REALTIME_COARSE    6
> #define TIME_MONOTONIC_COARSE   7
> #define TIME_BOOTTIME           8
> #define TIME_REALTIME_ALARM     9
> #define TIME_BOOTTIME_ALARM     10
> #define TIME_SGI_CYCLE          11
> #define TIME_TAI                12

Although implementations would be free to chose names for such
extension as they please, I'd suggest you derive the names from the
existing C standard names were this is appropriate. So this then would
in particular be

> #define TIME_UTC_COARSE    6
> #define TIME_UTC_ALARM     9

Thanks
Jₑₙₛ


-- 
:: ICube :::::::::::::::::::::::::::::: deputy director ::
:: Université de Strasbourg :::::::::::::::::::::: ICPS ::
:: INRIA Nancy Grand Est :::::::::::::::::::::::: Camus ::
:: :::::::::::::::::::::::::::::::::::: ☎ +33 368854536 ::
:: https://icube-icps.unistra.fr/index.php/Jens_Gustedt ::

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [musl] Re: [PATCH 4/4] c2y: Add monotonic timed wait support for threads mtx cnd
  2023-06-20  0:25 ` [musl] [PATCH 4/4] c2y: Add monotonic timed wait support for threads mtx cnd Yonggang Luo
@ 2023-06-20  7:53   ` 罗勇刚(Yonggang Luo)
  2023-06-20  8:51     ` Jₑₙₛ Gustedt
  0 siblings, 1 reply; 9+ messages in thread
From: 罗勇刚(Yonggang Luo) @ 2023-06-20  7:53 UTC (permalink / raw)
  To: Jens Gustedt, musl

[-- Attachment #1: Type: text/plain, Size: 9109 bytes --]

I'd like to receive some feedback of the function names
mtx_timedlock_monotonic
cnd_timedwait_monotonic

is properly as a proposal for c2y(the next standard after c23).
If that's acceptable by anyone, I'd like to use these names in the mesa
code base.
Currently if I want to use monotonic timedlock and timedwait in mesa code
base, I needs implement complicated
https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21291/diffs?commit_id=b240090ceb3ccdfb12fa0a3a258328e7df09803d
So I'd like to introduce these two functions in c2y, and indeed mesa is not
a sole user that face this issue,


On Tue, Jun 20, 2023 at 8:25 AM Yonggang Luo <luoyonggang@gmail.com> wrote:
>
> 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
>


--
         此致
礼
罗勇刚
Yours
    sincerely,
Yonggang Luo

[-- Attachment #2: Type: text/html, Size: 11038 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [musl] Re: [PATCH 4/4] c2y: Add monotonic timed wait support for threads mtx cnd
  2023-06-20  7:53   ` [musl] " 罗勇刚(Yonggang Luo)
@ 2023-06-20  8:51     ` Jₑₙₛ Gustedt
  0 siblings, 0 replies; 9+ messages in thread
From: Jₑₙₛ Gustedt @ 2023-06-20  8:51 UTC (permalink / raw)
  To: 罗勇刚(Yonggang Luo); +Cc: musl

Hello,

On Tue, 20 Jun 2023 15:53:42 +0800, 罗勇刚(Yonggang Luo) wrote:

> I'd like to receive some feedback of the function names
> mtx_timedlock_monotonic
> cnd_timedwait_monotonic

I find such interfaces too specific.

> is properly as a proposal for c2y(the next standard after c23).

I'd much prefer an interface that just adds the time base as a
parameter, similar to `pthread_cond_clockdwait` or so, perhaps named
`cnd_timedwait_base`.

That would be much easier to standardize. Just take an existing POSIX
interface, adapt from POSIX clocks to time bases, use a name that fits
into the C naming scheme, and copy over and adapt the description.

Thanks
Jₑₙₛ


-- 
:: ICube :::::::::::::::::::::::::::::: deputy director ::
:: Université de Strasbourg :::::::::::::::::::::: ICPS ::
:: INRIA Nancy Grand Est :::::::::::::::::::::::: Camus ::
:: :::::::::::::::::::::::::::::::::::: ☎ +33 368854536 ::
:: https://icube-icps.unistra.fr/index.php/Jens_Gustedt ::

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [musl] [PATCH 0/4] Add posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait c2y/mtx_timedlock_monotonic c2y/cnd_timedwait_monotonic
  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
                   ` (3 preceding siblings ...)
  2023-06-20  0:25 ` [musl] [PATCH 4/4] c2y: Add monotonic timed wait support for threads mtx cnd Yonggang Luo
@ 2023-06-20 14:18 ` enh
  4 siblings, 0 replies; 9+ messages in thread
From: enh @ 2023-06-20 14:18 UTC (permalink / raw)
  To: musl; +Cc: Jens Gustedt, Yonggang Luo

[-- Attachment #1: Type: text/plain, Size: 3820 bytes --]

On Mon, Jun 19, 2023 at 5:25 PM Yonggang Luo <luoyonggang@gmail.com> wrote:

> Currently, musl doesn't have pthread_mutex_clocklock
> pthread_cond_clockdwait, but
> glibc, android bionic, qnx libc already have these two functions, so
> implement them in
> musl.
>
> And for c11 threads, the mtx and cnd doesn't support for monotonic
> timedlock and timedwait,
> so add a proposaled function mtx_timedlock_monotonic
> cnd_timedwait_monotonic to do that.
>
> And indeed mtx_timedlock_monotonic and cnd_timedwait_monotonic can be
> implemented ontop of
> posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait, so I
> implemented posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait
> first in musl.
>
> I think mtx_timedlock_monotonic cnd_timedwait_monotonic is reasonable
> because it's newly added
> function and won't affect existing c11 threads functions.
> And it's can be implementd with glibc/qnx libc/android bionic without
> burden.
>

they _could_ be implemented, sure, but speaking as bionic's maintainer, i'd
definitely object to adding something so specific ... the whole reason with
worked with the glibc folks to get the new pthread functions with a clock
parameter standardized was because we were sick of the random subset of
functions working on specific clocks, and wanted to just have the clock be
a parameter.

if C wants to add equivalents to the pthread functions, those functions
should also take arguments that can be any of their TIME_* values rather
than hard-coding clocks into function names.


> For OS X it's can be implemented with pthread_cond_timedwait_relative_np
> For Windows it's can be implemented with SleepConditionVariableCS
> For platform have none of these, it's still can fallback to TIME_UTC
>
> When these
>
> Yonggang Luo (4):
>   trim spaces of pthread_cond_timedwait.c and pthread_mutex_timedlock.c
>   add pthread_mutex_clocklock and pthread_cond_clockdwait
>   c23: Implement newly base for timespec_get
>   c2y: Add monotonic timed wait support for threads mtx cnd
>
>  .../time32/cnd_timedwait_monotonic_time32.c   |  9 ++++
>  .../time32/mtx_timedlock_monotonic_time32.c   |  9 ++++
>  compat/time32/pthread_cond_clockwait_time32.c |  9 ++++
>  .../time32/pthread_mutex_clocklock_time32.c   |  9 ++++
>  compat/time32/time32.h                        |  4 ++
>  include/pthread.h                             |  4 ++
>  include/threads.h                             |  4 ++
>  include/time.h                                | 13 +++++-
>  src/include/pthread.h                         |  2 +
>  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 +++++------
>  src/thread/pthread_cond_clockwait.c           |  3 ++
>  src/thread/pthread_cond_timedwait.c           | 18 +++++---
>  src/thread/pthread_mutex_clocklock.c          |  3 ++
>  src/thread/pthread_mutex_timedlock.c          | 15 ++++---
>  src/time/timespec_get.c                       | 44 ++++++++++++++++++-
>  18 files changed, 161 insertions(+), 43 deletions(-)
>  create mode 100644 compat/time32/cnd_timedwait_monotonic_time32.c
>  create mode 100644 compat/time32/mtx_timedlock_monotonic_time32.c
>  create mode 100644 compat/time32/pthread_cond_clockwait_time32.c
>  create mode 100644 compat/time32/pthread_mutex_clocklock_time32.c
>  copy src/thread/{cnd_timedwait.c => cnd_timedwait_monotonic.c} (52%)
>  copy src/thread/{mtx_timedlock.c => mtx_timedlock_monotonic.c} (74%)
>  create mode 100644 src/thread/pthread_cond_clockwait.c
>  create mode 100644 src/thread/pthread_mutex_clocklock.c
>
> --
> 2.39.0.windows.1
>
>

[-- Attachment #2: Type: text/html, Size: 4627 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-06-20 14:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [musl] [PATCH 4/4] c2y: Add monotonic timed wait support for threads mtx cnd Yonggang Luo
2023-06-20  7:53   ` [musl] " 罗勇刚(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

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).