1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:47:44 +00:00

LibPthread: Reimplement semaphores

This implementation does not use locking or condition variables
internally; it's purely based on atomics and futexes.

Notably, concurrent sem_wait() and sem_post() calls can run *completely
in parallel* without slowing each other down, as long as there are empty
slots for them all to succeed without blocking.

Additionally, sem_wait() never executes an atomic operation with release
ordering, and sem_post() never executes an atomic operation with acquire
ordering (unless you count the syscall). This means the compiler and the
hardware are free to reorder code *into* the critical section.
This commit is contained in:
Sergey Bugaev 2021-07-05 15:10:34 +03:00 committed by Andreas Kling
parent 00d8dbe739
commit 690141ff8b
2 changed files with 107 additions and 132 deletions

View file

@ -14,9 +14,7 @@
__BEGIN_DECLS
typedef struct {
pthread_mutex_t mtx;
pthread_cond_t cv;
int value;
uint32_t value;
} sem_t;
int sem_close(sem_t*);
@ -28,6 +26,7 @@ int sem_post(sem_t*);
int sem_trywait(sem_t*);
int sem_unlink(const char*);
int sem_wait(sem_t*);
int sem_timedwait(sem_t*, const struct timespec* abstime);
#define SEM_VALUE_MAX INT_MAX