2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef _LIBS_UTILS_MUTEX_H
18 #define _LIBS_UTILS_MUTEX_H
21 #include <sys/types.h>
24 #if defined(HAVE_PTHREADS)
28 #include <utils/Errors.h>
30 // ---------------------------------------------------------------------------
32 // ---------------------------------------------------------------------------
37 * Simple mutex class. The implementation is system-dependent.
39 * The mutex must be unlocked by the thread that locked it. They are not
40 * recursive, i.e. the same thread can't lock it multiple times.
50 Mutex(const char* name);
51 Mutex(int type, const char* name = NULL);
54 // lock or unlock the mutex
58 // lock if possible; returns 0 on success, error otherwise
61 // Manages the mutex automatically. It'll be locked when Autolock is
62 // constructed and released when Autolock goes out of scope.
65 inline Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); }
66 inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); }
67 inline ~Autolock() { mLock.unlock(); }
73 friend class Condition;
75 // A mutex cannot be copied
77 Mutex& operator = (const Mutex&);
79 #if defined(HAVE_PTHREADS)
80 pthread_mutex_t mMutex;
87 // ---------------------------------------------------------------------------
89 #if defined(HAVE_PTHREADS)
91 inline Mutex::Mutex() {
92 pthread_mutex_init(&mMutex, NULL);
94 inline Mutex::Mutex(const char* name) {
95 pthread_mutex_init(&mMutex, NULL);
97 inline Mutex::Mutex(int type, const char* name) {
99 pthread_mutexattr_t attr;
100 pthread_mutexattr_init(&attr);
101 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
102 pthread_mutex_init(&mMutex, &attr);
103 pthread_mutexattr_destroy(&attr);
105 pthread_mutex_init(&mMutex, NULL);
108 inline Mutex::~Mutex() {
109 pthread_mutex_destroy(&mMutex);
111 inline status_t Mutex::lock() {
112 return -pthread_mutex_lock(&mMutex);
114 inline void Mutex::unlock() {
115 pthread_mutex_unlock(&mMutex);
117 inline status_t Mutex::tryLock() {
118 return -pthread_mutex_trylock(&mMutex);
121 #endif // HAVE_PTHREADS
123 // ---------------------------------------------------------------------------
126 * Automatic mutex. Declare one of these at the top of a function.
127 * When the function returns, it will go out of scope, and release the
131 typedef Mutex::Autolock AutoMutex;
133 // ---------------------------------------------------------------------------
134 }; // namespace android
135 // ---------------------------------------------------------------------------
137 #endif // _LIBS_UTILS_MUTEX_H