1 /**************************************************************************
3 * Copyright 2011-2012 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
27 * OS native thread abstraction.
29 * Mimics C++11 threads.
32 #ifndef _OS_THREAD_HPP_
33 #define _OS_THREAD_HPP_
43 #define USE_WIN32_CONDITION_VARIABLES 0
50 * - http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Thread_002dLocal.html
51 * - http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx
54 # define thread_specific __declspec(thread)
55 #elif defined(__GNUC__)
56 # define thread_specific __thread
58 # define thread_specific
59 # error "Unsupported compiler"
67 * Base class for mutex and recursive_mutex.
73 typedef CRITICAL_SECTION native_handle_type;
75 typedef pthread_mutex_t native_handle_type;
80 InitializeCriticalSection(&_native_handle);
82 pthread_mutexattr_t attr;
83 pthread_mutexattr_init(&attr);
84 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
85 pthread_mutex_init(&_native_handle, &attr);
86 pthread_mutexattr_destroy(&attr);
92 DeleteCriticalSection(&_native_handle);
94 pthread_mutex_destroy(&_native_handle);
101 EnterCriticalSection(&_native_handle);
103 pthread_mutex_lock(&_native_handle);
110 LeaveCriticalSection(&_native_handle);
112 pthread_mutex_unlock(&_native_handle);
116 native_handle_type & native_handle() {
117 return _native_handle;
121 native_handle_type _native_handle;
126 * Same interface as std::mutex.
128 class mutex : public _base_mutex
134 InitializeCriticalSection(&_native_handle);
136 pthread_mutex_init(&_native_handle, NULL);
143 * Same interface as std::recursive_mutex.
145 class recursive_mutex : public _base_mutex
149 recursive_mutex(void) {
151 InitializeCriticalSection(&_native_handle);
153 pthread_mutexattr_t attr;
154 pthread_mutexattr_init(&attr);
155 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
156 pthread_mutex_init(&_native_handle, &attr);
157 pthread_mutexattr_destroy(&attr);
164 * Same interface as std::unique_lock;
166 template< class Mutex >
170 typedef Mutex mutex_type;
173 unique_lock(mutex_type & mutex) :
205 * Same interface as std::condition_variable
207 class condition_variable
211 # if USE_WIN32_CONDITION_VARIABLES
212 // XXX: Only supported on Vista an higher. Not yet supported by WINE.
213 typedef CONDITION_VARIABLE native_handle_type;
214 native_handle_type _native_handle;
216 // http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
221 typedef pthread_cond_t native_handle_type;
222 native_handle_type _native_handle;
226 condition_variable() {
228 # if USE_WIN32_CONDITION_VARIABLES
229 InitializeConditionVariable(&_native_handle);
232 hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
235 pthread_cond_init(&_native_handle, NULL);
239 ~condition_variable() {
241 # if USE_WIN32_CONDITION_VARIABLES
247 pthread_cond_destroy(&_native_handle);
254 # if USE_WIN32_CONDITION_VARIABLES
255 WakeConditionVariable(&_native_handle);
262 pthread_cond_signal(&_native_handle);
267 wait(unique_lock<mutex> & lock) {
268 mutex::native_handle_type & mutex_native_handle = lock.mutex()->native_handle();
270 # if USE_WIN32_CONDITION_VARIABLES
271 SleepConditionVariableCS(&_native_handle, &mutex_native_handle, INFINITE);
273 InterlockedIncrement(&cWaiters);
274 LeaveCriticalSection(&mutex_native_handle);
275 WaitForSingleObject(hEvent, INFINITE);
276 EnterCriticalSection(&mutex_native_handle);
277 InterlockedDecrement(&cWaiters);
280 pthread_cond_wait(&_native_handle, &mutex_native_handle);
287 * Same interface as std::thread
292 typedef HANDLE native_handle_type;
294 typedef pthread_t native_handle_type;
304 thread(thread &other) :
305 _native_handle(other._native_handle)
309 template< class Function, class Arg >
310 explicit thread( Function& f, Arg arg ) {
313 _native_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)f, (LPVOID)arg, 0, &id);
315 pthread_create(&_native_handle, NULL, ( void *(*) (void *))f, arg);
320 joinable(void) const {
321 return _native_handle != 0;
327 WaitForSingleObject(_native_handle, INFINITE);
329 pthread_join(_native_handle, NULL);
334 native_handle_type _native_handle;
338 template< class Function, class Arg >
340 ThreadProc(LPVOID lpParameter) {
349 #endif /* _OS_THREAD_HPP_ */