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

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/sysv/consts/clock.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
#include "third_party/nsync/cv.h"
@ -34,7 +35,8 @@ int Put(long v, nsync_time abs_deadline) {
int err, added = 0, wake = 0;
nsync_mu_lock(&mu);
while (count == limit) {
if ((err = nsync_cv_wait_with_deadline(&non_full, &mu, abs_deadline, 0))) {
if ((err = nsync_cv_wait_with_deadline(&non_full, &mu, CLOCK_REALTIME,
abs_deadline, 0))) {
ASSERT_EQ(ETIMEDOUT, err);
ASSERT_NE(0, nsync_time_cmp(nsync_time_no_deadline, abs_deadline));
}
@ -59,7 +61,8 @@ long Get(nsync_time abs_deadline) {
long err, v = 0;
nsync_mu_lock(&mu);
while (!count) {
if ((err = nsync_cv_wait_with_deadline(&non_empty, &mu, abs_deadline, 0))) {
if ((err = nsync_cv_wait_with_deadline(&non_empty, &mu, CLOCK_REALTIME,
abs_deadline, 0))) {
ASSERT_EQ(ETIMEDOUT, err);
ASSERT_NE(0, nsync_time_cmp(nsync_time_no_deadline, abs_deadline));
}

View file

@ -0,0 +1,64 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2024 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/struct/timespec.h"
#include "libc/errno.h"
#include "libc/sysv/consts/clock.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
#include "libc/thread/thread2.h"
TEST(pthread_cond_timedwait, real) {
pthread_cond_t cv;
pthread_mutex_t mu;
pthread_condattr_t ca;
ASSERT_EQ(0, pthread_condattr_init(&ca));
ASSERT_EQ(0, pthread_condattr_setclock(&ca, CLOCK_REALTIME));
ASSERT_EQ(0, pthread_cond_init(&cv, &ca));
ASSERT_EQ(0, pthread_condattr_destroy(&ca));
ASSERT_EQ(0, pthread_mutex_init(&mu, 0));
ASSERT_EQ(0, pthread_mutex_lock(&mu));
struct timespec start = timespec_real();
struct timespec deadline = timespec_add(start, timespec_frommillis(100));
ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&cv, &mu, &deadline));
struct timespec end = timespec_real();
ASSERT_GE(timespec_tomillis(timespec_sub(end, start)), 100);
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
ASSERT_EQ(0, pthread_cond_destroy(&cv));
}
TEST(pthread_cond_timedwait, mono) {
pthread_cond_t cv;
pthread_mutex_t mu;
pthread_condattr_t ca;
ASSERT_EQ(0, pthread_condattr_init(&ca));
ASSERT_EQ(0, pthread_condattr_setclock(&ca, CLOCK_MONOTONIC));
ASSERT_EQ(0, pthread_cond_init(&cv, &ca));
ASSERT_EQ(0, pthread_condattr_destroy(&ca));
ASSERT_EQ(0, pthread_mutex_init(&mu, 0));
ASSERT_EQ(0, pthread_mutex_lock(&mu));
struct timespec start = timespec_mono();
struct timespec deadline = timespec_add(start, timespec_frommillis(100));
ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&cv, &mu, &deadline));
struct timespec end = timespec_mono();
ASSERT_GE(timespec_tomillis(timespec_sub(end, start)), 100);
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
ASSERT_EQ(0, pthread_cond_destroy(&cv));
}

View file

@ -26,7 +26,6 @@
#include "libc/calls/ucontext.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/limits.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/itimer.h"