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 * Simple OS abstraction.
29 * Mimics C++11 / boost 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();
243 SleepConditionVariableCS(&_native_handle, &mutex_native_handle, INFINITE);
245 pthread_cond_wait(&_native_handle, &mutex_native_handle);
250 native_handle_type _native_handle;
255 * Same interface as boost::thread_specific_ptr.
257 template <typename T>
258 class thread_specific_ptr
266 static void destructor(void *ptr) {
267 delete static_cast<T *>(ptr);
272 thread_specific_ptr(void) {
274 dwTlsIndex = TlsAlloc();
276 pthread_key_create(&key, &destructor);
280 ~thread_specific_ptr() {
284 pthread_key_delete(key);
291 ptr = TlsGetValue(dwTlsIndex);
293 ptr = pthread_getspecific(key);
295 return static_cast<T*>(ptr);
298 T* operator -> (void) const
303 T& operator * (void) const
308 void reset(T* new_value=0) {
309 T * old_value = get();
317 T * old_value = get();
323 void set(T* new_value) {
325 TlsSetValue(dwTlsIndex, new_value);
327 pthread_setspecific(key, new_value);
334 * Same interface as std::thread
339 typedef HANDLE native_handle_type;
341 typedef pthread_t native_handle_type;
344 template< class Function, class Arg >
345 explicit thread( Function& f, Arg arg ) {
349 _native_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)f, (LPVOID)arg, 0, &id);
351 pthread_create(&_native_handle, NULL, f, arg);
358 WaitForSingleObject(_native_handle, INFINITE);
360 pthread_join(_native_handle, NULL);
365 native_handle_type _native_handle;
369 template< class Function, class Arg >
371 ThreadProc(LPVOID lpParameter) {
380 #endif /* _OS_THREAD_HPP_ */