aboutsummaryrefslogtreecommitdiff
path: root/libc/bionic/pthread_mutex.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Stop using the __ANDROID_API_x__ constants.Elliott Hughes2019-12-201-1/+1
| | | | | | | | | | | Historically we've made a few mistakes where they haven't matched the right number. And most non-Googlers are much more familiar with the numbers, so it seems to make sense to rely more on them. Especially in header files, which we actually expect real people to have to read from time to time. Test: treehugger Change-Id: I0d4a97454ee108de1d32f21df285315c5488d886
* Implement new clock wait functionsTom Cherry2019-07-151-0/+18
| | | | | | | | | | | | pthread_cond_clockwait pthread_mutex_clocklock pthread_rwlock_clockrdlock pthread_rwlock_clockwrlock sem_clockwait Bug: 35756266 Test: new unit tests Change-Id: I71bd25eeec6476134b368d5bdf2f729d0bba595e
* Merge "PIMutexUnlock: load owner_tid in non-common case"Ryan Prichard2019-04-221-0/+2
|\
| * PIMutexUnlock: load owner_tid in non-common caseRyan Prichard2019-04-191-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | For a recursive or errorcheck PI mutex, the old_owner variable wasn't being initialized. As a result, unlocking a doubly-locked recursive mutex owned by another thread decremented the mutex counter. Instead, the unlock call should fail with EPERM. Bug: http://b/130841532 Test: bionic-unit-tests Test: bionic-unit-tests-glibc --gtest_filter='pthread.pthread_mutex_lock*' Change-Id: I37adb094cb2ce8d51df7b4f48e8d6bc144436418
* | Comment about lock destruction and unlockingRyan Prichard2019-04-191-0/+9
|/ | | | | | | | | | Add a couple of comments explaining that a mutex can be freed during the unlock call, immediately after the unlock's atomic exchange call but before its futex wakeup call. Bug: http://b/129744706 Test: bionic unit tests Change-Id: I2d290ebde880f46866098d022720896039e7022e
* Make android_get_application_target_sdk_version available to the NDK.Elliott Hughes2018-11-151-2/+1
| | | | | | | | | | | Also move this and android_get_device_api_level into <android/api-level.h> so that they're always available. This involves cleaning up <sys/cdefs.h> slightly. Bug: N/A Test: builds Change-Id: I25435c55f3549cd0d827a7581bee75ea8228028b
* Modernize codebase by replacing NULL with nullptrYi Kong2018-08-021-3/+3
| | | | | | | | Fixes -Wzero-as-null-pointer-constant warning. Test: m Bug: 68236239 Change-Id: I5b4123bc6709641315120a191e36cc57541349b2
* Add _monotonic_np versions of timed wait functionsTom Cherry2018-03-201-6/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | As a follow up to Ibba98f5d88be1c306d14e9b9366302ecbef6d534, where we added a work around to convert the CLOCK_REALTIME timeouts to CLOCK_MONOTONIC for pthread and semaphore timed wait functions, we're introducing a set of _monotonic_np versions of each of these functions that wait on CLOCK_MONOTONIC directly. The primary motivation here is that while the above work around helps for 3rd party code, it creates a dilemma when implementing new code that would use these functions: either one implements code with these functions knowing there is a race condition possible or one avoids these functions and reinvent their own waiting/signaling mechanisms. Neither are satisfactory, so we create a third option to use these Android specific _monotonic_np functions that completely remove the race condition while keeping the rest of the interface. Specifically this adds the below functions: pthread_mutex_timedlock_monotonic_np() pthread_cond_timedwait_monotonic_np() pthread_rwlock_timedrdlock_monotonic_np() pthread_rwlock_timedwrlock_monotonic_np() sem_timedwait_monotonic_np() Note that pthread_cond_timedwait_monotonic_np() previously existed and was removed since it's possible to initialize a condition variable to use CLOCK_MONOTONIC. It is added back for a mix of reasons, 1) Symmetry with the rest of the functions we're adding 2) libc++ cannot easily take advantage of the new initializer, but will be able to use this function in order to wait on std::steady_clock 3) Frankly, it's a better API to specify the clock in the waiter function than to specify the clock when the condition variable is initialized. Bug: 73951740 Test: new unit tests Change-Id: I23aa5c204e36a194237d41e064c5c8ccaa4204e3
* Check using destroyed mutexes.Yabin Cui2018-03-151-14/+40
| | | | | | | | | | | | | | For apps built for Android < P, return EBUSY. For apps built for Android >= P, abort. This is to keep old apps work, and help debugging apps built for >= P. Bug: http://b/74632097 Test: run bionic-unit-tests. Test: run bionic-benchmark. Change-Id: I5271565a1a6ad12678f85d558a7f862a2b7aab4b
* Avoid abort when calling pthread_mutex_destroy more than once.Yabin Cui2018-02-021-0/+5
| | | | | | Bug: http://b/72878088 Test: run bionic-unit-tests. Change-Id: I0c3a6c5a625d187d5f32ec8c821cfdd5e807a671
* Support priority inheritance mutex in 32-bit programs.Yabin Cui2018-02-011-42/+163
| | | | | | | | | | | Add fast path calling PIMutexTryLock() in pthread_mutex_lock. Add trace for pi mutex waiting. Bug: http://b/29177606 Test: run bionic-unit-tests. Test: run bionic-benchmarks. Change-Id: I30b6436692d5ea6b63ca9905df745edb843b5528
* Support priority inheritance mutex in 64bit programs.Yabin Cui2018-01-241-58/+248
| | | | | | | Bug: http://b/29177606 Test: run bionic-unit-tests on walleye. Test: run bionic-unit-tests-glibc on host. Change-Id: Iac349284aa73515f384e7509445f87434757f59e
* Use clang's nullability instead of nonnull.Dan Albert2016-05-051-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | http://clang.llvm.org/docs/AttributeReference.html#nonnull _Nonnull is similar to the nonnull attribute in that it will instruct compilers to warn the user if it can prove that a null argument is being passed. Unlike the nonnull attribute, this annotation indicated that a value *should not* be null, not that it *cannot* be null, or even that the behavior is undefined. The important distinction is that the optimizer will perform surprising optimizations like the following: void foo(void*) __attribute__(nonnull, 1); int bar(int* p) { foo(p); // The following null check will be elided because nonnull // attribute means that, since we call foo with p, p can be // assumed to not be null. Thus this will crash if we are called // with a null pointer. if (src != NULL) { return *p; } return 0; } int main() { return bar(NULL); } Note that by doing this we are no longer attaching any sort of attribute for GCC (GCC doesn't support attaching nonnull directly to a parameter, only to the function and naming the arguments positionally). This means we won't be getting a warning for this case from GCC any more. People that listen to warnings tend to use clang anyway, and we're quickly moving toward that as the default, so this seems to be an acceptable tradeoff. Change-Id: Ie05fe7cec2f19a082c1defb303f82bcf9241b88d
* sem_timedwait with a null timeout doesn't mean "forever".Elliott Hughes2015-12-161-2/+2
| | | | | | | | | It actually means "crash immediately". Well, it's an error. And callers are much more likely to realize their mistake if we crash immediately rather than return EINVAL. Historically, glibc has crashed and bionic -- before the recent changes -- returned EINVAL, so this is a behavior change. Change-Id: I0c2373a6703b20b8a97aacc1e66368a5885e8c51
* Merge "Use FUTEX_WAIT_BITSET to avoid converting timeouts."Yabin Cui2015-11-211-36/+28
|\
| * Use FUTEX_WAIT_BITSET to avoid converting timeouts.Yabin Cui2015-11-191-36/+28
| | | | | | | | | | | | | | | | Add unittests for pthread APIs with timeout parameter. Bug: 17569991 Change-Id: I6b3b9b2feae03680654cd64c3112ce7644632c87
* | Avoid tsan warning about pthread_mutex_destroy.Yabin Cui2015-11-171-9/+16
|/ | | | | | | | If calling pthread_mutex_trylock from pthread_mutex_destroy, tsan warns about an attempt to destroy a locked mutex. Bug: 25392375 Change-Id: I5feee20e7a0d0915adad24da874ec1ccce241381
* Allow NULL in pthread_mutex_lock/unlock.Christopher Ferris2015-06-091-0/+12
| | | | | | | | | | | The pthread_mutex_lock and pthread_mutex_unlock were allowed to fail silently on L 32 bit devices when passed a NULL. We changed this to a crash on 32 bit devices, but there are still games that make these calls and are not likely to be updated. Therefore, once again allow NULL to be passed in on 32 bit devices. Bug: 19995172 Change-Id: If7e8860075ecd63c0064d80f64e226fad7bd3c26
* Fix bug for recursive/errorcheck mutex on 32-bit devices.Yabin Cui2015-04-031-1/+25
| | | | | Bug: 19216648 Change-Id: I3b43b2d18d25b9bde352da1e35f9568133dec7cf
* Refactor pthread_mutex to support 32-bit owner_tid on 64-bit devices.Yabin Cui2015-04-011-155/+141
| | | | | Bug: 19216648 Change-Id: I765ecacc9036659c766f5d1f6600e1a65364199b
* Hide internal of pthread_mutex_t.Yabin Cui2015-03-251-113/+116
| | | | | Bug: 19249079 Change-Id: Iffb79c8d861b698d474f212dc80c638fc2cf1620
* Fix build: pthread_mutex/pthread_detach.Yabin Cui2015-03-171-1/+1
| | | | Change-Id: I9c7b6297d3bf3ab8004d05d44cc4c95159315c9e
* Remove duplication in pthread_mutex.cpp.Yabin Cui2015-03-121-202/+109
| | | | | | | | Also add unit tests about thread woken up by pthread_mutex_unlock. Bug: 19216648 Change-Id: I8bde8105b00186c52a2f41d92458ae4a5eb90426
* Add test about pthread_mutex_t owner tid limit.Yabin Cui2015-02-031-24/+3
| | | | | Bug: 19216648 Change-Id: I7b12955bdcad31c13bf8ec2740ff88ba15223ec0
* Switch pthread_mutex_t from bionic atomics to <stdatomic.h>.Yabin Cui2015-02-021-316/+294
| | | | | Bug: 17574456 Change-Id: I5ce3d3dc07e804e9ce55c42920f47531b56e04de
* POSIX says pthread_mutex_trylock returns EBUSY, not EDEADLK.Elliott Hughes2014-10-241-11/+12
| | | | | | Found by unit test. Change-Id: Iffbd2f04213616927fbd7b5419460031f7a078e9
* Revert "Work around a bug in Immersion's libImmEmulatorJ.so."Elliott Hughes2014-10-071-12/+0
| | | | | | This reverts commit 7d3f553f989f830976efa92ddc3c84661d4d42aa. Change-Id: I8909b6aa1d97e9a61dbe95a2d91b9fbe336b58f0
* Work around a bug in Immersion's libImmEmulatorJ.so.Elliott Hughes2014-10-071-0/+12
| | | | | | | | This library calls pthread_mutex_lock and pthread_mutex_unlock with a NULL pthread_mutex_t*. This gives them (and their users) one release to fix things. Bug: 17443936 Change-Id: I3b63c9a3dd63db0833f21073e323b3236a13b47a
* Add semaphore tests, fix sem_destroy.Elliott Hughes2014-09-191-6/+8
| | | | | Bug: https://code.google.com/p/android/issues/detail?id=76088 Change-Id: I4a0561b23e90312384d40a1c804ca64ee98f4066
* Added a bionic systrace class and tracing to pthread_mutex.cpp.Brigid Smith2014-07-281-0/+12
| | | | | | | | | | bionic_systrace.h contains an implementation of tracing that can be used with systrace.py and its associated viewer. pthread_mutex now uses this tracing to track pthread_mutex contention, which can be enabled by using the "bionic" command line option to systrace. Bug: 15116468 Change-Id: I30ed5b377c91ca4c36568a0e647ddf95d4e4a61a
* Minor style cleanup of some code I had to look at.Elliott Hughes2014-05-281-11/+6
| | | | | | (It turns out that this is the only place we're saying __inline in C++.) Change-Id: I8095e67a385087817c47caab9a621f82f8e0cfc8
* Updated ambiguous comment in pthread_mutex_lock.Brigid Smith2014-05-281-1/+1
| | | | | | The comment used the phrase "normal case" when it more specifically refers to the "recursive case," so I changed it to that. Change-Id: I8335cce4dee933c6a463aee653b28bd986b5b5e4
* Remove unnecessary #includes of <sys/atomics.h>.Elliott Hughes2014-05-221-1/+0
| | | | | Bug: 14903517 Change-Id: I7b5bcebe58774a441da986cc02376dd88e00ea0e
* Remove the broken pthread deadlock prediction.Elliott Hughes2014-05-141-60/+15
| | | | | | | This hasn't built in over one release cycle and no one even noticed. art does this the right way and other projects should do the same. Change-Id: I7d1fb84c4080e008f329ee73e209ce85a36e6d55
* Remove unnecessary #includes.Elliott Hughes2014-04-101-1/+0
| | | | Change-Id: Ie7e0c9ea03f35517c7dcf09fc808c12e55262bc1
* Remove dead NULL checks from pthread code.Elliott Hughes2014-03-041-34/+17
| | | | | | | | | | | | | GCC is removing these checks anyway because it knows the arguments must be non-null, so leaving this code around is just confusing. We know from experience that people were shipping code with locking bugs because they weren't checking for error returns. Failing hard like glibc does seems the better choice. (And it's what the checked in code was already doing; this patch doesn't change that. It just makes it more obvious that that's what's going on.) Change-Id: I167c6d7c0a296822baf0cb9b43b97821eba7ab35
* Implement POSIX pthread_mutex_timedlock.Elliott Hughes2014-03-041-115/+108
| | | | | | | This replaces the non-standard pthread_mutex_lock_timeout_np, which we have to keep around on LP32 for binary compatibility. Change-Id: I098dc7cd38369f0c1bec1fac35687fbd27392e00
* Small style cleanup.Elliott Hughes2013-12-181-1/+1
| | | | Change-Id: Ib45a4a2296232968296f9bd7cc3592ba46fd412d
* Merge "Properly detect timeout in pthread_mutex_lock_timeout_np_impl"Elliott Hughes2013-12-181-1/+1
|\
| * Properly detect timeout in pthread_mutex_lock_timeout_np_implKen Mixter2013-12-171-1/+1
| | | | | | | | | | | | | | Previously we were checking against a positive errno which would not be returned from a system call. Change-Id: I8e3a36f6fbf5ccc2191a152a1def37e2d6f93124
* | Remove harmful attempts to be helpful in pthread_mutex functions.Elliott Hughes2013-12-111-21/+4
|/ | | | | | | | | | | Most callers won't check for EINVAL, so it's best to fail early. GCC takes the nonnull attribute as a guarantee that an argument won't be NULL, so these hacks were already ineffective, which is how we found that at least one commercial game was using NULL as if it's a mutex, but actually getting no-op behavior. Bug: 11971278 Change-Id: I89646e043d931778805a8b692e07a34d076ee6bf
* Clean up forking and cloning.Elliott Hughes2013-11-191-1/+0
| | | | | | | | | The kernel now maintains the pthread_internal_t::tid field for us, and __clone was only used in one place so let's inline it so we don't have to leave such a dangerous function lying around. Also rename files to match their content and remove some useless #includes. Change-Id: I24299fb4a940e394de75f864ee36fdabbd9438f9
* Move the pthread debugging flags to the right place.Elliott Hughes2013-10-311-8/+0
| | | | Change-Id: Ie805bd837d1f72cdf1818e056c0baeb0857e4e84
* <pthread.h> fixes and pthread cleanup.Elliott Hughes2013-10-311-0/+844
<pthread.h> was missing nonnull attributes, noreturn on pthread_exit, and had incorrect cv qualifiers for several standard functions. I've also marked the non-standard stuff (where I count glibc rather than POSIX as "standard") so we can revisit this cruft for LP64 and try to ensure we're compatible with glibc. I've also broken out the pthread_cond* functions into a new file. I've made the remaining pthread files (plus ptrace) part of the bionic code and fixed all the warnings. I've added a few more smoke tests for chunks of untested pthread functionality. We no longer need the libc_static_common_src_files hack for any of the pthread implementation because we long since stripped out the rest of the armv5 support, and this hack was just to ensure that __get_tls in libc.a went via the kernel if necessary. This patch also finishes the job of breaking up the pthread.c monolith, and adds a handful of new tests. Change-Id: Idc0ae7f5d8aa65989598acd4c01a874fe21582c7