Chromium Code Reviews| Index: components/nacl/loader/nonsfi/irt_futex.cc |
| diff --git a/components/nacl/loader/nonsfi/irt_futex.cc b/components/nacl/loader/nonsfi/irt_futex.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fb1baa4bf65ac8e4a233a22f21b9f94deddb7819 |
| --- /dev/null |
| +++ b/components/nacl/loader/nonsfi/irt_futex.cc |
| @@ -0,0 +1,81 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <errno.h> |
| +#include <linux/futex.h> |
| +#include <sys/syscall.h> |
| +#include <sys/time.h> |
| +#include <unistd.h> |
| + |
| +#include "components/nacl/loader/nonsfi/irt_interfaces.h" |
| +#include "native_client/src/trusted/service_runtime/include/sys/time.h" |
| + |
| +namespace nacl { |
| +namespace nonsfi { |
| +namespace { |
| + |
| +// 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.
|
| +// the current time to host's timespec of the relative time between them. |
| +// 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.
|
| +// reltime is not available in this case). |
| +bool NaClAbsTimeToRelTime(const struct nacl_abi_timespec& nacl_abstime, |
| + const struct timespec& now, |
| + struct timespec* reltime) { |
| + 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.
|
| + 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
|
| + (nacl_abstime.tv_sec - now.tv_sec) * kNanoSecondsPerSecond + |
| + (nacl_abstime.tv_nsec - now.tv_nsec); |
| + if (elapsed_nsec < 0) |
| + return false; |
| + |
| + reltime->tv_sec = elapsed_nsec / kNanoSecondsPerSecond; |
| + reltime->tv_nsec = elapsed_nsec % kNanoSecondsPerSecond; |
| + return true; |
| +} |
| + |
| +int IrtFutexWaitAbs(volatile int* addr, int value, |
| + const struct nacl_abi_timespec* abstime) { |
| + struct timespec timeout; |
| + struct timespec* timeout_ptr = NULL; |
| + if (abstime) { |
| + // 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.
|
| + // futex_wait_abs is absolute timeout. So, here we convert it. |
| + struct timespec now; |
| + if (clock_gettime(CLOCK_REALTIME, &now)) |
| + return errno; |
| + |
| + // 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.
|
| + // 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.
|
| + if (!NaClAbsTimeToRelTime(*abstime, now, &timeout)) |
| + return ETIMEDOUT; |
| + timeout_ptr = &timeout; |
| + } |
| + if (syscall(SYS_futex, addr, FUTEX_WAIT_PRIVATE, value, timeout_ptr, 0, 0)) |
| + return errno; |
| + |
| + return 0; |
| +} |
| + |
| +int IrtFutexWake(volatile int* addr, int nwake, int* count) { |
| + int result = syscall(SYS_futex, addr, FUTEX_WAKE_PRIVATE, nwake, 0, 0, 0); |
| + if (result < 0) |
| + return errno; |
| + |
| + *count = result; |
| + return 0; |
| +} |
| + |
| +} // namespace |
| + |
| +// For futex_wait_abs, the argument type should be nacl_abi_timespec. However, |
| +// the definition uses its host type, struct timespec. So, here we need to cast |
| +// it. |
| +const nacl_irt_futex kIrtFutex = { |
| + reinterpret_cast<int(*)(volatile int*, int, const struct timespec*)>( |
| + IrtFutexWaitAbs), |
| + IrtFutexWake, |
| +}; |
| + |
| +} // namespace nonsfi |
| +} // namespace nacl |