mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH v2 0/4] Add posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait c2y/mtx_timedlock_monotonic c2y/cnd_timedwait_monotonic
@ 2023-06-20  7:44 Yonggang Luo
  2023-06-20  7:44 ` [musl] [PATCH v2 3/4] c23: Implement newly base for timespec_get Yonggang Luo
  0 siblings, 1 reply; 2+ messages in thread
From: Yonggang Luo @ 2023-06-20  7:44 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 using mtx_timedlock and cnd_timedwait
over TIME_UTC

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] 2+ messages in thread

* [musl] [PATCH v2 3/4] c23: Implement newly base for timespec_get
  2023-06-20  7:44 [musl] [PATCH v2 0/4] Add posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait c2y/mtx_timedlock_monotonic c2y/cnd_timedwait_monotonic Yonggang Luo
@ 2023-06-20  7:44 ` Yonggang Luo
  0 siblings, 0 replies; 2+ messages in thread
From: Yonggang Luo @ 2023-06-20  7:44 UTC (permalink / raw)
  To: Jens Gustedt, musl; +Cc: Yonggang Luo

These are implemented https://gustedt.gitlabpages.inria.fr/c23-library/#time_monotonic-time_active-time_thread_active

with:

#define TIME_UTC                1
#define TIME_MONOTONIC          2
#define TIME_ACTIVE             3
#define TIME_THREAD_ACTIVE      4
#define TIME_MONOTONIC_RAW      5
#define TIME_UTC_COARSE         6
#define TIME_MONOTONIC_COARSE   7
#define TIME_BOOTTIME           8
#define TIME_UTC_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..ab31d373 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_ACTIVE             3
+#define TIME_THREAD_ACTIVE      4
+#define TIME_MONOTONIC_RAW      5
+#define TIME_UTC_COARSE         6
+#define TIME_MONOTONIC_COARSE   7
+#define TIME_BOOTTIME           8
+#define TIME_UTC_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..b8738189 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_ACTIVE:
+		clockid = CLOCK_PROCESS_CPUTIME_ID;
+		break;
+	case TIME_THREAD_ACTIVE:
+		clockid = CLOCK_THREAD_CPUTIME_ID;
+		break;
+	case TIME_MONOTONIC_RAW:
+		clockid = CLOCK_MONOTONIC_RAW;
+		break;
+	case TIME_UTC_COARSE:
+		clockid = CLOCK_REALTIME_COARSE;
+		break;
+	case TIME_MONOTONIC_COARSE:
+		clockid = CLOCK_MONOTONIC_COARSE;
+		break;
+	case TIME_BOOTTIME:
+		clockid = CLOCK_BOOTTIME;
+		break;
+	case TIME_UTC_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] 2+ messages in thread

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-20  7:44 [musl] [PATCH v2 0/4] Add posix/pthread_mutex_clocklock posix/pthread_cond_clockdwait c2y/mtx_timedlock_monotonic c2y/cnd_timedwait_monotonic Yonggang Luo
2023-06-20  7:44 ` [musl] [PATCH v2 3/4] c23: Implement newly base for timespec_get Yonggang Luo

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