|
|
Message-Id: <20230620143703.1415-6-luoyonggang@gmail.com>
Date: Tue, 20 Jun 2023 22:37:03 +0800
From: Yonggang Luo <luoyonggang@...il.com>
To: Jens Gustedt <jens.gustedt@...ia.fr>,
enh <enh@...gle.com>,
musl@...ts.openwall.com
Cc: Yonggang Luo <luoyonggang@...il.com>
Subject: [PATCH v3 5/5] c2y: Add monotonic timedlock/timedwait support for threads mtx/cnd
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@...il.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
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.