2 * Copyright 2014 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
11 #include "native_client/src/nonsfi/linux/abi_conversion.h"
12 #include "native_client/src/nonsfi/linux/linux_syscall_defines.h"
13 #include "native_client/src/nonsfi/linux/linux_syscall_wrappers.h"
14 #include "native_client/src/nonsfi/linux/linux_syscalls.h"
15 #include "native_client/src/trusted/service_runtime/include/sys/time.h"
16 #include "native_client/src/untrusted/irt/irt_interfaces.h"
17 #include "native_client/src/untrusted/pthread/pthread_internal.h"
20 * Converts a pair of timespec of absolute time and host's timespec of
21 * the current time to host's timespec of the relative time between them.
22 * Note that assuming tv_nsec for the both input structs are non-negative,
23 * the returned reltime's tv_nsec is also always non-negative.
24 * (I.e., for -0.1 sec, {tv_sec: -1, tv_nsec: 900000000} will be returned).
26 static void abs_time_to_linux_rel_time(const struct timespec *nacl_abstime,
27 const struct timespec *now,
28 struct linux_abi_timespec *reltime) {
29 reltime->tv_sec = nacl_abstime->tv_sec - now->tv_sec;
30 reltime->tv_nsec = nacl_abstime->tv_nsec - now->tv_nsec;
31 if (reltime->tv_nsec < 0) {
33 reltime->tv_nsec += 1000000000;
37 static int nacl_irt_futex_wait_abs(volatile int *addr, int value,
38 const struct timespec *abstime) {
39 struct linux_abi_timespec timeout;
40 struct linux_abi_timespec *timeout_ptr = NULL;
43 * futex syscall takes relative timeout, but the ABI for IRT's
44 * futex_wait_abs is absolute timeout. So, here we convert it.
47 if (clock_gettime(CLOCK_REALTIME, &now) < 0)
49 abs_time_to_linux_rel_time(abstime, &now, &timeout);
52 * Linux's FUTEX_WAIT returns EINVAL for negative timeout, but an absolute
53 * time that is in the past is a valid argument to irt_futex_wait_abs(),
54 * and a caller expects ETIMEDOUT.
55 * Here check only tv_sec since we assume time_t is signed. See also
56 * the comment for abs_time_to_rel_time.
58 if (timeout.tv_sec < 0)
60 timeout_ptr = &timeout;
63 int result = linux_syscall6(__NR_futex, (uintptr_t) addr, FUTEX_WAIT_PRIVATE,
64 value, (uintptr_t) timeout_ptr, 0, 0);
70 static int nacl_irt_futex_wake(volatile int *addr, int nwake, int *count) {
71 int result = linux_syscall6(__NR_futex, (uintptr_t) addr, FUTEX_WAKE_PRIVATE,
79 const struct nacl_irt_futex nacl_irt_futex = {
80 nacl_irt_futex_wait_abs,
84 extern struct nacl_irt_futex __nc_irt_futex
85 __attribute__((alias("nacl_irt_futex")));