diff --git a/examples/thread.c b/examples/thread.c index d156f89b6..3c7783e6d 100644 --- a/examples/thread.c +++ b/examples/thread.c @@ -12,13 +12,13 @@ #include "libc/thread/self.h" #include "libc/thread/detach.h" #include "libc/thread/join.h" -#include "libc/thread/nativesem.h" +#include "libc/thread/sem.h" #include "libc/time/time.h" -cthread_native_sem_t semaphore; +cthread_sem_t semaphore; int worker(void* arg) { - cthread_native_sem_signal(&semaphore); + cthread_sem_signal(&semaphore); cthread_t self = cthread_self(); int tid = self->tid; @@ -30,12 +30,12 @@ int worker(void* arg) { } int main() { - cthread_native_sem_init(&semaphore, 0); + cthread_sem_init(&semaphore, 0); cthread_t thread; int rc = cthread_create(&thread, NULL, &worker, NULL); if (rc == 0) { - cthread_native_sem_wait(&semaphore, 0, NULL); + cthread_sem_wait(&semaphore, 0, NULL); //printf("thread created: %p\n", thread); sleep(1); #if 1 @@ -44,8 +44,8 @@ int main() { rc = cthread_detach(thread); sleep(2); #endif - cthread_native_sem_signal(&semaphore); - cthread_native_sem_wait(&semaphore, 0, NULL); + cthread_sem_signal(&semaphore); + cthread_sem_wait(&semaphore, 0, NULL); //printf("thread joined: %p -> %d\n", thread, rc); } else { printf("ERROR: thread could not be started: %d\n", rc); diff --git a/libc/thread/nativesem.c b/libc/thread/sem.c similarity index 82% rename from libc/thread/nativesem.c rename to libc/thread/sem.c index f260e177e..0789f1bd0 100644 --- a/libc/thread/nativesem.c +++ b/libc/thread/sem.c @@ -19,7 +19,7 @@ #include "libc/bits/atomic.h" #include "libc/sysv/consts/futex.h" #include "libc/sysv/consts/nr.h" -#include "libc/thread/nativesem.h" +#include "libc/thread/sem.h" #include "libc/thread/yield.h" #define CTHREAD_THREAD_VAL_BITS 32 @@ -34,16 +34,16 @@ static void pause(int attempt) { } } -int cthread_native_sem_init(cthread_native_sem_t* sem, int count) { +int cthread_sem_init(cthread_sem_t* sem, int count) { sem->linux.count = count; return 0; } -int cthread_native_sem_destroy(cthread_native_sem_t* sem) { +int cthread_sem_destroy(cthread_sem_t* sem) { (void)sem; return 0; } -int cthread_native_sem_signal(cthread_native_sem_t* sem) { +int cthread_sem_signal(cthread_sem_t* sem) { uint64_t count; asm volatile("lock xadd\t%1, %0" : "+m"(sem->linux.count), "=r"(count) @@ -64,8 +64,7 @@ int cthread_native_sem_signal(cthread_native_sem_t* sem) { return 0; } -int cthread_native_sem_wait_futex(cthread_native_sem_t* sem, - const struct timespec* timeout) { +int cthread_sem_wait_futex(cthread_sem_t* sem, const struct timespec* timeout) { uint64_t count; // record current thread as waiter @@ -101,36 +100,34 @@ int cthread_native_sem_wait_futex(cthread_native_sem_t* sem, return 0; } -int cthread_native_sem_wait_spin(cthread_native_sem_t* sem, uint64_t count, - int spin, const struct timespec* timeout) { +int cthread_sem_wait_spin(cthread_sem_t* sem, uint64_t count, int spin, + const struct timespec* timeout) { // spin on pause for (int attempt = 0; attempt < spin; ++attempt) { //if ((count >> CTHREAD_THREAD_VAL_BITS) != 0) break; while ((uint32_t)count > 0) { // spin is useful if multiple waiters can acquire the semaphore at the same time - if (atomic_compare_exchange_weak( - &sem->linux.count, count, count - 1)) { + if (atomic_compare_exchange_weak(&sem->linux.count, count, count - 1)) { return 0; } } pause(attempt); } - return cthread_native_sem_wait_futex(sem, timeout); + return cthread_sem_wait_futex(sem, timeout); } -int cthread_native_sem_wait(cthread_native_sem_t* sem, int spin, - const struct timespec* timeout) { +int cthread_sem_wait(cthread_sem_t* sem, int spin, + const struct timespec* timeout) { uint64_t count = atomic_load(&sem->linux.count); // uncontended while ((uint32_t)count > 0) { // spin is useful if multiple waiters can acquire the semaphore at the same time - if (atomic_compare_exchange_weak( - &sem->linux.count, count, count - 1)) { + if (atomic_compare_exchange_weak(&sem->linux.count, count, count - 1)) { return 0; } } - return cthread_native_sem_wait_spin(sem, count, spin, timeout); + return cthread_sem_wait_spin(sem, count, spin, timeout); } diff --git a/libc/thread/nativesem.h b/libc/thread/sem.h similarity index 56% rename from libc/thread/nativesem.h rename to libc/thread/sem.h index aab938b2a..1e4183f34 100644 --- a/libc/thread/nativesem.h +++ b/libc/thread/sem.h @@ -7,19 +7,19 @@ COSMOPOLITAN_C_START_ * @fileoverview native semaphore for implementation details */ -typedef union cthread_native_sem_t { +typedef union cthread_sem_t { struct { uint64_t count; } linux; -} cthread_native_sem_t; +} cthread_sem_t; struct timespec; -int cthread_native_sem_init(cthread_native_sem_t*, int); -int cthread_native_sem_destroy(cthread_native_sem_t*); +int cthread_sem_init(cthread_sem_t*, int); +int cthread_sem_destroy(cthread_sem_t*); -int cthread_native_sem_wait(cthread_native_sem_t*, int, const struct timespec*); -int cthread_native_sem_signal(cthread_native_sem_t*); +int cthread_sem_wait(cthread_sem_t*, int, const struct timespec*); +int cthread_sem_signal(cthread_sem_t*); COSMOPOLITAN_C_END_