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 | |
Mark Seaborn
2014/01/15 19:27:54
Nit: capitalise as "NaCl"
hidehiko
2014/01/16 06:52:23
Done.
| |
19 // the current time to host's timespec of the relative time between them. | |
20 // Returns true if the relative time is non-negative. Otherwise false (and | |
Mark Seaborn
2014/01/15 19:27:54
Maybe just have the caller check whether the resul
hidehiko
2014/01/16 06:52:23
Done.
| |
21 // reltime is not available in this case). | |
22 bool NaClAbsTimeToRelTime(const struct nacl_abi_timespec& nacl_abstime, | |
23 const struct timespec& now, | |
24 struct timespec* reltime) { | |
25 const int64 kNanoSecondsPerSecond = 1000000000; | |
Mark Seaborn
2014/01/15 19:27:54
Maybe use "int64_t" rather than "int64"? In Linux
hidehiko
2014/01/16 06:52:23
Acknowledged.
| |
26 int64 elapsed_nsec = | |
Mark Seaborn
2014/01/15 19:27:54
It's better to do what unsandboxed_irt.c does:
hidehiko
2014/01/16 06:52:23
Done, assuming both input tv_nsec are non-negative
Mark Seaborn
2014/01/16 19:39:47
Right. It's required that 0 <= tv_nsec < 10000000
| |
27 (nacl_abstime.tv_sec - now.tv_sec) * kNanoSecondsPerSecond + | |
28 (nacl_abstime.tv_nsec - now.tv_nsec); | |
29 if (elapsed_nsec < 0) | |
30 return false; | |
31 | |
32 reltime->tv_sec = elapsed_nsec / kNanoSecondsPerSecond; | |
33 reltime->tv_nsec = elapsed_nsec % kNanoSecondsPerSecond; | |
34 return true; | |
35 } | |
36 | |
37 int IrtFutexWaitAbs(volatile int* addr, int value, | |
38 const struct nacl_abi_timespec* abstime) { | |
39 struct timespec timeout; | |
40 struct timespec* timeout_ptr = NULL; | |
41 if (abstime) { | |
42 // futex syscall takes relative timeout, but the ABI for irt's | |
Mark Seaborn
2014/01/15 19:27:54
Nit: capitalise as "IRT"
hidehiko
2014/01/16 06:52:23
Done.
| |
43 // futex_wait_abs is absolute timeout. So, here we convert it. | |
44 struct timespec now; | |
45 if (clock_gettime(CLOCK_REALTIME, &now)) | |
46 return errno; | |
47 | |
48 // Linux's FUTEX_WAIT returns EINVAL for negative timeout, but it valid | |
Mark Seaborn
2014/01/15 19:27:54
How about: "but an absolute time that is in the pa
hidehiko
2014/01/16 06:52:23
Done.
| |
49 // on irt_futex_wait_abs, and a caller expects ETIMEOUT. | |
Mark Seaborn
2014/01/15 19:27:54
ETIMEDOUT
hidehiko
2014/01/16 06:52:23
Done.
| |
50 if (!NaClAbsTimeToRelTime(*abstime, now, &timeout)) | |
51 return ETIMEDOUT; | |
52 timeout_ptr = &timeout; | |
53 } | |
54 if (syscall(SYS_futex, addr, FUTEX_WAIT_PRIVATE, value, timeout_ptr, 0, 0)) | |
55 return errno; | |
56 | |
57 return 0; | |
58 } | |
59 | |
60 int IrtFutexWake(volatile int* addr, int nwake, int* count) { | |
61 int result = syscall(SYS_futex, addr, FUTEX_WAKE_PRIVATE, nwake, 0, 0, 0); | |
62 if (result < 0) | |
63 return errno; | |
64 | |
65 *count = result; | |
66 return 0; | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 // For futex_wait_abs, the argument type should be nacl_abi_timespec. However, | |
72 // the definition uses its host type, struct timespec. So, here we need to cast | |
73 // it. | |
74 const nacl_irt_futex kIrtFutex = { | |
75 reinterpret_cast<int(*)(volatile int*, int, const struct timespec*)>( | |
76 IrtFutexWaitAbs), | |
77 IrtFutexWake, | |
78 }; | |
79 | |
80 } // namespace nonsfi | |
81 } // namespace nacl | |
OLD | NEW |