| OLD | NEW |
| 1 //===- subzero/src/IceTLS.h - thread_local workaround -----------*- C++ -*-===// | 1 //===- subzero/src/IceTLS.h - thread_local workaround -----------*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 /// | 9 /// |
| 10 /// \file | 10 /// \file |
| 11 /// This file defines macros for working around the lack of support for | 11 /// This file defines macros for working around the lack of support for |
| 12 /// thread_local in MacOS 10.6. It assumes std::thread is written in terms of | 12 /// thread_local in MacOS 10.6. It assumes std::thread is written in terms of |
| 13 /// pthread. Define ICE_THREAD_LOCAL_HACK to enable the pthread workarounds. | 13 /// pthread. Define ICE_THREAD_LOCAL_HACK to enable the pthread workarounds. |
| 14 /// | 14 /// |
| 15 //===----------------------------------------------------------------------===// | 15 //===----------------------------------------------------------------------===// |
| 16 | 16 |
| 17 #ifndef SUBZERO_SRC_ICETLS_H | 17 #ifndef SUBZERO_SRC_ICETLS_H |
| 18 #define SUBZERO_SRC_ICETLS_H | 18 #define SUBZERO_SRC_ICETLS_H |
| 19 | 19 |
| 20 #if defined(_MSC_VER) | |
| 21 #define ICE_ATTRIBUTE_TLS __declspec(thread) | |
| 22 #else // !_MSC_VER | |
| 23 #define ICE_ATTRIBUTE_TLS thread_local | |
| 24 #endif // !_MSC_VER | |
| 25 | 20 |
| 26 // Defines 4 macros for unifying thread_local and pthread: | 21 /// |
| 27 // | 22 /// @defgroup /IceTLS Defines 5 macros for unifying thread_local and pthread: |
| 28 // ICE_TLS_DECLARE_FIELD(Type, FieldName): Declare a static thread_local field | 23 /// @{ |
| 29 // inside the current class definition. "Type" needs to be a pointer type, such | 24 /// |
| 30 // as int* or class Foo*. | 25 /// \def ICE_TLS_DECLARE_FIELD(Type, FieldName) |
| 31 // | 26 /// Declare a static thread_local field inside the current class definition. |
| 32 // ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName): Define a static | 27 /// "Type" needs to be a pointer type, such as int* or class Foo*. |
| 33 // thread_local field outside of its class definition. The field will | 28 /// |
| 34 // ultimately be initialized to nullptr. | 29 /// \def ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName) |
| 35 // | 30 /// Define a static thread_local field outside of its class definition. The |
| 36 // ICE_TLS_INIT_FIELD(FieldName): Ensure the thread_local field is properly | 31 /// field will ultimately be initialized to nullptr. |
| 37 // initialized. This is intended to be called from within a static method of | 32 /// |
| 38 // the field's class after main() starts (to ensure that the pthread library is | 33 /// \def ICE_TLS_INIT_FIELD(FieldName) |
| 39 // fully initialized) but before any uses of ICE_TLS_GET_FIELD or | 34 /// Ensure the thread_local field is properly initialized. This is intended |
| 40 // ICE_TLS_SET_FIELD. | 35 /// to be called from within a static method of the field's class after main() |
| 41 // | 36 /// starts (to ensure that the pthread library is fully initialized) but before |
| 42 // ICE_TLS_GET_FIELD(Type, FieldName): Read the value of the static | 37 /// any uses of ICE_TLS_GET_FIELD or ICE_TLS_SET_FIELD. |
| 43 // thread_local field. Must be done within the context of its class. | 38 /// |
| 44 // | 39 /// \def ICE_TLS_GET_FIELD(Type, FieldName) |
| 45 // ICE_TLS_SET_FIELD(FieldName, Value): Write a value into the static | 40 /// Read the value of the static thread_local field. Must be done within the |
| 46 // thread_local field. Must be done within the context of its class. | 41 /// context of its class. |
| 42 /// |
| 43 /// \def ICE_TLS_SET_FIELD(FieldName, Value) |
| 44 /// Write a value into the static thread_local field. Must be done within the |
| 45 /// context of its class. |
| 47 | 46 |
| 48 // TODO(stichnot): Limit this define to only the platforms that | 47 /// \todo TODO(stichnot) |
| 49 // absolutely require it. And ideally, eventually remove this hack | 48 /// Limit this define to only the platforms that absolutely require it. And |
| 50 // altogether. | 49 /// ideally, eventually remove this hack altogether. |
| 50 /// |
| 51 |
| 52 /// |
| 53 /// \def ICE_THREAD_LOCAL_HACK |
| 54 /// |
| 51 #define ICE_THREAD_LOCAL_HACK | 55 #define ICE_THREAD_LOCAL_HACK |
| 52 #ifdef ICE_THREAD_LOCAL_HACK | 56 #ifdef ICE_THREAD_LOCAL_HACK |
| 53 | 57 |
| 54 // For a static thread_local field F of a class C, instead of declaring and | 58 // For a static thread_local field F of a class C, instead of declaring and |
| 55 // defining C::F, we create two static fields: | 59 // defining C::F, we create two static fields: |
| 56 // static pthread_key_t F__key; | 60 // static pthread_key_t F__key; |
| 57 // static int F__initStatus; | 61 // static int F__initStatus; |
| 58 // | 62 // |
| 59 // The F__initStatus field is used to hold the result of the | 63 // The F__initStatus field is used to hold the result of the |
| 60 // pthread_key_create() call, where a zero value indicates success, and a | 64 // pthread_key_create() call, where a zero value indicates success, and a |
| (...skipping 18 matching lines...) Expand all Loading... |
| 79 } | 83 } |
| 80 #define ICE_TLS_GET_FIELD(FieldName) \ | 84 #define ICE_TLS_GET_FIELD(FieldName) \ |
| 81 (assert(FieldName##__initStatus == 0), \ | 85 (assert(FieldName##__initStatus == 0), \ |
| 82 static_cast<FieldName##__type>(pthread_getspecific(FieldName##__key))) | 86 static_cast<FieldName##__type>(pthread_getspecific(FieldName##__key))) |
| 83 #define ICE_TLS_SET_FIELD(FieldName, Value) \ | 87 #define ICE_TLS_SET_FIELD(FieldName, Value) \ |
| 84 (assert(FieldName##__initStatus == 0), \ | 88 (assert(FieldName##__initStatus == 0), \ |
| 85 pthread_setspecific(FieldName##__key, (Value))) | 89 pthread_setspecific(FieldName##__key, (Value))) |
| 86 | 90 |
| 87 #else // !ICE_THREAD_LOCAL_HACK | 91 #else // !ICE_THREAD_LOCAL_HACK |
| 88 | 92 |
| 93 #if defined(_MSC_VER) |
| 94 #define ICE_ATTRIBUTE_TLS __declspec(thread) |
| 95 #else // !_MSC_VER |
| 96 #define ICE_ATTRIBUTE_TLS thread_local |
| 97 #endif // !_MSC_VER |
| 98 |
| 99 |
| 89 #define ICE_TLS_DECLARE_FIELD(Type, FieldName) \ | 100 #define ICE_TLS_DECLARE_FIELD(Type, FieldName) \ |
| 90 static ICE_ATTRIBUTE_TLS Type FieldName | 101 static ICE_ATTRIBUTE_TLS Type FieldName |
| 91 #define ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName) \ | 102 #define ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName) \ |
| 92 ICE_ATTRIBUTE_TLS Type ClassName::FieldName = nullptr | 103 ICE_ATTRIBUTE_TLS Type ClassName::FieldName = nullptr |
| 93 #define ICE_TLS_INIT_FIELD(FieldName) | 104 #define ICE_TLS_INIT_FIELD(FieldName) |
| 94 #define ICE_TLS_GET_FIELD(FieldName) (FieldName) | 105 #define ICE_TLS_GET_FIELD(FieldName) (FieldName) |
| 95 #define ICE_TLS_SET_FIELD(FieldName, Value) (FieldName = (Value)) | 106 #define ICE_TLS_SET_FIELD(FieldName, Value) (FieldName = (Value)) |
| 96 | 107 |
| 97 #endif // !ICE_THREAD_LOCAL_HACK | 108 #endif // !ICE_THREAD_LOCAL_HACK |
| 98 | 109 |
| 110 /// |
| 111 /// @} |
| 112 /// |
| 113 |
| 99 #endif // SUBZERO_SRC_ICETLS_H | 114 #endif // SUBZERO_SRC_ICETLS_H |
| OLD | NEW |