mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:07:34 +00:00
LibC: Add futex_wait() and futex_wake() helpers
These are convinient wrappers over the most used futex operations. futex_wait() also does some smarts for timeout and clock handling. Use the new futex_wait() instead of a similar private helper in LibPthread.
This commit is contained in:
parent
a482a3e609
commit
8fee93d868
2 changed files with 26 additions and 15 deletions
|
@ -458,27 +458,16 @@ int pthread_cond_destroy(pthread_cond_t*)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int futex_wait(uint32_t& futex_addr, uint32_t value, const struct timespec* abstime)
|
||||
{
|
||||
int saved_errno = errno;
|
||||
// NOTE: FUTEX_WAIT takes a relative timeout, so use FUTEX_WAIT_BITSET instead!
|
||||
int rc = futex(&futex_addr, FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME, value, abstime, nullptr, FUTEX_BITSET_MATCH_ANY);
|
||||
if (rc < 0 && errno == EAGAIN) {
|
||||
// If we didn't wait, that's not an error
|
||||
errno = saved_errno;
|
||||
rc = 0;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* abstime)
|
||||
{
|
||||
u32 value = cond->value;
|
||||
cond->previous = value;
|
||||
pthread_mutex_unlock(mutex);
|
||||
int rc = futex_wait(cond->value, value, abstime);
|
||||
int rc = futex_wait(&cond->value, value, abstime, cond->clockid);
|
||||
pthread_mutex_lock(mutex);
|
||||
return rc;
|
||||
if (rc < 0 && errno != EAGAIN)
|
||||
return errno;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue