blob: d106185f020e7e79a4102a07e633c31b1c25c41d [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
31// ---------------------------------------------------------------------------
32namespace android {
33// ---------------------------------------------------------------------------
34
35class Condition;
36
37/*
Greg Kaiserd62698d2016-04-05 10:55:13 -070038 * NOTE: This class is for code that builds on Win32. Its usage is
39 * deprecated for code which doesn't build for Win32. New code which
40 * doesn't build for Win32 should use std::mutex and std::lock_guard instead.
41 *
Mathias Agopian2bd99592012-02-25 23:02:14 -080042 * Simple mutex class. The implementation is system-dependent.
43 *
44 * The mutex must be unlocked by the thread that locked it. They are not
45 * recursive, i.e. the same thread can't lock it multiple times.
46 */
47class Mutex {
48public:
49 enum {
50 PRIVATE = 0,
51 SHARED = 1
52 };
Jesse Hall601424a2014-12-23 14:00:29 -080053
Mathias Agopian2bd99592012-02-25 23:02:14 -080054 Mutex();
Chih-Hung Hsieh2a929962016-08-02 12:14:47 -070055 explicit Mutex(const char* name);
56 explicit Mutex(int type, const char* name = NULL);
Mathias Agopian2bd99592012-02-25 23:02:14 -080057 ~Mutex();
58
59 // lock or unlock the mutex
60 status_t lock();
61 void unlock();
62
63 // lock if possible; returns 0 on success, error otherwise
64 status_t tryLock();
65
Elliott Hughes9b828ad2015-07-30 08:47:35 -070066#if defined(__ANDROID__)
Andy Hung604ba482016-08-23 18:15:32 -070067 // Lock the mutex, but don't wait longer than timeoutNs (relative time).
Jesse Hall601424a2014-12-23 14:00:29 -080068 // Returns 0 on success, TIMED_OUT for failure due to timeout expiration.
69 //
70 // OSX doesn't have pthread_mutex_timedlock() or equivalent. To keep
71 // capabilities consistent across host OSes, this method is only available
72 // when building Android binaries.
Andy Hung604ba482016-08-23 18:15:32 -070073 //
74 // FIXME?: pthread_mutex_timedlock is based on CLOCK_REALTIME,
75 // which is subject to NTP adjustments, and includes time during suspend,
76 // so a timeout may occur even though no processes could run.
77 // Not holding a partial wakelock may lead to a system suspend.
78 status_t timedLock(nsecs_t timeoutNs);
Jesse Hall601424a2014-12-23 14:00:29 -080079#endif
80
Mathias Agopian2bd99592012-02-25 23:02:14 -080081 // Manages the mutex automatically. It'll be locked when Autolock is
82 // constructed and released when Autolock goes out of scope.
83 class Autolock {
84 public:
Chih-Hung Hsieh2a929962016-08-02 12:14:47 -070085 inline explicit Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); }
86 inline explicit Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); }
Mathias Agopian2bd99592012-02-25 23:02:14 -080087 inline ~Autolock() { mLock.unlock(); }
88 private:
89 Mutex& mLock;
90 };
91
92private:
93 friend class Condition;
Jesse Hall601424a2014-12-23 14:00:29 -080094
Mathias Agopian2bd99592012-02-25 23:02:14 -080095 // A mutex cannot be copied
96 Mutex(const Mutex&);
97 Mutex& operator = (const Mutex&);
Jesse Hall601424a2014-12-23 14:00:29 -080098
Yabin Cui4a6e5a32015-01-26 19:48:54 -080099#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800100 pthread_mutex_t mMutex;
101#else
102 void _init();
103 void* mState;
104#endif
105};
106
107// ---------------------------------------------------------------------------
108
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800109#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800110
111inline Mutex::Mutex() {
112 pthread_mutex_init(&mMutex, NULL);
113}
Igor Murashkina27c1e02012-12-05 16:10:26 -0800114inline Mutex::Mutex(__attribute__((unused)) const char* name) {
Mathias Agopian2bd99592012-02-25 23:02:14 -0800115 pthread_mutex_init(&mMutex, NULL);
116}
Igor Murashkina27c1e02012-12-05 16:10:26 -0800117inline Mutex::Mutex(int type, __attribute__((unused)) const char* name) {
Mathias Agopian2bd99592012-02-25 23:02:14 -0800118 if (type == SHARED) {
119 pthread_mutexattr_t attr;
120 pthread_mutexattr_init(&attr);
121 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
122 pthread_mutex_init(&mMutex, &attr);
123 pthread_mutexattr_destroy(&attr);
124 } else {
125 pthread_mutex_init(&mMutex, NULL);
126 }
127}
128inline Mutex::~Mutex() {
129 pthread_mutex_destroy(&mMutex);
130}
131inline status_t Mutex::lock() {
132 return -pthread_mutex_lock(&mMutex);
133}
134inline void Mutex::unlock() {
135 pthread_mutex_unlock(&mMutex);
136}
137inline status_t Mutex::tryLock() {
138 return -pthread_mutex_trylock(&mMutex);
139}
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700140#if defined(__ANDROID__)
Jesse Hall601424a2014-12-23 14:00:29 -0800141inline status_t Mutex::timedLock(nsecs_t timeoutNs) {
Andy Hung604ba482016-08-23 18:15:32 -0700142 timeoutNs += systemTime(SYSTEM_TIME_REALTIME);
Jesse Hall601424a2014-12-23 14:00:29 -0800143 const struct timespec ts = {
Chih-Hung Hsiehba8cdf92015-01-12 16:21:46 -0800144 /* .tv_sec = */ static_cast<time_t>(timeoutNs / 1000000000),
145 /* .tv_nsec = */ static_cast<long>(timeoutNs % 1000000000),
Jesse Hall601424a2014-12-23 14:00:29 -0800146 };
147 return -pthread_mutex_timedlock(&mMutex, &ts);
148}
149#endif
Mathias Agopian2bd99592012-02-25 23:02:14 -0800150
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800151#endif // !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800152
153// ---------------------------------------------------------------------------
154
155/*
156 * Automatic mutex. Declare one of these at the top of a function.
157 * When the function returns, it will go out of scope, and release the
158 * mutex.
159 */
Jesse Hall601424a2014-12-23 14:00:29 -0800160
Mathias Agopian2bd99592012-02-25 23:02:14 -0800161typedef Mutex::Autolock AutoMutex;
162
163// ---------------------------------------------------------------------------
164}; // namespace android
165// ---------------------------------------------------------------------------
166
167#endif // _LIBS_UTILS_MUTEX_H