Files
semi-libc/src/thread/__timedwait.c
T
Jens Gustedt d906fa3118 unify the use of FUTEX_PRIVATE
The flag 1<<7 is used in several places for different purposes that are
not always easy to distinguish. Mark those usages that correspond to the
flag that is used by the kernel for futexes.
2017-07-04 17:12:52 -04:00

47 lines
1.1 KiB
C

#include <pthread.h>
#include <time.h>
#include <errno.h>
#include "futex.h"
#include "syscall.h"
#include "pthread_impl.h"
int __pthread_setcancelstate(int, int *);
int __clock_gettime(clockid_t, struct timespec *);
int __timedwait_cp(volatile int *addr, int val,
clockid_t clk, const struct timespec *at, int priv)
{
int r;
struct timespec to, *top=0;
if (priv) priv = FUTEX_PRIVATE;
if (at) {
if (at->tv_nsec >= 1000000000UL) return EINVAL;
if (__clock_gettime(clk, &to)) return EINVAL;
to.tv_sec = at->tv_sec - to.tv_sec;
if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
to.tv_sec--;
to.tv_nsec += 1000000000;
}
if (to.tv_sec < 0) return ETIMEDOUT;
top = &to;
}
r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT|priv, val, top);
if (r == ENOSYS) r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT, val, top);
if (r != EINTR && r != ETIMEDOUT && r != ECANCELED) r = 0;
return r;
}
int __timedwait(volatile int *addr, int val,
clockid_t clk, const struct timespec *at, int priv)
{
int cs, r;
__pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
r = __timedwait_cp(addr, val, clk, at, priv);
__pthread_setcancelstate(cs, 0);
return r;
}