Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _LIBS_UTILS_MUTEX_H |
| 18 | #define _LIBS_UTILS_MUTEX_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <time.h> |
| 23 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 24 | #if !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 25 | # include <pthread.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <utils/Errors.h> |
Jesse Hall | 601424a | 2014-12-23 14:00:29 -0800 | [diff] [blame] | 29 | #include <utils/Timers.h> |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 30 | |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 31 | // Enable thread safety attributes only with clang. |
| 32 | // The attributes can be safely erased when compiling with other compilers. |
| 33 | #if defined(__clang__) && (!defined(SWIG)) |
| 34 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) |
| 35 | #else |
| 36 | #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op |
| 37 | #endif |
| 38 | |
| 39 | #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) |
| 40 | |
| 41 | #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) |
| 42 | |
| 43 | #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) |
| 44 | |
| 45 | #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) |
| 46 | |
| 47 | #define ACQUIRED_BEFORE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) |
| 48 | |
| 49 | #define ACQUIRED_AFTER(...) THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) |
| 50 | |
| 51 | #define REQUIRES(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) |
| 52 | |
| 53 | #define REQUIRES_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) |
| 54 | |
| 55 | #define ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) |
| 56 | |
| 57 | #define ACQUIRE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) |
| 58 | |
| 59 | #define RELEASE(...) THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) |
| 60 | |
| 61 | #define RELEASE_SHARED(...) THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) |
| 62 | |
| 63 | #define TRY_ACQUIRE(...) THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) |
| 64 | |
| 65 | #define TRY_ACQUIRE_SHARED(...) \ |
| 66 | THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) |
| 67 | |
| 68 | #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) |
| 69 | |
| 70 | #define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) |
| 71 | |
| 72 | #define ASSERT_SHARED_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) |
| 73 | |
| 74 | #define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) |
| 75 | |
| 76 | #define NO_THREAD_SAFETY_ANALYSIS THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) |
| 77 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 78 | // --------------------------------------------------------------------------- |
| 79 | namespace android { |
| 80 | // --------------------------------------------------------------------------- |
| 81 | |
| 82 | class Condition; |
| 83 | |
| 84 | /* |
Greg Kaiser | d62698d | 2016-04-05 10:55:13 -0700 | [diff] [blame] | 85 | * NOTE: This class is for code that builds on Win32. Its usage is |
| 86 | * deprecated for code which doesn't build for Win32. New code which |
| 87 | * doesn't build for Win32 should use std::mutex and std::lock_guard instead. |
| 88 | * |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 89 | * Simple mutex class. The implementation is system-dependent. |
| 90 | * |
| 91 | * The mutex must be unlocked by the thread that locked it. They are not |
| 92 | * recursive, i.e. the same thread can't lock it multiple times. |
| 93 | */ |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 94 | class CAPABILITY("mutex") Mutex { |
| 95 | public: |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 96 | enum { |
| 97 | PRIVATE = 0, |
| 98 | SHARED = 1 |
| 99 | }; |
Jesse Hall | 601424a | 2014-12-23 14:00:29 -0800 | [diff] [blame] | 100 | |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 101 | Mutex(); |
| 102 | explicit Mutex(const char* name); |
Yi Kong | c1a1562 | 2018-07-11 17:37:34 -0700 | [diff] [blame] | 103 | explicit Mutex(int type, const char* name = nullptr); |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 104 | ~Mutex(); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 105 | |
| 106 | // lock or unlock the mutex |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 107 | status_t lock() ACQUIRE(); |
| 108 | void unlock() RELEASE(); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 109 | |
| 110 | // lock if possible; returns 0 on success, error otherwise |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 111 | status_t tryLock() TRY_ACQUIRE(true); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 112 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 113 | #if defined(__ANDROID__) |
Andy Hung | 604ba48 | 2016-08-23 18:15:32 -0700 | [diff] [blame] | 114 | // Lock the mutex, but don't wait longer than timeoutNs (relative time). |
Jesse Hall | 601424a | 2014-12-23 14:00:29 -0800 | [diff] [blame] | 115 | // Returns 0 on success, TIMED_OUT for failure due to timeout expiration. |
| 116 | // |
| 117 | // OSX doesn't have pthread_mutex_timedlock() or equivalent. To keep |
| 118 | // capabilities consistent across host OSes, this method is only available |
| 119 | // when building Android binaries. |
Andy Hung | 604ba48 | 2016-08-23 18:15:32 -0700 | [diff] [blame] | 120 | // |
| 121 | // FIXME?: pthread_mutex_timedlock is based on CLOCK_REALTIME, |
| 122 | // which is subject to NTP adjustments, and includes time during suspend, |
| 123 | // so a timeout may occur even though no processes could run. |
| 124 | // Not holding a partial wakelock may lead to a system suspend. |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 125 | status_t timedLock(nsecs_t timeoutNs) TRY_ACQUIRE(true); |
Jesse Hall | 601424a | 2014-12-23 14:00:29 -0800 | [diff] [blame] | 126 | #endif |
| 127 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 128 | // Manages the mutex automatically. It'll be locked when Autolock is |
| 129 | // constructed and released when Autolock goes out of scope. |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 130 | class SCOPED_CAPABILITY Autolock { |
| 131 | public: |
| 132 | inline explicit Autolock(Mutex& mutex) ACQUIRE(mutex) : mLock(mutex) { mLock.lock(); } |
| 133 | inline explicit Autolock(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex) { mLock.lock(); } |
| 134 | inline ~Autolock() RELEASE() { mLock.unlock(); } |
| 135 | |
| 136 | private: |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 137 | Mutex& mLock; |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 138 | // Cannot be copied or moved - declarations only |
| 139 | Autolock(const Autolock&); |
| 140 | Autolock& operator=(const Autolock&); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 143 | private: |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 144 | friend class Condition; |
Jesse Hall | 601424a | 2014-12-23 14:00:29 -0800 | [diff] [blame] | 145 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 146 | // A mutex cannot be copied |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 147 | Mutex(const Mutex&); |
| 148 | Mutex& operator=(const Mutex&); |
Jesse Hall | 601424a | 2014-12-23 14:00:29 -0800 | [diff] [blame] | 149 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 150 | #if !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 151 | pthread_mutex_t mMutex; |
| 152 | #else |
Siarhei Vishniakou | 4e5b691 | 2017-07-12 13:36:51 -0700 | [diff] [blame] | 153 | void _init(); |
| 154 | void* mState; |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 155 | #endif |
| 156 | }; |
| 157 | |
| 158 | // --------------------------------------------------------------------------- |
| 159 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 160 | #if !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 161 | |
| 162 | inline Mutex::Mutex() { |
Yi Kong | c1a1562 | 2018-07-11 17:37:34 -0700 | [diff] [blame] | 163 | pthread_mutex_init(&mMutex, nullptr); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 164 | } |
Igor Murashkin | a27c1e0 | 2012-12-05 16:10:26 -0800 | [diff] [blame] | 165 | inline Mutex::Mutex(__attribute__((unused)) const char* name) { |
Yi Kong | c1a1562 | 2018-07-11 17:37:34 -0700 | [diff] [blame] | 166 | pthread_mutex_init(&mMutex, nullptr); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 167 | } |
Igor Murashkin | a27c1e0 | 2012-12-05 16:10:26 -0800 | [diff] [blame] | 168 | inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) { |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 169 | if (type == SHARED) { |
| 170 | pthread_mutexattr_t attr; |
| 171 | pthread_mutexattr_init(&attr); |
| 172 | pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
| 173 | pthread_mutex_init(&mMutex, &attr); |
| 174 | pthread_mutexattr_destroy(&attr); |
| 175 | } else { |
Yi Kong | c1a1562 | 2018-07-11 17:37:34 -0700 | [diff] [blame] | 176 | pthread_mutex_init(&mMutex, nullptr); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | inline Mutex::~Mutex() { |
| 180 | pthread_mutex_destroy(&mMutex); |
| 181 | } |
| 182 | inline status_t Mutex::lock() { |
| 183 | return -pthread_mutex_lock(&mMutex); |
| 184 | } |
| 185 | inline void Mutex::unlock() { |
| 186 | pthread_mutex_unlock(&mMutex); |
| 187 | } |
| 188 | inline status_t Mutex::tryLock() { |
| 189 | return -pthread_mutex_trylock(&mMutex); |
| 190 | } |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 191 | #if defined(__ANDROID__) |
Jesse Hall | 601424a | 2014-12-23 14:00:29 -0800 | [diff] [blame] | 192 | inline status_t Mutex::timedLock(nsecs_t timeoutNs) { |
Andy Hung | 604ba48 | 2016-08-23 18:15:32 -0700 | [diff] [blame] | 193 | timeoutNs += systemTime(SYSTEM_TIME_REALTIME); |
Jesse Hall | 601424a | 2014-12-23 14:00:29 -0800 | [diff] [blame] | 194 | const struct timespec ts = { |
Chih-Hung Hsieh | ba8cdf9 | 2015-01-12 16:21:46 -0800 | [diff] [blame] | 195 | /* .tv_sec = */ static_cast<time_t>(timeoutNs / 1000000000), |
| 196 | /* .tv_nsec = */ static_cast<long>(timeoutNs % 1000000000), |
Jesse Hall | 601424a | 2014-12-23 14:00:29 -0800 | [diff] [blame] | 197 | }; |
| 198 | return -pthread_mutex_timedlock(&mMutex, &ts); |
| 199 | } |
| 200 | #endif |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 201 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 202 | #endif // !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 203 | |
| 204 | // --------------------------------------------------------------------------- |
| 205 | |
| 206 | /* |
| 207 | * Automatic mutex. Declare one of these at the top of a function. |
| 208 | * When the function returns, it will go out of scope, and release the |
| 209 | * mutex. |
| 210 | */ |
Jesse Hall | 601424a | 2014-12-23 14:00:29 -0800 | [diff] [blame] | 211 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 212 | typedef Mutex::Autolock AutoMutex; |
| 213 | |
| 214 | // --------------------------------------------------------------------------- |
Pirama Arumuga Nainar | 90affce | 2018-04-11 23:10:28 -0700 | [diff] [blame] | 215 | } // namespace android |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 216 | // --------------------------------------------------------------------------- |
| 217 | |
| 218 | #endif // _LIBS_UTILS_MUTEX_H |