From: Delyan Kratunov Date: Fri, 29 May 2015 00:30:59 +0000 (-0700) Subject: Make detail/Futex.cpp bionic-safe X-Git-Tag: v0.42.0~16 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=7a4c7813e2ea74b19fb2872b4f43a6ee304e1d49;p=folly.git Make detail/Futex.cpp bionic-safe 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 --- diff --git a/folly/detail/Futex.cpp b/folly/detail/Futex.cpp index 7c1d2e60..f3d7bb04 100644 --- a/folly/detail/Futex.cpp +++ b/folly/detail/Futex.cpp @@ -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 struct timespec timeSpecFromTimePoint(time_point 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(duration); - auto nanos = duration_cast(duration - secs); + + // timespec-safe seconds and nanoseconds; + // chrono::{nano,}seconds are `long long int` + // whereas timespec uses smaller types + using time_t_seconds = duration; + using long_nanos = duration; + + auto secs = duration_cast(epoch); + auto nanos = duration_cast(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 */