OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <errno.h> |
| 6 #include <linux/futex.h> |
| 7 #include <sys/syscall.h> |
| 8 #include <sys/time.h> |
| 9 #include <unistd.h> |
| 10 |
| 11 #include "components/nacl/loader/nonsfi/irt_interfaces.h" |
| 12 #include "native_client/src/trusted/service_runtime/include/sys/time.h" |
| 13 |
| 14 namespace nacl { |
| 15 namespace nonsfi { |
| 16 namespace { |
| 17 |
| 18 // Converts a pair of NaCl's timespec of absolute time and host's timespec of |
| 19 // the current time to host's timespec of the relative time between them. |
| 20 // Note that assuming tv_nsec for the both input structs are non-negative, |
| 21 // the returned reltime's tv_nsec is also always non-negative. |
| 22 // (I.e., for -0.1 sec, {tv_sec: -1, tv_nsec: 900000000} will be returned). |
| 23 void NaClAbsTimeToRelTime(const struct nacl_abi_timespec& nacl_abstime, |
| 24 const struct timespec& now, |
| 25 struct timespec* reltime) { |
| 26 reltime->tv_sec = nacl_abstime.tv_sec - now.tv_sec; |
| 27 reltime->tv_nsec = nacl_abstime.tv_nsec - now.tv_nsec; |
| 28 if (reltime->tv_nsec < 0) { |
| 29 reltime->tv_sec -= 1; |
| 30 reltime->tv_nsec += 1000000000; |
| 31 } |
| 32 } |
| 33 |
| 34 int IrtFutexWaitAbs(volatile int* addr, int value, |
| 35 const struct nacl_abi_timespec* abstime) { |
| 36 struct timespec timeout; |
| 37 struct timespec* timeout_ptr = NULL; |
| 38 if (abstime) { |
| 39 // futex syscall takes relative timeout, but the ABI for IRT's |
| 40 // futex_wait_abs is absolute timeout. So, here we convert it. |
| 41 struct timespec now; |
| 42 if (clock_gettime(CLOCK_REALTIME, &now)) |
| 43 return errno; |
| 44 NaClAbsTimeToRelTime(*abstime, now, &timeout); |
| 45 |
| 46 // Linux's FUTEX_WAIT returns EINVAL for negative timeout, but an absolute |
| 47 // time that is in the past is a valid argument to irt_futex_wait_abs(), |
| 48 // and a caller expects ETIMEDOUT. |
| 49 // Here check only tv_sec since we assume time_t is signed. See also |
| 50 // the comment for NaClAbsTimeToRelTime. |
| 51 if (timeout.tv_sec < 0) |
| 52 return ETIMEDOUT; |
| 53 timeout_ptr = &timeout; |
| 54 } |
| 55 if (syscall(SYS_futex, addr, FUTEX_WAIT_PRIVATE, value, timeout_ptr, 0, 0)) |
| 56 return errno; |
| 57 |
| 58 return 0; |
| 59 } |
| 60 |
| 61 int IrtFutexWake(volatile int* addr, int nwake, int* count) { |
| 62 int result = syscall(SYS_futex, addr, FUTEX_WAKE_PRIVATE, nwake, 0, 0, 0); |
| 63 if (result < 0) |
| 64 return errno; |
| 65 |
| 66 *count = result; |
| 67 return 0; |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 |
| 72 // For futex_wait_abs, the argument type should be nacl_abi_timespec. However, |
| 73 // the definition uses its host type, struct timespec. So, here we need to cast |
| 74 // it. |
| 75 const nacl_irt_futex kIrtFutex = { |
| 76 reinterpret_cast<int(*)(volatile int*, int, const struct timespec*)>( |
| 77 IrtFutexWaitAbs), |
| 78 IrtFutexWake, |
| 79 }; |
| 80 |
| 81 } // namespace nonsfi |
| 82 } // namespace nacl |
OLD | NEW |