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_
47 * - http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Thread_002dLocal.html
48 * - http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx
51 # define thread_specific __declspec(thread)
52 #elif defined(__GNUC__)
53 # define thread_specific __thread
55 # define thread_specific
56 # error "Unsupported compiler"
64 * Base class for mutex and recursive_mutex.
70 typedef CRITICAL_SECTION native_handle_type;
72 typedef pthread_mutex_t native_handle_type;
77 InitializeCriticalSection(&_native_handle);
79 pthread_mutexattr_t attr;
80 pthread_mutexattr_init(&attr);
81 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
82 pthread_mutex_init(&_native_handle, &attr);
83 pthread_mutexattr_destroy(&attr);
89 DeleteCriticalSection(&_native_handle);
91 pthread_mutex_destroy(&_native_handle);
98 EnterCriticalSection(&_native_handle);
100 pthread_mutex_lock(&_native_handle);
107 LeaveCriticalSection(&_native_handle);
109 pthread_mutex_unlock(&_native_handle);
113 native_handle_type & native_handle() {
114 return _native_handle;
118 native_handle_type _native_handle;
123 * Same interface as std::mutex.
125 class mutex : public _base_mutex
131 InitializeCriticalSection(&_native_handle);
133 pthread_mutex_init(&_native_handle, NULL);
140 * Same interface as std::recursive_mutex.
142 class recursive_mutex : public _base_mutex
146 recursive_mutex(void) {
148 InitializeCriticalSection(&_native_handle);
150 pthread_mutexattr_t attr;
151 pthread_mutexattr_init(&attr);
152 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
153 pthread_mutex_init(&_native_handle, &attr);
154 pthread_mutexattr_destroy(&attr);
161 * Same interface as std::unique_lock;
163 template< class Mutex >
167 typedef Mutex mutex_type;
170 unique_lock(mutex_type & mutex) :
202 * Same interface as std::condition_variable
204 class condition_variable
208 typedef CONDITION_VARIABLE native_handle_type;
210 typedef pthread_cond_t native_handle_type;
213 condition_variable() {
215 InitializeConditionVariable(&_native_handle);
217 pthread_cond_init(&_native_handle, NULL);
221 ~condition_variable() {
225 pthread_cond_destroy(&_native_handle);
232 WakeConditionVariable(&_native_handle);
234 pthread_cond_signal(&_native_handle);
239 wait(unique_lock<mutex> & lock) {
240 mutex::native_handle_type & mutex_native_handle = lock.mutex()->native_handle();
242 SleepConditionVariableCS(&_native_handle, &mutex_native_handle, INFINITE);
244 pthread_cond_wait(&_native_handle, &mutex_native_handle);
249 native_handle_type _native_handle;
254 * Same interface as std::thread
259 typedef HANDLE native_handle_type;
261 typedef pthread_t native_handle_type;
271 thread(thread &other) :
272 _native_handle(other._native_handle)
276 template< class Function, class Arg >
277 explicit thread( Function& f, Arg arg ) {
280 _native_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)f, (LPVOID)arg, 0, &id);
282 pthread_create(&_native_handle, NULL, ( void *(*) (void *))f, arg);
287 joinable(void) const {
288 return _native_handle != 0;
294 WaitForSingleObject(_native_handle, INFINITE);
296 pthread_join(_native_handle, NULL);
301 native_handle_type _native_handle;
305 template< class Function, class Arg >
307 ThreadProc(LPVOID lpParameter) {
316 #endif /* _OS_THREAD_HPP_ */