mirror of
https://github.com/fluencelabs/musl
synced 2025-06-28 06:02:04 +00:00
to deal with the fact that the public headers may be used with pre-c99 compilers, __restrict is used in place of restrict, and defined appropriately for any supported compiler. we also avoid the form [restrict] since older versions of gcc rejected it due to a bug in the original c99 standard, and instead use the form *restrict.
24 lines
423 B
C
24 lines
423 B
C
#include <semaphore.h>
|
|
#include "pthread_impl.h"
|
|
|
|
static void cleanup(void *p)
|
|
{
|
|
a_dec(p);
|
|
}
|
|
|
|
int sem_timedwait(sem_t *restrict sem, const struct timespec *restrict at)
|
|
{
|
|
while (sem_trywait(sem)) {
|
|
int r;
|
|
a_inc(sem->__val+1);
|
|
a_cas(sem->__val, 0, -1);
|
|
r = __timedwait(sem->__val, -1, CLOCK_REALTIME, at, cleanup, sem->__val+1, 0);
|
|
a_dec(sem->__val+1);
|
|
if (r) {
|
|
errno = r;
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|