blob: 29c2e8ce1c60f6833dc500100db9f4e04ed3a12f [file] [log] [blame]
Mathias Agopian2bd99592012-02-25 23:02:14 -08001/*
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 Cui4a6e5a32015-01-26 19:48:54 -080024#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -080025# include <pthread.h>
26#endif
27
28#include <utils/Errors.h>
Jesse Hall601424a2014-12-23 14:00:29 -080029#include <utils/Timers.h>
Mathias Agopian2bd99592012-02-25 23:02:14 -080030
Siarhei Vishniakou4e5b6912017-07-12 13:36:51 -070031// 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 Agopian2bd99592012-02-25 23:02:14 -080078// ---------------------------------------------------------------------------
79namespace android {
80// ---------------------------------------------------------------------------
81
82class Condition;
83
84/*
Greg Kaiserd62698d2016-04-05 10:55:13 -070085 * 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 Agopian2bd99592012-02-25 23:02:14 -080089 * 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 Vishniakou4e5b6912017-07-12 13:36:51 -070094class CAPABILITY("mutex") Mutex {
95 public:
Mathias Agopian2bd99592012-02-25 23:02:14 -080096 enum {
97 PRIVATE = 0,
98 SHARED = 1
99 };
Jesse Hall601424a2014-12-23 14:00:29 -0800100
Siarhei Vishniakou4e5b6912017-07-12 13:36:51 -0700101 Mutex();
102 explicit Mutex(const char* name);
Yi Kongc1a15622018-07-11 17:37:34 -0700103 explicit Mutex(int type, const char* name = nullptr);
Siarhei Vishniakou4e5b6912017-07-12 13:36:51 -0700104 ~Mutex();
Mathias Agopian2bd99592012-02-25 23:02:14 -0800105
106 // lock or unlock the mutex
Siarhei Vishniakou4e5b6912017-07-12 13:36:51 -0700107 status_t lock() ACQUIRE();
108 void unlock() RELEASE();
Mathias Agopian2bd99592012-02-25 23:02:14 -0800109
110 // lock if possible; returns 0 on success, error otherwise
Siarhei Vishniakou4e5b6912017-07-12 13:36:51 -0700111 status_t tryLock() TRY_ACQUIRE(true);
Mathias Agopian2bd99592012-02-25 23:02:14 -0800112
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700113#if defined(__ANDROID__)
Andy Hung604ba482016-08-23 18:15:32 -0700114 // Lock the mutex, but don't wait longer than timeoutNs (relative time).
Jesse Hall601424a2014-12-23 14:00:29 -0800115 // 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 Hung604ba482016-08-23 18:15:32 -0700120 //
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 Vishniakou4e5b6912017-07-12 13:36:51 -0700125 status_t timedLock(nsecs_t timeoutNs) TRY_ACQUIRE(true);
Jesse Hall601424a2014-12-23 14:00:29 -0800126#endif
127
Mathias Agopian2bd99592012-02-25 23:02:14 -0800128 // Manages the mutex automatically. It'll be locked when Autolock is
129 // constructed and released when Autolock goes out of scope.
Siarhei Vishniakou4e5b6912017-07-12 13:36:51 -0700130 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 Agopian2bd99592012-02-25 23:02:14 -0800137 Mutex& mLock;
Siarhei Vishniakou4e5b6912017-07-12 13:36:51 -0700138 // Cannot be copied or moved - declarations only
139 Autolock(const Autolock&);
140 Autolock& operator=(const Autolock&);
Mathias Agopian2bd99592012-02-25 23:02:14 -0800141 };
142
Siarhei Vishniakou4e5b6912017-07-12 13:36:51 -0700143 private:
Mathias Agopian2bd99592012-02-25 23:02:14 -0800144 friend class Condition;
Jesse Hall601424a2014-12-23 14:00:29 -0800145
Mathias Agopian2bd99592012-02-25 23:02:14 -0800146 // A mutex cannot be copied
Siarhei Vishniakou4e5b6912017-07-12 13:36:51 -0700147 Mutex(const Mutex&);
148 Mutex& operator=(const Mutex&);
Jesse Hall601424a2014-12-23 14:00:29 -0800149
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800150#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800151 pthread_mutex_t mMutex;
152#else
Siarhei Vishniakou4e5b6912017-07-12 13:36:51 -0700153 void _init();
154 void* mState;
Mathias Agopian2bd99592012-02-25 23:02:14 -0800155#endif
156};
157
158// ---------------------------------------------------------------------------
159
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800160#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800161
162inline Mutex::Mutex() {
Yi Kongc1a15622018-07-11 17:37:34 -0700163 pthread_mutex_init(&mMutex, nullptr);
Mathias Agopian2bd99592012-02-25 23:02:14 -0800164}
Igor Murashkina27c1e02012-12-05 16:10:26 -0800165inline Mutex::Mutex(__attribute__((unused)) const char* name) {
Yi Kongc1a15622018-07-11 17:37:34 -0700166 pthread_mutex_init(&mMutex, nullptr);
Mathias Agopian2bd99592012-02-25 23:02:14 -0800167}
Igor Murashkina27c1e02012-12-05 16:10:26 -0800168inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) {
Mathias Agopian2bd99592012-02-25 23:02:14 -0800169 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 Kongc1a15622018-07-11 17:37:34 -0700176 pthread_mutex_init(&mMutex, nullptr);
Mathias Agopian2bd99592012-02-25 23:02:14 -0800177 }
178}
179inline Mutex::~Mutex() {
180 pthread_mutex_destroy(&mMutex);
181}
182inline status_t Mutex::lock() {
183 return -pthread_mutex_lock(&mMutex);
184}
185inline void Mutex::unlock() {
186 pthread_mutex_unlock(&mMutex);
187}
188inline status_t Mutex::tryLock() {
189 return -pthread_mutex_trylock(&mMutex);
190}
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700191#if defined(__ANDROID__)
Jesse Hall601424a2014-12-23 14:00:29 -0800192inline status_t Mutex::timedLock(nsecs_t timeoutNs) {
Andy Hung604ba482016-08-23 18:15:32 -0700193 timeoutNs += systemTime(SYSTEM_TIME_REALTIME);
Jesse Hall601424a2014-12-23 14:00:29 -0800194 const struct timespec ts = {
Chih-Hung Hsiehba8cdf92015-01-12 16:21:46 -0800195 /* .tv_sec = */ static_cast<time_t>(timeoutNs / 1000000000),
196 /* .tv_nsec = */ static_cast<long>(timeoutNs % 1000000000),
Jesse Hall601424a2014-12-23 14:00:29 -0800197 };
198 return -pthread_mutex_timedlock(&mMutex, &ts);
199}
200#endif
Mathias Agopian2bd99592012-02-25 23:02:14 -0800201
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800202#endif // !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800203
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 Hall601424a2014-12-23 14:00:29 -0800211
Mathias Agopian2bd99592012-02-25 23:02:14 -0800212typedef Mutex::Autolock AutoMutex;
213
214// ---------------------------------------------------------------------------
Pirama Arumuga Nainar90affce2018-04-11 23:10:28 -0700215} // namespace android
Mathias Agopian2bd99592012-02-25 23:02:14 -0800216// ---------------------------------------------------------------------------
217
218#endif // _LIBS_UTILS_MUTEX_H