Skip to content

Commit e7d6bef

Browse files
committed
Add support for pthread_cond_clockwait(3)
1 parent 5217ea1 commit e7d6bef

File tree

15 files changed

+519
-57
lines changed

15 files changed

+519
-57
lines changed

kos/include/bits/crt/pthreadtypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ typedef union __pthread_condattr {
445445
#endif /* __CC__ */
446446
#endif /* !__USE_PTHREAD_INTERNALS */
447447

448-
#define __OFFSET_PTHREAD_COND_LOCK 0
448+
#define __OFFSET_PTHREAD_COND_FLAGS 0
449449
#define __OFFSET_PTHREAD_COND_FUTEX 4
450450
#define __OFFSET_PTHREAD_COND_TOTAL_SEQ 8
451451
#define __OFFSET_PTHREAD_COND_WAKEUP_SEQ 16
@@ -456,7 +456,7 @@ typedef union __pthread_condattr {
456456
#ifdef __CC__
457457
struct __pthread_cond_s {
458458
#ifdef __USE_PTHREAD_INTERNALS
459-
__INT32_TYPE__ _c_lock; /* Unused (on KOS) */
459+
__UINT32_TYPE__ c_flags; /* Set of `LFUTEX_WAIT_FLAG_TIMEOUT_PRIVATE | LFUTEX_WAIT_FLAG_TIMEOUT_REALTIME' */
460460
__UINT32_TYPE__ c_futex; /* Futex control word & condition-version counter. */
461461
__UINT64_TYPE__ _c_total_seq; /* Unused (on KOS) */
462462
__UINT64_TYPE__ _c_wakeup_seq; /* Unused (on KOS) */

kos/include/i386-kos/crt-features/crt-kos32.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,7 @@
13171317
#define __CRT_HAVE___pthread_barrier_init
13181318
#define __CRT_HAVE___pthread_barrier_wait
13191319
#define __CRT_HAVE___pthread_cleanup_routine
1320+
#define __CRT_HAVE___pthread_cond_clockwait64
13201321
#define __CRT_HAVE___pthread_cond_destroy
13211322
#define __CRT_HAVE___pthread_cond_init
13221323
#define __CRT_HAVE___pthread_cond_timedwait64
@@ -4666,6 +4667,8 @@
46664667
#define __CRT_HAVE_pthread_barrierattr_setpshared
46674668
#define __CRT_HAVE_pthread_cancel
46684669
#define __CRT_HAVE_pthread_cond_broadcast
4670+
#define __CRT_HAVE_pthread_cond_clockwait
4671+
#define __CRT_HAVE_pthread_cond_clockwait64
46694672
#define __CRT_HAVE_pthread_cond_destroy
46704673
#define __CRT_HAVE_pthread_cond_init
46714674
#define __CRT_HAVE_pthread_cond_reltimedwait64_np

kos/include/i386-kos/crt-features/crt-kos64.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,7 @@
12591259
#define __CRT_HAVE___pthread_barrier_init
12601260
#define __CRT_HAVE___pthread_barrier_wait
12611261
#define __CRT_HAVE___pthread_cleanup_routine
1262+
#define __CRT_HAVE___pthread_cond_clockwait64
12621263
#define __CRT_HAVE___pthread_cond_destroy
12631264
#define __CRT_HAVE___pthread_cond_init
12641265
#define __CRT_HAVE___pthread_getspecific
@@ -4350,6 +4351,7 @@
43504351
#define __CRT_HAVE_pthread_barrierattr_setpshared
43514352
#define __CRT_HAVE_pthread_cancel
43524353
#define __CRT_HAVE_pthread_cond_broadcast
4354+
#define __CRT_HAVE_pthread_cond_clockwait
43534355
#define __CRT_HAVE_pthread_cond_destroy
43544356
#define __CRT_HAVE_pthread_cond_init
43554357
#define __CRT_HAVE_pthread_cond_reltimedwait_np
@@ -7653,6 +7655,7 @@
76537655
#define __CRT_HAVE_KOS$__pthread_barrier_init
76547656
#define __CRT_HAVE_KOS$__pthread_barrier_wait
76557657
#define __CRT_HAVE_KOS$__pthread_cleanup_routine
7658+
#define __CRT_HAVE_KOS$__pthread_cond_clockwait64
76567659
#define __CRT_HAVE_KOS$__pthread_cond_destroy
76577660
#define __CRT_HAVE_KOS$__pthread_cond_init
76587661
#define __CRT_HAVE_KOS$__pthread_getspecific
@@ -10248,6 +10251,7 @@
1024810251
#define __CRT_HAVE_KOS$pthread_barrierattr_setpshared
1024910252
#define __CRT_HAVE_KOS$pthread_cancel
1025010253
#define __CRT_HAVE_KOS$pthread_cond_broadcast
10254+
#define __CRT_HAVE_KOS$pthread_cond_clockwait
1025110255
#define __CRT_HAVE_KOS$pthread_cond_destroy
1025210256
#define __CRT_HAVE_KOS$pthread_cond_init
1025310257
#define __CRT_HAVE_KOS$pthread_cond_reltimedwait_np
@@ -12233,6 +12237,7 @@
1223312237
#define __CRT_HAVE_DOS$__pthread_barrier_init
1223412238
#define __CRT_HAVE_DOS$__pthread_barrier_wait
1223512239
#define __CRT_HAVE_DOS$__pthread_cleanup_routine
12240+
#define __CRT_HAVE_DOS$__pthread_cond_clockwait64
1223612241
#define __CRT_HAVE_DOS$__pthread_cond_destroy
1223712242
#define __CRT_HAVE_DOS$__pthread_cond_init
1223812243
#define __CRT_HAVE_DOS$__pthread_getspecific
@@ -15124,6 +15129,7 @@
1512415129
#define __CRT_HAVE_DOS$pthread_barrierattr_setpshared
1512515130
#define __CRT_HAVE_DOS$pthread_cancel
1512615131
#define __CRT_HAVE_DOS$pthread_cond_broadcast
15132+
#define __CRT_HAVE_DOS$pthread_cond_clockwait
1512715133
#define __CRT_HAVE_DOS$pthread_cond_destroy
1512815134
#define __CRT_HAVE_DOS$pthread_cond_init
1512915135
#define __CRT_HAVE_DOS$pthread_cond_reltimedwait_np

kos/include/kos/asm/futex.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,16 @@
114114

115115
/* Flags for waiter futex functions. */
116116
#define LFUTEX_WAIT_FLAG_TIMEOUT_ABSOLUTE __UINT32_C(0x00000000) /* Default: Timeouts are given as absolute positions of CPU quantum time (`CLOCK_PROCESS_CPUTIME_ID') */
117-
#define LFUTEX_WAIT_FLAG_TIMEOUT_RELATIVE __UINT32_C(0x40000000) /* The given timeout argument describes a timeout relative to the point when waiting starts */
117+
#define LFUTEX_WAIT_FLAG_TIMEOUT_FORPOLL __UINT32_C(0x10000000) /* Connect to signals for polling. (s.a. `task_connect_for_poll()') */
118+
#define LFUTEX_WAIT_FLAG_TIMEOUT_RELATIVE __UINT32_C(0x20000000) /* The given timeout argument describes a timeout relative to the point when waiting starts */
119+
#define LFUTEX_WAIT_FLAG_TIMEOUT_PRIVATE __UINT32_C(0x40000000) /* Unused (but meant to represent linux's `FUTEX_PRIVATE_FLAG') */
118120
#define LFUTEX_WAIT_FLAG_TIMEOUT_REALTIME __UINT32_C(0x80000000) /* The given timeout argument is in absolute realtime (`CLOCK_REALTIME') */
119-
#define LFUTEX_WAIT_FLAG_TIMEOUT_FORPOLL __UINT32_C(0x20000000) /* Connect to signals for polling. (s.a. `task_connect_for_poll()') */
121+
122+
/* Convert linux futex flags to KOS:
123+
* >> LFUTEX_WAIT_FLAG_TIMEOUT_FROMLINUX(0) == LFUTEX_WAIT_FLAG_TIMEOUT_ABSOLUTE
124+
* >> LFUTEX_WAIT_FLAG_TIMEOUT_FROMLINUX(FUTEX_CLOCK_REALTIME) == LFUTEX_WAIT_FLAG_TIMEOUT_REALTIME
125+
* >> LFUTEX_WAIT_FLAG_TIMEOUT_FROMLINUX(FUTEX_PRIVATE_FLAG) == LFUTEX_WAIT_FLAG_TIMEOUT_PRIVATE */
126+
#define LFUTEX_WAIT_FLAG_TIMEOUT_FROMLINUX(x) (__CCAST(__UINT32_TYPE__)(x) << 23)
127+
#define LFUTEX_WAIT_FLAG_TIMEOUT_TOLINUX(x) (__CCAST(__INT32_TYPE__)(x) >> 23)
120128

121129
#endif /* !_KOS_ASM_FUTEX_H */
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* HASH CRC-32:0x31856e65 */
2+
/* Copyright (c) 2019-2025 Griefer@Work *
3+
* *
4+
* This software is provided 'as-is', without any express or implied *
5+
* warranty. In no event will the authors be held liable for any damages *
6+
* arising from the use of this software. *
7+
* *
8+
* Permission is granted to anyone to use this software for any purpose, *
9+
* including commercial applications, and to alter it and redistribute it *
10+
* freely, subject to the following restrictions: *
11+
* *
12+
* 1. The origin of this software must not be misrepresented; you must not *
13+
* claim that you wrote the original software. If you use this software *
14+
* in a product, an acknowledgement (see the following) in the product *
15+
* documentation is required: *
16+
* Portions Copyright (c) 2019-2025 Griefer@Work *
17+
* 2. Altered source versions must be plainly marked as such, and must not be *
18+
* misrepresented as being the original software. *
19+
* 3. This notice may not be removed or altered from any source distribution. *
20+
*/
21+
#ifndef __local_pthread_cond_clockwait_defined
22+
#define __local_pthread_cond_clockwait_defined
23+
#include <__crt.h>
24+
#include <bits/types.h>
25+
#if defined(__CRT_HAVE_pthread_cond_clockwait64) || defined(__CRT_HAVE___pthread_cond_clockwait64) || defined(__CRT_HAVE_pthread_cond_clockwait)
26+
#include <bits/crt/pthreadtypes.h>
27+
#include <bits/os/timespec.h>
28+
__NAMESPACE_LOCAL_BEGIN
29+
#if !defined(__local___localdep_crt_pthread_cond_clockwait32_defined) && defined(__CRT_HAVE_pthread_cond_clockwait)
30+
#define __local___localdep_crt_pthread_cond_clockwait32_defined
31+
__CREDIRECT(__ATTR_WUNUSED __ATTR_IN(4) __ATTR_INOUT(1) __ATTR_INOUT(2),__errno_t,__NOTHROW_RPC,__localdep_crt_pthread_cond_clockwait32,(__pthread_cond_t *__restrict __self, __pthread_mutex_t *__restrict __mutex, __clockid_t __clock_id, struct __timespec32 const *__restrict __abstime),pthread_cond_clockwait,(__self,__mutex,__clock_id,__abstime))
32+
#endif /* !__local___localdep_crt_pthread_cond_clockwait32_defined && __CRT_HAVE_pthread_cond_clockwait */
33+
#ifndef __local___localdep_pthread_cond_clockwait64_defined
34+
#define __local___localdep_pthread_cond_clockwait64_defined
35+
#if defined(__CRT_HAVE_pthread_cond_clockwait) && __SIZEOF_TIME32_T__ == __SIZEOF_TIME64_T__
36+
__CREDIRECT(__ATTR_WUNUSED __ATTR_IN(4) __ATTR_INOUT(1) __ATTR_INOUT(2),__errno_t,__NOTHROW_RPC,__localdep_pthread_cond_clockwait64,(__pthread_cond_t *__restrict __self, __pthread_mutex_t *__restrict __mutex, __clockid_t __clock_id, struct __timespec64 const *__restrict __abstime),pthread_cond_clockwait,(__self,__mutex,__clock_id,__abstime))
37+
#elif defined(__CRT_HAVE_pthread_cond_clockwait64)
38+
__CREDIRECT(__ATTR_WUNUSED __ATTR_IN(4) __ATTR_INOUT(1) __ATTR_INOUT(2),__errno_t,__NOTHROW_RPC,__localdep_pthread_cond_clockwait64,(__pthread_cond_t *__restrict __self, __pthread_mutex_t *__restrict __mutex, __clockid_t __clock_id, struct __timespec64 const *__restrict __abstime),pthread_cond_clockwait64,(__self,__mutex,__clock_id,__abstime))
39+
#elif defined(__CRT_HAVE___pthread_cond_clockwait64)
40+
__CREDIRECT(__ATTR_WUNUSED __ATTR_IN(4) __ATTR_INOUT(1) __ATTR_INOUT(2),__errno_t,__NOTHROW_RPC,__localdep_pthread_cond_clockwait64,(__pthread_cond_t *__restrict __self, __pthread_mutex_t *__restrict __mutex, __clockid_t __clock_id, struct __timespec64 const *__restrict __abstime),__pthread_cond_clockwait64,(__self,__mutex,__clock_id,__abstime))
41+
#elif defined(__CRT_HAVE_pthread_cond_clockwait)
42+
__NAMESPACE_LOCAL_END
43+
#include <libc/local/pthread/pthread_cond_clockwait64.h>
44+
__NAMESPACE_LOCAL_BEGIN
45+
#define __localdep_pthread_cond_clockwait64 __LIBC_LOCAL_NAME(pthread_cond_clockwait64)
46+
#else /* ... */
47+
#undef __local___localdep_pthread_cond_clockwait64_defined
48+
#endif /* !... */
49+
#endif /* !__local___localdep_pthread_cond_clockwait64_defined */
50+
__LOCAL_LIBC(pthread_cond_clockwait) __ATTR_WUNUSED __ATTR_IN(4) __ATTR_INOUT(1) __ATTR_INOUT(2) __errno_t
51+
__NOTHROW_RPC(__LIBCCALL __LIBC_LOCAL_NAME(pthread_cond_clockwait))(__pthread_cond_t *__restrict __self, __pthread_mutex_t *__restrict __mutex, __clockid_t __clock_id, struct timespec const *__restrict __abstime) {
52+
#ifdef __CRT_HAVE_pthread_cond_clockwait
53+
__errno_t __result;
54+
struct __timespec32 __abstime32;
55+
__abstime32.tv_sec = (__time32_t)__abstime->tv_sec;
56+
__abstime32.tv_nsec = __abstime->tv_nsec;
57+
__result = (__NAMESPACE_LOCAL_SYM __localdep_crt_pthread_cond_clockwait32)(__self, __mutex, __clock_id, &__abstime32);
58+
return __result;
59+
#else /* __CRT_HAVE_pthread_cond_clockwait */
60+
__errno_t __result;
61+
struct __timespec64 __abstime64;
62+
__abstime64.tv_sec = (__time64_t)__abstime->tv_sec;
63+
__abstime64.tv_nsec = __abstime->tv_nsec;
64+
__result = (__NAMESPACE_LOCAL_SYM __localdep_pthread_cond_clockwait64)(__self, __mutex, __clock_id, &__abstime64);
65+
return __result;
66+
#endif /* !__CRT_HAVE_pthread_cond_clockwait */
67+
}
68+
__NAMESPACE_LOCAL_END
69+
#ifndef __local___localdep_pthread_cond_clockwait_defined
70+
#define __local___localdep_pthread_cond_clockwait_defined
71+
#define __localdep_pthread_cond_clockwait __LIBC_LOCAL_NAME(pthread_cond_clockwait)
72+
#endif /* !__local___localdep_pthread_cond_clockwait_defined */
73+
#else /* __CRT_HAVE_pthread_cond_clockwait64 || __CRT_HAVE___pthread_cond_clockwait64 || __CRT_HAVE_pthread_cond_clockwait */
74+
#undef __local_pthread_cond_clockwait_defined
75+
#endif /* !__CRT_HAVE_pthread_cond_clockwait64 && !__CRT_HAVE___pthread_cond_clockwait64 && !__CRT_HAVE_pthread_cond_clockwait */
76+
#endif /* !__local_pthread_cond_clockwait_defined */
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* HASH CRC-32:0xef122bac */
2+
/* Copyright (c) 2019-2025 Griefer@Work *
3+
* *
4+
* This software is provided 'as-is', without any express or implied *
5+
* warranty. In no event will the authors be held liable for any damages *
6+
* arising from the use of this software. *
7+
* *
8+
* Permission is granted to anyone to use this software for any purpose, *
9+
* including commercial applications, and to alter it and redistribute it *
10+
* freely, subject to the following restrictions: *
11+
* *
12+
* 1. The origin of this software must not be misrepresented; you must not *
13+
* claim that you wrote the original software. If you use this software *
14+
* in a product, an acknowledgement (see the following) in the product *
15+
* documentation is required: *
16+
* Portions Copyright (c) 2019-2025 Griefer@Work *
17+
* 2. Altered source versions must be plainly marked as such, and must not be *
18+
* misrepresented as being the original software. *
19+
* 3. This notice may not be removed or altered from any source distribution. *
20+
*/
21+
#ifndef __local_pthread_cond_clockwait64_defined
22+
#define __local_pthread_cond_clockwait64_defined
23+
#include <__crt.h>
24+
#ifdef __CRT_HAVE_pthread_cond_clockwait
25+
#include <bits/types.h>
26+
#include <bits/crt/pthreadtypes.h>
27+
#include <bits/os/timespec.h>
28+
__NAMESPACE_LOCAL_BEGIN
29+
#ifndef __local___localdep_crt_pthread_cond_clockwait32_defined
30+
#define __local___localdep_crt_pthread_cond_clockwait32_defined
31+
__CREDIRECT(__ATTR_WUNUSED __ATTR_IN(4) __ATTR_INOUT(1) __ATTR_INOUT(2),__errno_t,__NOTHROW_RPC,__localdep_crt_pthread_cond_clockwait32,(__pthread_cond_t *__restrict __self, __pthread_mutex_t *__restrict __mutex, __clockid_t __clock_id, struct __timespec32 const *__restrict __abstime),pthread_cond_clockwait,(__self,__mutex,__clock_id,__abstime))
32+
#endif /* !__local___localdep_crt_pthread_cond_clockwait32_defined */
33+
__LOCAL_LIBC(pthread_cond_clockwait64) __ATTR_WUNUSED __ATTR_IN(4) __ATTR_INOUT(1) __ATTR_INOUT(2) __errno_t
34+
__NOTHROW_RPC(__LIBCCALL __LIBC_LOCAL_NAME(pthread_cond_clockwait64))(__pthread_cond_t *__restrict __self, __pthread_mutex_t *__restrict __mutex, __clockid_t __clock_id, struct __timespec64 const *__restrict __abstime) {
35+
__errno_t __result;
36+
struct __timespec32 __abstime32;
37+
__abstime32.tv_sec = (__time32_t)__abstime->tv_sec;
38+
__abstime32.tv_nsec = __abstime->tv_nsec;
39+
__result = (__NAMESPACE_LOCAL_SYM __localdep_crt_pthread_cond_clockwait32)(__self, __mutex, &__abstime32);
40+
return __result;
41+
}
42+
__NAMESPACE_LOCAL_END
43+
#ifndef __local___localdep_pthread_cond_clockwait64_defined
44+
#define __local___localdep_pthread_cond_clockwait64_defined
45+
#define __localdep_pthread_cond_clockwait64 __LIBC_LOCAL_NAME(pthread_cond_clockwait64)
46+
#endif /* !__local___localdep_pthread_cond_clockwait64_defined */
47+
#else /* __CRT_HAVE_pthread_cond_clockwait */
48+
#undef __local_pthread_cond_clockwait64_defined
49+
#endif /* !__CRT_HAVE_pthread_cond_clockwait */
50+
#endif /* !__local_pthread_cond_clockwait64_defined */

kos/include/linux/futex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
#define FUTEX_WAIT_REQUEUE_PI 11
5151
#define FUTEX_CMP_REQUEUE_PI 12
5252

53-
#define FUTEX_PRIVATE_FLAG 128
54-
#define FUTEX_CLOCK_REALTIME 256
53+
#define FUTEX_PRIVATE_FLAG 0x080
54+
#define FUTEX_CLOCK_REALTIME 0x100
5555
#define FUTEX_CMD_MASK (~(FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME))
5656
#define FUTEX_WAIT_PRIVATE (FUTEX_WAIT | FUTEX_PRIVATE_FLAG)
5757
#define FUTEX_WAKE_PRIVATE (FUTEX_WAKE | FUTEX_PRIVATE_FLAG)

0 commit comments

Comments
 (0)