| Tom Cherry | ac49ced | 2017-08-17 13:18:52 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2017 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include "private/bionic_futex.h" | 
|  | 30 |  | 
|  | 31 | #include <time.h> | 
|  | 32 |  | 
|  | 33 | #include "private/bionic_time_conversions.h" | 
|  | 34 |  | 
|  | 35 | int __futex_wait_ex(volatile void* ftx, bool shared, int value, bool use_realtime_clock, | 
|  | 36 | const timespec* abs_timeout) { | 
|  | 37 | const timespec* futex_abs_timeout = abs_timeout; | 
|  | 38 | // pthread's and semaphore's default behavior is to use CLOCK_REALTIME, however this behavior is | 
|  | 39 | // essentially never intended, as that clock is prone to change discontinuously. | 
|  | 40 | // | 
|  | 41 | // What users really intend is to use CLOCK_MONOTONIC, however only pthread_cond_timedwait() | 
|  | 42 | // provides this as an option and even there, a large amount of existing code does not opt into | 
|  | 43 | // CLOCK_MONOTONIC. | 
|  | 44 | // | 
|  | 45 | // We have seen numerous bugs directly attributable to this difference.  Therefore, we provide | 
|  | 46 | // this general workaround to always use CLOCK_MONOTONIC for waiting, regardless of what the input | 
|  | 47 | // timespec is. | 
|  | 48 | timespec converted_monotonic_abs_timeout; | 
|  | 49 | if (abs_timeout && use_realtime_clock) { | 
|  | 50 | monotonic_time_from_realtime_time(converted_monotonic_abs_timeout, *abs_timeout); | 
|  | 51 | if (converted_monotonic_abs_timeout.tv_sec < 0) { | 
|  | 52 | return -ETIMEDOUT; | 
|  | 53 | } | 
|  | 54 | futex_abs_timeout = &converted_monotonic_abs_timeout; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | return __futex(ftx, (shared ? FUTEX_WAIT_BITSET : FUTEX_WAIT_BITSET_PRIVATE), value, | 
|  | 58 | futex_abs_timeout, FUTEX_BITSET_MATCH_ANY); | 
|  | 59 | } |