]> git.cworth.org Git - apitrace/blob - common/os_thread.hpp
More helpful messages on exceptions inside apitrace code.
[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 #ifdef _WIN32
36 #include <windows.h>
37 #else
38 #include <pthread.h>
39 #endif
40
41 namespace os {
42
43
44     template <typename T>
45     class thread_specific_ptr
46     {
47     private:
48 #ifdef _WIN32
49         DWORD dwTlsIndex;
50 #else
51         pthread_key_t key;
52
53         static void destructor(void *ptr) {
54             delete static_cast<T *>(ptr);
55         }
56 #endif
57
58     public:
59         thread_specific_ptr(void) {
60 #ifdef _WIN32
61             dwTlsIndex = TlsAlloc();
62 #else
63             pthread_key_create(&key, &destructor);
64 #endif
65         }
66
67         ~thread_specific_ptr() {
68 #ifdef _WIN32
69             TlsFree(dwTlsIndex);
70 #else
71             pthread_key_delete(key);
72 #endif
73         }
74
75         T* get(void) const {
76             void *ptr;
77 #ifdef _WIN32
78             ptr = TlsGetValue(dwTlsIndex);
79 #else
80             ptr = pthread_getspecific(key);
81 #endif
82             return static_cast<T*>(ptr);
83         }
84
85         T* operator -> (void) const
86         {
87             return get();
88         }
89
90         T& operator * (void) const
91         {
92             return *get();
93         }
94
95         void reset(T* new_value=0) {
96             T * old_value = get();
97 #ifdef _WIN32
98             TlsSetValue(dwTlsIndex, new_value);
99 #else
100             pthread_setspecific(key, new_value);
101 #endif
102             if (old_value) {
103                 delete old_value;
104             }
105         }
106     };
107
108 } /* namespace os */
109
110 #endif /* _OS_THREAD_HPP_ */