2011-08-02 21:11:36 -04:00
|
|
|
#include <pthread.h>
|
2011-02-12 00:22:29 -05:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "futex.h"
|
|
|
|
#include "syscall.h"
|
2011-04-17 11:43:03 -04:00
|
|
|
|
2011-08-07 11:14:32 -04:00
|
|
|
static int do_wait(volatile int *addr, int val,
|
|
|
|
clockid_t clk, const struct timespec *at, int priv)
|
2011-02-12 00:22:29 -05:00
|
|
|
{
|
2011-08-07 11:14:32 -04:00
|
|
|
int r;
|
2011-08-02 21:11:36 -04:00
|
|
|
struct timespec to, *top=0;
|
|
|
|
|
2011-08-07 11:14:32 -04:00
|
|
|
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;
|
2011-08-02 21:11:36 -04:00
|
|
|
}
|
2011-08-07 11:14:32 -04:00
|
|
|
|
|
|
|
r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT, val, top);
|
2011-08-02 21:11:36 -04:00
|
|
|
if (r == EINTR || r == EINVAL || r == ETIMEDOUT) return r;
|
2011-03-16 11:35:46 -04:00
|
|
|
return 0;
|
2011-02-12 00:22:29 -05:00
|
|
|
}
|
2011-08-02 21:11:36 -04:00
|
|
|
|
2011-08-07 11:14:32 -04:00
|
|
|
int __timedwait(volatile int *addr, int val,
|
|
|
|
clockid_t clk, const struct timespec *at,
|
|
|
|
void (*cleanup)(void *), void *arg, int priv)
|
2011-08-02 21:11:36 -04:00
|
|
|
{
|
2011-08-07 11:14:32 -04:00
|
|
|
int r, cs;
|
|
|
|
|
|
|
|
if (!cleanup) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
|
|
|
|
pthread_cleanup_push(cleanup, arg);
|
|
|
|
|
|
|
|
r = do_wait(addr, val, clk, at, priv);
|
|
|
|
|
|
|
|
pthread_cleanup_pop(0);
|
|
|
|
if (!cleanup) pthread_setcancelstate(cs, 0);
|
|
|
|
|
2011-08-02 21:11:36 -04:00
|
|
|
return r;
|
|
|
|
}
|