1 #ifndef _DEMUTEX_HPP
2 #define _DEMUTEX_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements C++ Base Library
5 * -----------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief deMutex C++ wrapper.
24 *//*--------------------------------------------------------------------*/
25
26 #include "deDefs.hpp"
27 #include "deMutex.h"
28
29 namespace de
30 {
31
32 /*--------------------------------------------------------------------*//*!
33 * \brief Mutual exclusion lock
34 *
35 * Mutex class provides standard mutual exclusion lock functionality.
36 *//*--------------------------------------------------------------------*/
37 class Mutex
38 {
39 public:
40 Mutex(uint32_t flags = 0);
41 ~Mutex(void);
42
43 void lock(void) throw();
44 void unlock(void) throw();
45 bool tryLock(void) throw();
46
47 private:
48 Mutex(const Mutex &other); // Not allowed!
49 Mutex &operator=(const Mutex &other); // Not allowed!
50
51 deMutex m_mutex;
52 };
53
54 /*--------------------------------------------------------------------*//*!
55 * \brief Scoped mutex lock.
56 *
57 * ScopedLock provides helper for maintaining Mutex lock for the duration
58 * of current scope. The lock is acquired in constructor and released
59 * when ScopedLock goes out of scope.
60 *//*--------------------------------------------------------------------*/
61 class ScopedLock
62 {
63 public:
64 ScopedLock(Mutex &mutex);
~ScopedLock(void)65 ~ScopedLock(void)
66 {
67 m_mutex.unlock();
68 }
69
70 private:
71 ScopedLock(const ScopedLock &other); // Not allowed!
72 ScopedLock &operator=(const ScopedLock &other); // Not allowed!
73
74 Mutex &m_mutex;
75 };
76
77 // Mutex inline implementations.
78
79 /*--------------------------------------------------------------------*//*!
80 * \brief Acquire mutex lock.
81 * \note This method will never report failure. If an error occurs due
82 * to misuse or other reason it will lead to process termination
83 * in debug build.
84 *
85 * If mutex is currently locked the function will block until current
86 * lock is released.
87 *
88 * In recursive mode further calls from the thread owning the mutex will
89 * succeed and increment lock count.
90 *//*--------------------------------------------------------------------*/
lock(void)91 inline void Mutex::lock(void) throw()
92 {
93 deMutex_lock(m_mutex);
94 }
95
96 /*--------------------------------------------------------------------*//*!
97 * \brief Release mutex lock.
98 * \note This method will never report failure. If an error occurs due
99 * to misuse or other reason it will lead to process termination
100 * in debug build.
101 *
102 * In recursive mode the mutex will be released once the lock count reaches
103 * zero.
104 *//*--------------------------------------------------------------------*/
unlock(void)105 inline void Mutex::unlock(void) throw()
106 {
107 deMutex_unlock(m_mutex);
108 }
109
110 /*--------------------------------------------------------------------*//*!
111 * \brief Try to acquire lock.
112 * \return Returns true if lock was acquired and false otherwise.
113 *
114 * This function will never block, i.e. it will return false if mutex
115 * is currently locked.
116 *//*--------------------------------------------------------------------*/
tryLock(void)117 inline bool Mutex::tryLock(void) throw()
118 {
119 return deMutex_tryLock(m_mutex) == true;
120 }
121
122 // ScopedLock inline implementations.
123
124 /*--------------------------------------------------------------------*//*!
125 * \brief Acquire scoped lock to mutex.
126 * \param mutex Mutex to be locked.
127 *//*--------------------------------------------------------------------*/
ScopedLock(Mutex & mutex)128 inline ScopedLock::ScopedLock(Mutex &mutex) : m_mutex(mutex)
129 {
130 m_mutex.lock();
131 }
132
133 } // namespace de
134
135 #endif // _DEMUTEX_HPP
136