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_
46 * Base class for mutex and recursive_mutex.
52 typedef CRITICAL_SECTION native_handle_type;
54 typedef pthread_mutex_t native_handle_type;
59 InitializeCriticalSection(&_native_handle);
61 pthread_mutexattr_t attr;
62 pthread_mutexattr_init(&attr);
63 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
64 pthread_mutex_init(&_native_handle, &attr);
65 pthread_mutexattr_destroy(&attr);
71 DeleteCriticalSection(&_native_handle);
73 pthread_mutex_destroy(&_native_handle);
80 EnterCriticalSection(&_native_handle);
82 pthread_mutex_lock(&_native_handle);
89 LeaveCriticalSection(&_native_handle);
91 pthread_mutex_unlock(&_native_handle);
95 native_handle_type & native_handle() {
96 return _native_handle;
100 native_handle_type _native_handle;
105 * Same interface as std::mutex.
107 class mutex : public _base_mutex
113 InitializeCriticalSection(&_native_handle);
115 pthread_mutex_init(&_native_handle, NULL);
122 * Same interface as std::recursive_mutex.
124 class recursive_mutex : public _base_mutex
128 recursive_mutex(void) {
130 InitializeCriticalSection(&_native_handle);
132 pthread_mutexattr_t attr;
133 pthread_mutexattr_init(&attr);
134 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
135 pthread_mutex_init(&_native_handle, &attr);
136 pthread_mutexattr_destroy(&attr);
143 * Same interface as std::unique_lock;
145 template< class Mutex >
149 typedef Mutex mutex_type;
152 unique_lock(mutex_type & mutex) :
184 * Same interface as std::condition_variable
186 class condition_variable
190 typedef CONDITION_VARIABLE native_handle_type;
192 typedef pthread_cond_t native_handle_type;
195 condition_variable() {
197 InitializeConditionVariable(&_native_handle);
199 pthread_cond_init(&_native_handle, NULL);
203 ~condition_variable() {
207 pthread_cond_destroy(&_native_handle);
214 WakeConditionVariable(&_native_handle);
216 pthread_cond_signal(&_native_handle);
221 wait(unique_lock<mutex> & lock) {
222 mutex::native_handle_type & mutex_native_handle = lock.mutex()->native_handle();
225 SleepConditionVariableCS(&_native_handle, &mutex_native_handle, INFINITE);
227 pthread_cond_wait(&_native_handle, &mutex_native_handle);
232 native_handle_type _native_handle;
237 * Same interface as boost::thread_specific_ptr.
239 template <typename T>
240 class thread_specific_ptr
248 static void destructor(void *ptr) {
249 delete static_cast<T *>(ptr);
254 thread_specific_ptr(void) {
256 dwTlsIndex = TlsAlloc();
258 pthread_key_create(&key, &destructor);
262 ~thread_specific_ptr() {
266 pthread_key_delete(key);
273 ptr = TlsGetValue(dwTlsIndex);
275 ptr = pthread_getspecific(key);
277 return static_cast<T*>(ptr);
280 T* operator -> (void) const
285 T& operator * (void) const
290 void reset(T* new_value=0) {
291 T * old_value = get();
299 T * old_value = get();
305 void set(T* new_value) {
307 TlsSetValue(dwTlsIndex, new_value);
309 pthread_setspecific(key, new_value);
316 * Same interface as std::thread
321 typedef HANDLE native_handle_type;
323 typedef pthread_t native_handle_type;
326 template< class Function, class Arg >
327 explicit thread( Function& f, Arg & arg ) {
331 _native_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)f, (LPVOID)arg, 0, &id);
333 pthread_create(&_native_handle, NULL, f, arg);
340 WaitForSingleObject(_native_handle, INFINITE);
342 pthread_join(_native_handle, NULL);
347 native_handle_type _native_handle;
351 template< class Function, class Arg >
353 ThreadProc(LPVOID lpParameter) {
362 #endif /* _OS_THREAD_HPP_ */