mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-03 19:22:27 +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
|
@ -19,6 +19,7 @@
|
|||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.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"
|
||||
|
@ -37,12 +38,17 @@
|
|||
* @raise EINVAL if `value` exceeds `SEM_VALUE_MAX`
|
||||
*/
|
||||
int sem_init(sem_t *sem, int pshared, unsigned value) {
|
||||
if (value > SEM_VALUE_MAX)
|
||||
return einval();
|
||||
sem->sem_magic = SEM_MAGIC_UNNAMED;
|
||||
atomic_store_explicit(&sem->sem_value, value, memory_order_relaxed);
|
||||
sem->sem_pshared = !!pshared;
|
||||
sem->sem_pid = getpid();
|
||||
sem->sem_waiters = 0;
|
||||
return 0;
|
||||
int rc;
|
||||
if (value > SEM_VALUE_MAX) {
|
||||
rc = einval();
|
||||
} else {
|
||||
sem->sem_magic = SEM_MAGIC_UNNAMED;
|
||||
atomic_store_explicit(&sem->sem_value, value, memory_order_relaxed);
|
||||
sem->sem_pshared = !!pshared;
|
||||
sem->sem_pid = getpid();
|
||||
sem->sem_waiters = 0;
|
||||
rc = 0;
|
||||
}
|
||||
STRACE("sem_init(%p, %hhhd, %u) → %d% m", sem, pshared, value, rc);
|
||||
return rc;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue