diff options
author | Mathias Agopian <mathias@google.com> | 2012-02-25 23:02:14 -0800 |
---|---|---|
committer | Alex Ray <aray@google.com> | 2013-07-30 13:56:59 -0700 |
commit | 2bd99599bb9eef197e6d1ccacb9f808ebfbcc598 (patch) | |
tree | 85876a98887836cb3a6293880b81b4f15842fdc2 /include/utils/Mutex.h | |
parent | 2ccfe1a0606f59b5cefd177f9dd5c837d0ea2d0b (diff) | |
download | system_core-2bd99599bb9eef197e6d1ccacb9f808ebfbcc598.zip system_core-2bd99599bb9eef197e6d1ccacb9f808ebfbcc598.tar.gz system_core-2bd99599bb9eef197e6d1ccacb9f808ebfbcc598.tar.bz2 |
split utils/threads.h into multiple headers
AndroidDefs.h - C/C++ enums and types
Mutex.h - Mutex class declaration
Condition.h - Condition class declaration
RWLock.h - RWLock class declaration
Thread.h - Thread class declaration
AndroidThreads.h - low-level thread creation functions
threads.h - includes all the above for backward source compatibility
Change-Id: Ia2f80c175333b59a41d1720985810bb2346e94cb
Note: implementations are still in Threads.cpp
Diffstat (limited to 'include/utils/Mutex.h')
-rw-r--r-- | include/utils/Mutex.h | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/include/utils/Mutex.h b/include/utils/Mutex.h new file mode 100644 index 0000000..de6fb39 --- /dev/null +++ b/include/utils/Mutex.h @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _LIBS_UTILS_MUTEX_H +#define _LIBS_UTILS_MUTEX_H + +#include <stdint.h> +#include <sys/types.h> +#include <time.h> + +#if defined(HAVE_PTHREADS) +# include <pthread.h> +#endif + +#include <utils/Errors.h> + +// --------------------------------------------------------------------------- +namespace android { +// --------------------------------------------------------------------------- + +class Condition; + +/* + * Simple mutex class. The implementation is system-dependent. + * + * The mutex must be unlocked by the thread that locked it. They are not + * recursive, i.e. the same thread can't lock it multiple times. + */ +class Mutex { +public: + enum { + PRIVATE = 0, + SHARED = 1 + }; + + Mutex(); + Mutex(const char* name); + Mutex(int type, const char* name = NULL); + ~Mutex(); + + // lock or unlock the mutex + status_t lock(); + void unlock(); + + // lock if possible; returns 0 on success, error otherwise + status_t tryLock(); + + // Manages the mutex automatically. It'll be locked when Autolock is + // constructed and released when Autolock goes out of scope. + class Autolock { + public: + inline Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); } + inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); } + inline ~Autolock() { mLock.unlock(); } + private: + Mutex& mLock; + }; + +private: + friend class Condition; + + // A mutex cannot be copied + Mutex(const Mutex&); + Mutex& operator = (const Mutex&); + +#if defined(HAVE_PTHREADS) + pthread_mutex_t mMutex; +#else + void _init(); + void* mState; +#endif +}; + +// --------------------------------------------------------------------------- + +#if defined(HAVE_PTHREADS) + +inline Mutex::Mutex() { + pthread_mutex_init(&mMutex, NULL); +} +inline Mutex::Mutex(const char* name) { + pthread_mutex_init(&mMutex, NULL); +} +inline Mutex::Mutex(int type, const char* name) { + if (type == SHARED) { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); + pthread_mutex_init(&mMutex, &attr); + pthread_mutexattr_destroy(&attr); + } else { + pthread_mutex_init(&mMutex, NULL); + } +} +inline Mutex::~Mutex() { + pthread_mutex_destroy(&mMutex); +} +inline status_t Mutex::lock() { + return -pthread_mutex_lock(&mMutex); +} +inline void Mutex::unlock() { + pthread_mutex_unlock(&mMutex); +} +inline status_t Mutex::tryLock() { + return -pthread_mutex_trylock(&mMutex); +} + +#endif // HAVE_PTHREADS + +// --------------------------------------------------------------------------- + +/* + * Automatic mutex. Declare one of these at the top of a function. + * When the function returns, it will go out of scope, and release the + * mutex. + */ + +typedef Mutex::Autolock AutoMutex; + +// --------------------------------------------------------------------------- +}; // namespace android +// --------------------------------------------------------------------------- + +#endif // _LIBS_UTILS_MUTEX_H |