]> git.cworth.org Git - apitrace/blob - common/os_thread.hpp
Merge remote-tracking branch 'github/master' into profile-gui
[apitrace] / common / os_thread.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 /*
27  * Simple OS abstraction.
28  *
29  * Mimics C++11 / boost threads.
30  */
31
32 #ifndef _OS_THREAD_HPP_
33 #define _OS_THREAD_HPP_
34
35
36 #ifdef _WIN32
37 #include <windows.h>
38 #else
39 #include <pthread.h>
40 #endif
41
42 namespace os {
43
44
45     class recursive_mutex
46     {
47     public:
48 #ifdef _WIN32
49         typedef CRITICAL_SECTION native_handle_type;
50 #else
51         typedef pthread_mutex_t native_handle_type;
52 #endif
53
54         recursive_mutex(void) {
55 #ifdef _WIN32
56             InitializeCriticalSection(&_native_handle);
57 #else
58             pthread_mutexattr_t attr;
59             pthread_mutexattr_init(&attr);
60             pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
61             pthread_mutex_init(&_native_handle, &attr);
62             pthread_mutexattr_destroy(&attr);
63 #endif
64         }
65
66         ~recursive_mutex() {
67 #ifdef _WIN32
68             DeleteCriticalSection(&_native_handle);
69 #else
70             pthread_mutex_destroy(&_native_handle);
71 #endif
72         }
73
74         inline void
75         lock(void) {
76 #ifdef _WIN32
77             EnterCriticalSection(&_native_handle);
78 #else
79             pthread_mutex_lock(&_native_handle);
80 #endif
81         }
82
83         inline void
84         unlock(void) {
85 #ifdef _WIN32
86             LeaveCriticalSection(&_native_handle);
87 #else
88             pthread_mutex_unlock(&_native_handle);
89 #endif
90         }
91
92     private:
93         native_handle_type _native_handle;
94     };
95
96
97     template <typename T>
98     class thread_specific_ptr
99     {
100     private:
101 #ifdef _WIN32
102         DWORD dwTlsIndex;
103 #else
104         pthread_key_t key;
105
106         static void destructor(void *ptr) {
107             delete static_cast<T *>(ptr);
108         }
109 #endif
110
111     public:
112         thread_specific_ptr(void) {
113 #ifdef _WIN32
114             dwTlsIndex = TlsAlloc();
115 #else
116             pthread_key_create(&key, &destructor);
117 #endif
118         }
119
120         ~thread_specific_ptr() {
121 #ifdef _WIN32
122             TlsFree(dwTlsIndex);
123 #else
124             pthread_key_delete(key);
125 #endif
126         }
127
128         T* get(void) const {
129             void *ptr;
130 #ifdef _WIN32
131             ptr = TlsGetValue(dwTlsIndex);
132 #else
133             ptr = pthread_getspecific(key);
134 #endif
135             return static_cast<T*>(ptr);
136         }
137
138         T* operator -> (void) const
139         {
140             return get();
141         }
142
143         T& operator * (void) const
144         {
145             return *get();
146         }
147
148         void reset(T* new_value=0) {
149             T * old_value = get();
150 #ifdef _WIN32
151             TlsSetValue(dwTlsIndex, new_value);
152 #else
153             pthread_setspecific(key, new_value);
154 #endif
155             if (old_value) {
156                 delete old_value;
157             }
158         }
159     };
160
161 } /* namespace os */
162
163 #endif /* _OS_THREAD_HPP_ */