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 static inline __always_inline int FutexWithTimeout(volatile void* ftx, int op, int value, 36 bool use_realtime_clock, 37 const timespec* abs_timeout, int bitset) { 38 const timespec* futex_abs_timeout = abs_timeout; 39 // pthread's and semaphore's default behavior is to use CLOCK_REALTIME, however this behavior is 40 // essentially never intended, as that clock is prone to change discontinuously. 41 // 42 // What users really intend is to use CLOCK_MONOTONIC, however only pthread_cond_timedwait() 43 // provides this as an option and even there, a large amount of existing code does not opt into 44 // CLOCK_MONOTONIC. 45 // 46 // We have seen numerous bugs directly attributable to this difference. Therefore, we provide 47 // this general workaround to always use CLOCK_MONOTONIC for waiting, regardless of what the input 48 // timespec is. 49 timespec converted_monotonic_abs_timeout; 50 if (abs_timeout && use_realtime_clock) { 51 monotonic_time_from_realtime_time(converted_monotonic_abs_timeout, *abs_timeout); 52 if (converted_monotonic_abs_timeout.tv_sec < 0) { 53 return -ETIMEDOUT; 54 } 55 futex_abs_timeout = &converted_monotonic_abs_timeout; 56 } 57 58 return __futex(ftx, op, value, futex_abs_timeout, bitset); 59 } 60 61 int __futex_wait_ex(volatile void* ftx, bool shared, int value, bool use_realtime_clock, 62 const timespec* abs_timeout) { 63 return FutexWithTimeout(ftx, (shared ? FUTEX_WAIT_BITSET : FUTEX_WAIT_BITSET_PRIVATE), value, 64 use_realtime_clock, abs_timeout, FUTEX_BITSET_MATCH_ANY); 65 } 66 67 int __futex_pi_lock_ex(volatile void* ftx, bool shared, bool use_realtime_clock, 68 const timespec* abs_timeout) { 69 return FutexWithTimeout(ftx, (shared ? FUTEX_LOCK_PI : FUTEX_LOCK_PI_PRIVATE), 0, 70 use_realtime_clock, abs_timeout, 0); 71 } 72