OLD | NEW |
---|---|
(Empty) | |
1 //===- subzero/src/IceTLS.h - thread_local workaround -----------*- C++ -*-===// | |
2 // | |
3 // The Subzero Code Generator | |
4 // | |
5 // This file is distributed under the University of Illinois Open Source | |
6 // License. See LICENSE.TXT for details. | |
7 // | |
8 //===----------------------------------------------------------------------===// | |
9 // | |
10 // This file defines macros for working around the lack of support for | |
11 // thread_local in MacOS 10.6. It assumes std::thread is written in | |
12 // terms of pthread. Define ICE_THREAD_LOCAL_HACK to enable the | |
13 // pthread workarounds. | |
14 // | |
15 //===----------------------------------------------------------------------===// | |
16 | |
17 #ifndef SUBZERO_SRC_ICETLS_H | |
18 #define SUBZERO_SRC_ICETLS_H | |
19 | |
20 // Defines 4 macros for unifying thread_local and pthread: | |
21 // | |
22 // ICE_TLS_DECLARE_FIELD(FieldName, Type): Declare a static | |
23 // thread_local field inside the current class definition. "Type" | |
24 // needs to be a pointer type, such as int* or class Foo*. | |
25 // | |
26 // ICE_TLS_DEFINE_FIELD(ClassName, FieldName, Type): Define a static | |
27 // thread_local field outside of its class definition, and initialize | |
28 // it to nullptr. | |
29 // | |
30 // ICE_TLS_GET_FIELD(FieldName, Type): Read the value of the static | |
31 // thread_local field. Must be done within the context of its class. | |
32 // | |
33 // ICE_TLS_SET_FIELD(FieldName, Value): Write a value into the static | |
34 // thread_local field. Must be done within the context of its class. | |
JF
2015/01/24 05:42:47
I have a mild preference for argument order being
Jim Stichnoth
2015/01/24 16:55:33
Done.
| |
35 | |
36 #ifdef ICE_THREAD_LOCAL_HACK | |
37 | |
38 // For a static thread_local field F of a class C, instead of | |
39 // declaring and defining F::C, we create two static fields: | |
40 // static pthread_key_t F__key; | |
41 // static int F__dummy; | |
42 // | |
43 // The F__dummy field is just used to hold the result of the | |
44 // pthread_key_create() call, which is executed as a static | |
45 // initializer. The F__key field is used as the argument to | |
46 // pthread_getspecific() and pthread_setspecific(). | |
47 | |
48 #include <pthread.h> | |
49 | |
50 #define ICE_TLS_DECLARE_FIELD(FieldName, Type) \ | |
51 static pthread_key_t FieldName##__key; \ | |
52 static int FieldName##__dummy | |
53 #define ICE_TLS_DEFINE_FIELD(ClassName, FieldName, Type) \ | |
54 pthread_key_t ClassName::FieldName##__key; \ | |
55 int ClassName::FieldName##__dummy = \ | |
56 pthread_key_create(&ClassName::FieldName##__key, nullptr) | |
JF
2015/01/24 05:42:47
It's not clear from the pthread docs that you can'
Jim Stichnoth
2015/01/24 16:55:33
I don't see how that could be a problem here, unle
| |
57 #define ICE_TLS_GET_FIELD(FieldName, Type) \ | |
58 static_cast<Type>(pthread_getspecific(FieldName##__key)) | |
59 #define ICE_TLS_SET_FIELD(FieldName, Value) \ | |
60 pthread_setspecific(FieldName##__key, (Value)) | |
61 | |
62 #else // !ICE_THREAD_LOCAL_HACK | |
63 | |
64 #define ICE_TLS_DECLARE_FIELD(FieldName, Type) \ | |
65 static thread_local Type FieldName | |
66 #define ICE_TLS_DEFINE_FIELD(ClassName, FieldName, Type) \ | |
67 thread_local Type ClassName::FieldName = nullptr | |
68 #define ICE_TLS_GET_FIELD(FieldName, Type) (FieldName) | |
69 #define ICE_TLS_SET_FIELD(FieldName, Value) (FieldName = (Value)) | |
70 | |
71 #endif // !ICE_THREAD_LOCAL_HACK | |
72 | |
73 #endif // SUBZERO_SRC_ICETLS_H | |
OLD | NEW |