Make detail/Futex.cpp bionic-safe
authorDelyan Kratunov <delyank@fb.com>
Fri, 29 May 2015 00:30:59 +0000 (17:30 -0700)
committerNoam Lerner <noamler@fb.com>
Wed, 3 Jun 2015 16:50:55 +0000 (09:50 -0700)
Summary:
The headers that the Android NDK exports are all sorts of screwed up.
In particular, they expose a subset of the `futex.h`, so, even though the
kernel supports newer options, we can't reference them from the NDK headers.

This diff ensures Futex.cpp redefines all the things it uses, if the headers
are lacking.

It also transitions away from `SYS_` to `__NR_` which is apparently the newer
convention.

Lastly, the duration usage is made explicitly `time_t`-safe for use on 32-bit
platforms where `time_t` is `long int` but `std::chrono:seconds` is stored as
`long long int`.

Test Plan:
Existing tests, sample app compiled and ran cleanly on Android as well
(tests not verified on Android due to folly largely being unported to Android yet).

Reviewed By: ngbronson@fb.com

Subscribers: folly-diffs@, yfeldblum, chalfant

FB internal diff: D2069306

Signature: t1:2069306:1431721711:36c77b1afe8dd9259c1050f11a87511dcf7dd25f

folly/detail/Futex.cpp

index 7c1d2e608017af66d52bf1026f37e94339a5390a..f3d7bb0454ab92621b147be77f0188ae937e6a81 100644 (file)
@@ -40,8 +40,24 @@ namespace {
 
 #ifdef __linux__
 
+/// Certain toolchains (like Android's) don't include the full futex API in
+/// their headers even though they support it. Make sure we have our constants
+/// even if the headers don't have them.
+#ifndef FUTEX_WAIT_BITSET
+# define FUTEX_WAIT_BITSET 9
+#endif
+#ifndef FUTEX_WAKE_BITSET
+# define FUTEX_WAKE_BITSET 10
+#endif
+#ifndef FUTEX_PRIVATE_FLAG
+# define FUTEX_PRIVATE_FLAG 128
+#endif
+#ifndef FUTEX_CLOCK_REALTIME
+# define FUTEX_CLOCK_REALTIME 256
+#endif
+
 int nativeFutexWake(void* addr, int count, uint32_t wakeMask) {
-  int rv = syscall(SYS_futex,
+  int rv = syscall(__NR_futex,
                    addr, /* addr1 */
                    FUTEX_WAKE_BITSET | FUTEX_PRIVATE_FLAG, /* op */
                    count, /* val */
@@ -58,13 +74,20 @@ template <class Clock>
 struct timespec
 timeSpecFromTimePoint(time_point<Clock> absTime)
 {
-  auto duration = absTime.time_since_epoch();
-  if (duration.count() < 0) {
+  auto epoch = absTime.time_since_epoch();
+  if (epoch.count() < 0) {
     // kernel timespec_valid requires non-negative seconds and nanos in [0,1G)
-    duration = Clock::duration::zero();
+    epoch = Clock::duration::zero();
   }
-  auto secs = duration_cast<seconds>(duration);
-  auto nanos = duration_cast<nanoseconds>(duration - secs);
+
+  // timespec-safe seconds and nanoseconds;
+  // chrono::{nano,}seconds are `long long int`
+  // whereas timespec uses smaller types
+  using time_t_seconds = duration<std::time_t, seconds::period>;
+  using long_nanos = duration<long int, nanoseconds::period>;
+
+  auto secs = duration_cast<time_t_seconds>(epoch);
+  auto nanos = duration_cast<long_nanos>(epoch - secs);
   struct timespec result = { secs.count(), nanos.count() };
   return result;
 }
@@ -91,7 +114,7 @@ FutexResult nativeFutexWaitImpl(void* addr,
 
   // Unlike FUTEX_WAIT, FUTEX_WAIT_BITSET requires an absolute timeout
   // value - http://locklessinc.com/articles/futex_cheat_sheet/
-  int rv = syscall(SYS_futex,
+  int rv = syscall(__NR_futex,
                    addr, /* addr1 */
                    op, /* op */
                    expected, /* val */