• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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			(deUint32 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) { m_mutex.unlock(); }
66 
67 private:
68 					ScopedLock		(const ScopedLock& other); // Not allowed!
69 	ScopedLock&		operator=		(const ScopedLock& other); // Not allowed!
70 
71 	Mutex&			m_mutex;
72 };
73 
74 // Mutex inline implementations.
75 
76 /*--------------------------------------------------------------------*//*!
77  * \brief Acquire mutex lock.
78  * \note This method will never report failure. If an error occurs due
79  *		 to misuse or other reason it will lead to process termination
80 *		 in debug build.
81  *
82  * If mutex is currently locked the function will block until current
83  * lock is released.
84  *
85  * In recursive mode further calls from the thread owning the mutex will
86  * succeed and increment lock count.
87  *//*--------------------------------------------------------------------*/
lock(void)88 inline void Mutex::lock (void) throw()
89 {
90 	deMutex_lock(m_mutex);
91 }
92 
93 /*--------------------------------------------------------------------*//*!
94  * \brief Release mutex lock.
95  * \note This method will never report failure. If an error occurs due
96  *		 to misuse or other reason it will lead to process termination
97 *		 in debug build.
98  *
99  * In recursive mode the mutex will be released once the lock count reaches
100  * zero.
101  *//*--------------------------------------------------------------------*/
unlock(void)102 inline void Mutex::unlock (void) throw()
103 {
104 	deMutex_unlock(m_mutex);
105 }
106 
107 /*--------------------------------------------------------------------*//*!
108  * \brief Try to acquire lock.
109  * \return Returns true if lock was acquired and false otherwise.
110  *
111  * This function will never block, i.e. it will return false if mutex
112  * is currently locked.
113  *//*--------------------------------------------------------------------*/
tryLock(void)114 inline bool Mutex::tryLock (void) throw()
115 {
116 	return deMutex_tryLock(m_mutex) == DE_TRUE;
117 }
118 
119 // ScopedLock inline implementations.
120 
121 /*--------------------------------------------------------------------*//*!
122  * \brief Acquire scoped lock to mutex.
123  * \param mutex Mutex to be locked.
124  *//*--------------------------------------------------------------------*/
ScopedLock(Mutex & mutex)125 inline ScopedLock::ScopedLock (Mutex& mutex)
126 	: m_mutex(mutex)
127 {
128 	m_mutex.lock();
129 }
130 
131 } // de
132 
133 #endif // _DEMUTEX_HPP
134