Introduce pthread_condattr_setclock()

This is one of the few POSIX APIs that was missing. It lets you choose a
monotonic clock for your condition variables. This might improve perf on
some platforms. It might also grant more flexibility with NTP configs. I
know Qt is one project that believes it needs this. To introduce this, I
needed to change some the *NSYNC APIs, to support passing a clock param.
There's also new benchmarks, demonstrating Cosmopolitan's supremacy over
many libc implementations when it comes to mutex performance. Cygwin has
an alarmingly bad pthread_mutex_t implementation. It is so bad that they
would have been significantly better off if they'd used naive spinlocks.
This commit is contained in:
Justine Tunney 2024-09-02 23:37:50 -07:00
parent 79516bf08e
commit 3c61a541bd
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
55 changed files with 449 additions and 155 deletions

View file

@ -1,9 +1,9 @@
// config
#define USE POSIX
#define ITERATIONS 50000
#define THREADS 10
// #define ITERATIONS 100000
// #define THREADS 30
// USE may be
#define SPIN 1
#define FUTEX 2
#define POSIX 3
@ -26,6 +26,7 @@
#include <linux/futex.h>
#include <sys/syscall.h>
static inline long nsync_futex_wait_(atomic_int *uaddr, int val, char pshare,
int clock,
const struct timespec *timeout) {
return syscall(SYS_futex, uaddr, pshare ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE,
val, timeout, NULL, 0);
@ -144,25 +145,40 @@ static inline long nsync_futex_wake_(atomic_int *uaddr, int num_to_wake,
// 216,236 us user
// 127,344 us sys
//
// footek_test on freebsd.test. 613 µs 2'120 µs 133'272 µs
// footek_test on freebsd.test. (cosmo)
// 126,803 us real
// 3,100 us user
// 176,744 us sys
//
// footek_test on freebsd.test. (freebsd libc)
// 219,073 us real
// 158,103 us user
// 1,146,252 us sys
//
// footek_test on netbsd.test. 350 µs 3'570 µs 262'186 µs
// 199,882 us real
// 138,178 us user
// 329,501 us sys
//
// footek_test on openbsd.test. 454 µs 2'185 µs 153'258 µs
// footek_test on openbsd.test. (cosmo)
// 138,619 us real
// 30,000 us user
// 110,000 us sys
//
// footek_test on win10.test. 233 µs 6'133 µs 260'812 µs
// footek_test on openbsd.test. (openbsd libc)
// 385,431 us real
// 80,000 us user
// 1,350,000 us sys
//
// footek_test on win10.test. (cosmo)
// 156,382 us real
// 312,500 us user
// 31,250 us sys
//
// footek_test on win10.test. (cygwin)
// 9,334,610 us real
// 1,562,000 us user
// 6,093,000 us sys
// arm fleet
// with spin lock
@ -261,7 +277,7 @@ void lock(atomic_int *futex) {
while (word > 0) {
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
#if USE == FUTEX
nsync_futex_wait_(futex, 2, 0, 0);
nsync_futex_wait_(futex, 2, 0, 0, 0);
#endif
pthread_setcancelstate(cs, 0);
word = atomic_exchange_explicit(futex, 2, memory_order_acquire);