| 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 | 12 /// thread_local in MacOS 10.6. It assumes std::thread is written in terms of |
| 13 /// terms of pthread. Define ICE_THREAD_LOCAL_HACK to enable the | 13 /// pthread. Define ICE_THREAD_LOCAL_HACK to enable the pthread workarounds. |
| 14 /// pthread workarounds. | |
| 15 /// | 14 /// |
| 16 //===----------------------------------------------------------------------===// | 15 //===----------------------------------------------------------------------===// |
| 17 | 16 |
| 18 #ifndef SUBZERO_SRC_ICETLS_H | 17 #ifndef SUBZERO_SRC_ICETLS_H |
| 19 #define SUBZERO_SRC_ICETLS_H | 18 #define SUBZERO_SRC_ICETLS_H |
| 20 | 19 |
| 21 #if defined(_MSC_VER) | 20 #if defined(_MSC_VER) |
| 22 #define ICE_ATTRIBUTE_TLS __declspec(thread) | 21 #define ICE_ATTRIBUTE_TLS __declspec(thread) |
| 23 #else // !_MSC_VER | 22 #else // !_MSC_VER |
| 24 #define ICE_ATTRIBUTE_TLS thread_local | 23 #define ICE_ATTRIBUTE_TLS thread_local |
| 25 #endif // !_MSC_VER | 24 #endif // !_MSC_VER |
| 26 | 25 |
| 27 // Defines 4 macros for unifying thread_local and pthread: | 26 // Defines 4 macros for unifying thread_local and pthread: |
| 28 // | 27 // |
| 29 // ICE_TLS_DECLARE_FIELD(Type, FieldName): Declare a static | 28 // ICE_TLS_DECLARE_FIELD(Type, FieldName): Declare a static thread_local field |
| 30 // thread_local field inside the current class definition. "Type" | 29 // inside the current class definition. "Type" needs to be a pointer type, such |
| 31 // needs to be a pointer type, such as int* or class Foo*. | 30 // as int* or class Foo*. |
| 32 // | 31 // |
| 33 // ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName): Define a static | 32 // ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName): Define a static |
| 34 // thread_local field outside of its class definition. The field will | 33 // thread_local field outside of its class definition. The field will |
| 35 // ultimately be initialized to nullptr. | 34 // ultimately be initialized to nullptr. |
| 36 // | 35 // |
| 37 // ICE_TLS_INIT_FIELD(FieldName): Ensure the thread_local field is | 36 // ICE_TLS_INIT_FIELD(FieldName): Ensure the thread_local field is properly |
| 38 // properly initialized. This is intended to be called from within a | 37 // initialized. This is intended to be called from within a static method of |
| 39 // static method of the field's class after main() starts (to ensure | 38 // the field's class after main() starts (to ensure that the pthread library is |
| 40 // that the pthread library is fully initialized) but before any uses | 39 // fully initialized) but before any uses of ICE_TLS_GET_FIELD or |
| 41 // of ICE_TLS_GET_FIELD or ICE_TLS_SET_FIELD. | 40 // ICE_TLS_SET_FIELD. |
| 42 // | 41 // |
| 43 // ICE_TLS_GET_FIELD(Type, FieldName): Read the value of the static | 42 // ICE_TLS_GET_FIELD(Type, FieldName): Read the value of the static |
| 44 // thread_local field. Must be done within the context of its class. | 43 // thread_local field. Must be done within the context of its class. |
| 45 // | 44 // |
| 46 // ICE_TLS_SET_FIELD(FieldName, Value): Write a value into the static | 45 // ICE_TLS_SET_FIELD(FieldName, Value): Write a value into the static |
| 47 // thread_local field. Must be done within the context of its class. | 46 // thread_local field. Must be done within the context of its class. |
| 48 | 47 |
| 49 // TODO(stichnot): Limit this define to only the platforms that | 48 // TODO(stichnot): Limit this define to only the platforms that |
| 50 // absolutely require it. And ideally, eventually remove this hack | 49 // absolutely require it. And ideally, eventually remove this hack |
| 51 // altogether. | 50 // altogether. |
| 52 #define ICE_THREAD_LOCAL_HACK | 51 #define ICE_THREAD_LOCAL_HACK |
| 53 #ifdef ICE_THREAD_LOCAL_HACK | 52 #ifdef ICE_THREAD_LOCAL_HACK |
| 54 | 53 |
| 55 // For a static thread_local field F of a class C, instead of | 54 // For a static thread_local field F of a class C, instead of declaring and |
| 56 // declaring and defining C::F, we create two static fields: | 55 // defining C::F, we create two static fields: |
| 57 // static pthread_key_t F__key; | 56 // static pthread_key_t F__key; |
| 58 // static int F__initStatus; | 57 // static int F__initStatus; |
| 59 // | 58 // |
| 60 // The F__initStatus field is used to hold the result of the | 59 // The F__initStatus field is used to hold the result of the |
| 61 // pthread_key_create() call, where a zero value indicates success, | 60 // pthread_key_create() call, where a zero value indicates success, and a |
| 62 // and a nonzero value indicates failure or that ICE_TLS_INIT_FIELD() | 61 // nonzero value indicates failure or that ICE_TLS_INIT_FIELD() was never |
| 63 // was never called. | 62 // called. The F__key field is used as the argument to pthread_getspecific() |
| 64 // The F__key field is used as the argument to | 63 // and pthread_setspecific(). |
| 65 // pthread_getspecific() and pthread_setspecific(). | |
| 66 | 64 |
| 67 #include <pthread.h> | 65 #include <pthread.h> |
| 68 | 66 |
| 69 #define ICE_TLS_DECLARE_FIELD(Type, FieldName) \ | 67 #define ICE_TLS_DECLARE_FIELD(Type, FieldName) \ |
| 70 using FieldName##__type = Type; \ | 68 using FieldName##__type = Type; \ |
| 71 static pthread_key_t FieldName##__key; \ | 69 static pthread_key_t FieldName##__key; \ |
| 72 static int FieldName##__initStatus | 70 static int FieldName##__initStatus |
| 73 #define ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName) \ | 71 #define ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName) \ |
| 74 pthread_key_t ClassName::FieldName##__key; \ | 72 pthread_key_t ClassName::FieldName##__key; \ |
| 75 int ClassName::FieldName##__initStatus = 1 | 73 int ClassName::FieldName##__initStatus = 1 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 92 static ICE_ATTRIBUTE_TLS Type FieldName | 90 static ICE_ATTRIBUTE_TLS Type FieldName |
| 93 #define ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName) \ | 91 #define ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName) \ |
| 94 ICE_ATTRIBUTE_TLS Type ClassName::FieldName = nullptr | 92 ICE_ATTRIBUTE_TLS Type ClassName::FieldName = nullptr |
| 95 #define ICE_TLS_INIT_FIELD(FieldName) | 93 #define ICE_TLS_INIT_FIELD(FieldName) |
| 96 #define ICE_TLS_GET_FIELD(FieldName) (FieldName) | 94 #define ICE_TLS_GET_FIELD(FieldName) (FieldName) |
| 97 #define ICE_TLS_SET_FIELD(FieldName, Value) (FieldName = (Value)) | 95 #define ICE_TLS_SET_FIELD(FieldName, Value) (FieldName = (Value)) |
| 98 | 96 |
| 99 #endif // !ICE_THREAD_LOCAL_HACK | 97 #endif // !ICE_THREAD_LOCAL_HACK |
| 100 | 98 |
| 101 #endif // SUBZERO_SRC_ICETLS_H | 99 #endif // SUBZERO_SRC_ICETLS_H |
| OLD | NEW |