1 /**************************************************************************
3 * Copyright 2011 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 boost::thread_specific_ptr.
145 template <typename T>
146 class thread_specific_ptr
154 static void destructor(void *ptr) {
155 delete static_cast<T *>(ptr);
160 thread_specific_ptr(void) {
162 dwTlsIndex = TlsAlloc();
164 pthread_key_create(&key, &destructor);
168 ~thread_specific_ptr() {
172 pthread_key_delete(key);
179 ptr = TlsGetValue(dwTlsIndex);
181 ptr = pthread_getspecific(key);
183 return static_cast<T*>(ptr);
186 T* operator -> (void) const
191 T& operator * (void) const
196 void reset(T* new_value=0) {
197 T * old_value = get();
199 TlsSetValue(dwTlsIndex, new_value);
201 pthread_setspecific(key, new_value);
212 #endif /* _OS_THREAD_HPP_ */