1570af302Sopenharmony_ci#include <time.h>
2570af302Sopenharmony_ci#include <limits.h>
3570af302Sopenharmony_ci#include "pthread_impl.h"
4570af302Sopenharmony_ci
5570af302Sopenharmony_ci#define IS32BIT(x) !((x)+0x80000000ULL>>32)
6570af302Sopenharmony_ci
7570af302Sopenharmony_ciint timer_settime(timer_t t, int flags, const struct itimerspec *restrict val, struct itimerspec *restrict old)
8570af302Sopenharmony_ci{
9570af302Sopenharmony_ci	if ((intptr_t)t < 0) {
10570af302Sopenharmony_ci		pthread_t td = (void *)((uintptr_t)t << 1);
11570af302Sopenharmony_ci		t = (void *)(uintptr_t)(td->timer_id & INT_MAX);
12570af302Sopenharmony_ci	}
13570af302Sopenharmony_ci#ifdef SYS_timer_settime64
14570af302Sopenharmony_ci	time_t is = val->it_interval.tv_sec, vs = val->it_value.tv_sec;
15570af302Sopenharmony_ci	long ins = val->it_interval.tv_nsec, vns = val->it_value.tv_nsec;
16570af302Sopenharmony_ci	int r = -ENOSYS;
17570af302Sopenharmony_ci	if (SYS_timer_settime == SYS_timer_settime64
18570af302Sopenharmony_ci	    || !IS32BIT(is) || !IS32BIT(vs) || (sizeof(time_t)>4 && old))
19570af302Sopenharmony_ci		r = __syscall(SYS_timer_settime64, t, flags,
20570af302Sopenharmony_ci			((long long[]){is, ins, vs, vns}), old);
21570af302Sopenharmony_ci	if (SYS_timer_settime == SYS_timer_settime64 || r!=-ENOSYS)
22570af302Sopenharmony_ci		return __syscall_ret(r);
23570af302Sopenharmony_ci	if (!IS32BIT(is) || !IS32BIT(vs))
24570af302Sopenharmony_ci		return __syscall_ret(-ENOTSUP);
25570af302Sopenharmony_ci	long old32[4];
26570af302Sopenharmony_ci	r = __syscall(SYS_timer_settime, t, flags,
27570af302Sopenharmony_ci		((long[]){is, ins, vs, vns}), old32);
28570af302Sopenharmony_ci	if (!r && old) {
29570af302Sopenharmony_ci		old->it_interval.tv_sec = old32[0];
30570af302Sopenharmony_ci		old->it_interval.tv_nsec = old32[1];
31570af302Sopenharmony_ci		old->it_value.tv_sec = old32[2];
32570af302Sopenharmony_ci		old->it_value.tv_nsec = old32[3];
33570af302Sopenharmony_ci	}
34570af302Sopenharmony_ci	return __syscall_ret(r);
35570af302Sopenharmony_ci#endif
36570af302Sopenharmony_ci	return syscall(SYS_timer_settime, t, flags, val, old);
37570af302Sopenharmony_ci}
38