]> git.sesse.net Git - casparcg/blob - dependencies64/tbb/include/tbb/mutex.h
Unpacked dependencies64
[casparcg] / dependencies64 / tbb / include / tbb / mutex.h
1 /*
2     Copyright 2005-2011 Intel Corporation.  All Rights Reserved.
3
4     This file is part of Threading Building Blocks.
5
6     Threading Building Blocks is free software; you can redistribute it
7     and/or modify it under the terms of the GNU General Public License
8     version 2 as published by the Free Software Foundation.
9
10     Threading Building Blocks is distributed in the hope that it will be
11     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with Threading Building Blocks; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19     As a special exception, you may use this file as part of a free software
20     library without restriction.  Specifically, if other files instantiate
21     templates or use macros or inline functions from this file, or you compile
22     this file and link it with other files to produce an executable, this
23     file does not by itself cause the resulting executable to be covered by
24     the GNU General Public License.  This exception does not however
25     invalidate any other reasons why the executable file might be covered by
26     the GNU General Public License.
27 */
28
29 #ifndef __TBB_mutex_H
30 #define __TBB_mutex_H
31
32 #if _WIN32||_WIN64
33 #include "machine/windows_api.h"
34 #else
35 #include <pthread.h>
36 #endif /* _WIN32||_WIN64 */
37
38 #include <new>
39 #include "aligned_space.h"
40 #include "tbb_stddef.h"
41 #include "tbb_profiling.h"
42
43 namespace tbb {
44
45 //! Wrapper around the platform's native reader-writer lock.
46 /** For testing purposes only.
47     @ingroup synchronization */
48 class mutex {
49 public:
50     //! Construct unacquired mutex.
51     mutex() {
52 #if TBB_USE_ASSERT || TBB_USE_THREADING_TOOLS
53     internal_construct();
54 #else
55   #if _WIN32||_WIN64
56         InitializeCriticalSection(&impl);
57   #else
58         int error_code = pthread_mutex_init(&impl,NULL);
59         if( error_code )
60             tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_init failed");
61   #endif /* _WIN32||_WIN64*/
62 #endif /* TBB_USE_ASSERT */
63     };
64
65     ~mutex() {
66 #if TBB_USE_ASSERT
67         internal_destroy();
68 #else
69   #if _WIN32||_WIN64
70         DeleteCriticalSection(&impl);
71   #else
72         pthread_mutex_destroy(&impl); 
73
74   #endif /* _WIN32||_WIN64 */
75 #endif /* TBB_USE_ASSERT */
76     };
77
78     class scoped_lock;
79     friend class scoped_lock;
80
81     //! The scoped locking pattern
82     /** It helps to avoid the common problem of forgetting to release lock.
83         It also nicely provides the "node" for queuing locks. */
84     class scoped_lock : internal::no_copy {
85     public:
86         //! Construct lock that has not acquired a mutex. 
87         scoped_lock() : my_mutex(NULL) {};
88
89         //! Acquire lock on given mutex.
90         scoped_lock( mutex& mutex ) {
91             acquire( mutex );
92         }
93
94         //! Release lock (if lock is held).
95         ~scoped_lock() {
96             if( my_mutex ) 
97                 release();
98         }
99
100         //! Acquire lock on given mutex.
101         void acquire( mutex& mutex ) {
102 #if TBB_USE_ASSERT
103             internal_acquire(mutex);
104 #else
105             mutex.lock();
106             my_mutex = &mutex;
107 #endif /* TBB_USE_ASSERT */
108         }
109
110         //! Try acquire lock on given mutex.
111         bool try_acquire( mutex& mutex ) {
112 #if TBB_USE_ASSERT
113             return internal_try_acquire (mutex);
114 #else
115             bool result = mutex.try_lock();
116             if( result )
117                 my_mutex = &mutex;
118             return result;
119 #endif /* TBB_USE_ASSERT */
120         }
121
122         //! Release lock
123         void release() {
124 #if TBB_USE_ASSERT
125             internal_release ();
126 #else
127             my_mutex->unlock();
128             my_mutex = NULL;
129 #endif /* TBB_USE_ASSERT */
130         }
131
132     private:
133         //! The pointer to the current mutex to work
134         mutex* my_mutex;
135
136         //! All checks from acquire using mutex.state were moved here
137         void __TBB_EXPORTED_METHOD internal_acquire( mutex& m );
138
139         //! All checks from try_acquire using mutex.state were moved here
140         bool __TBB_EXPORTED_METHOD internal_try_acquire( mutex& m );
141
142         //! All checks from release using mutex.state were moved here
143         void __TBB_EXPORTED_METHOD internal_release();
144
145         friend class mutex;
146     };
147
148     // Mutex traits
149     static const bool is_rw_mutex = false;
150     static const bool is_recursive_mutex = false;
151     static const bool is_fair_mutex = false;
152
153     // ISO C++0x compatibility methods
154
155     //! Acquire lock
156     void lock() {
157 #if TBB_USE_ASSERT
158         aligned_space<scoped_lock,1> tmp;
159         new(tmp.begin()) scoped_lock(*this);
160 #else
161   #if _WIN32||_WIN64
162         EnterCriticalSection(&impl);
163   #else
164         pthread_mutex_lock(&impl);
165   #endif /* _WIN32||_WIN64 */
166 #endif /* TBB_USE_ASSERT */
167     }
168
169     //! Try acquiring lock (non-blocking)
170     /** Return true if lock acquired; false otherwise. */
171     bool try_lock() {
172 #if TBB_USE_ASSERT
173         aligned_space<scoped_lock,1> tmp;
174         scoped_lock& s = *tmp.begin();
175         s.my_mutex = NULL;
176         return s.internal_try_acquire(*this);
177 #else
178   #if _WIN32||_WIN64
179         return TryEnterCriticalSection(&impl)!=0;
180   #else
181         return pthread_mutex_trylock(&impl)==0;
182   #endif /* _WIN32||_WIN64 */
183 #endif /* TBB_USE_ASSERT */
184     }
185
186     //! Release lock
187     void unlock() {
188 #if TBB_USE_ASSERT
189         aligned_space<scoped_lock,1> tmp;
190         scoped_lock& s = *tmp.begin();
191         s.my_mutex = this;
192         s.internal_release();
193 #else
194   #if _WIN32||_WIN64
195         LeaveCriticalSection(&impl);
196   #else
197         pthread_mutex_unlock(&impl);
198   #endif /* _WIN32||_WIN64 */
199 #endif /* TBB_USE_ASSERT */
200     }
201
202     //! Return native_handle
203   #if _WIN32||_WIN64
204     typedef LPCRITICAL_SECTION native_handle_type;
205   #else
206     typedef pthread_mutex_t* native_handle_type;
207   #endif
208     native_handle_type native_handle() { return (native_handle_type) &impl; }
209
210     enum state_t {
211         INITIALIZED=0x1234,
212         DESTROYED=0x789A,
213         HELD=0x56CD
214     };
215 private:
216 #if _WIN32||_WIN64
217     CRITICAL_SECTION impl;    
218     enum state_t state;
219 #else
220     pthread_mutex_t impl;
221 #endif /* _WIN32||_WIN64 */
222
223     //! All checks from mutex constructor using mutex.state were moved here
224     void __TBB_EXPORTED_METHOD internal_construct();
225
226     //! All checks from mutex destructor using mutex.state were moved here
227     void __TBB_EXPORTED_METHOD internal_destroy();
228
229 #if _WIN32||_WIN64
230 public:
231     //!  Set the internal state
232     void set_state( state_t to ) { state = to; }
233 #endif
234 };
235
236 __TBB_DEFINE_PROFILING_SET_NAME(mutex)
237
238 } // namespace tbb 
239
240 #endif /* __TBB_mutex_H */