mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 06:48:31 +00:00
Speed up unnamed POSIX semaphores
When sem_wait() used its futexes it would always use process shared mode which can be problematic on platforms like Windows, where that causes it to use the slow futex polyfill. Now when sem_init() is called in private mode that'll be passed along so we can use a faster WaitOnAddress() call
This commit is contained in:
parent
b5fcb59a85
commit
462ba6909e
5 changed files with 76 additions and 30 deletions
|
@ -18,6 +18,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/strace.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/semaphore.h"
|
||||
|
@ -40,14 +41,20 @@
|
|||
* @raise EBUSY if `sem` has waiters
|
||||
*/
|
||||
int sem_destroy(sem_t *sem) {
|
||||
int waiters;
|
||||
int rc, waiters;
|
||||
npassert(sem->sem_magic != SEM_MAGIC_NAMED);
|
||||
if (sem->sem_magic != SEM_MAGIC_UNNAMED)
|
||||
return einval();
|
||||
waiters = atomic_load_explicit(&sem->sem_waiters, memory_order_relaxed);
|
||||
unassert(waiters >= 0);
|
||||
if (waiters)
|
||||
return ebusy();
|
||||
atomic_store_explicit(&sem->sem_value, INT_MIN, memory_order_relaxed);
|
||||
return 0;
|
||||
if (sem->sem_magic != SEM_MAGIC_UNNAMED) {
|
||||
rc = einval();
|
||||
} else {
|
||||
waiters = atomic_load_explicit(&sem->sem_waiters, memory_order_relaxed);
|
||||
unassert(waiters >= 0);
|
||||
if (waiters) {
|
||||
rc = ebusy();
|
||||
} else {
|
||||
atomic_store_explicit(&sem->sem_value, INT_MIN, memory_order_relaxed);
|
||||
rc = 0;
|
||||
}
|
||||
}
|
||||
STRACE("sem_destroy(%p) → %d% m", sem, rc);
|
||||
return rc;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue