Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// This file defines macros for working around the lack of support for |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 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. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 14 | /// |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #ifndef SUBZERO_SRC_ICETLS_H |
| 18 | #define SUBZERO_SRC_ICETLS_H |
| 19 | |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 20 | |
Reed Kotler | af5d44c | 2015-11-09 16:52:56 -0800 | [diff] [blame^] | 21 | /// |
| 22 | /// @defgroup /IceTLS Defines 5 macros for unifying thread_local and pthread: |
| 23 | /// @{ |
| 24 | /// |
| 25 | /// \def ICE_TLS_DECLARE_FIELD(Type, FieldName) |
| 26 | /// Declare a static thread_local field inside the current class definition. |
| 27 | /// "Type" needs to be a pointer type, such as int* or class Foo*. |
| 28 | /// |
| 29 | /// \def ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName) |
| 30 | /// Define a static thread_local field outside of its class definition. The |
| 31 | /// field will ultimately be initialized to nullptr. |
| 32 | /// |
| 33 | /// \def ICE_TLS_INIT_FIELD(FieldName) |
| 34 | /// Ensure the thread_local field is properly initialized. This is intended |
| 35 | /// to be called from within a static method of the field's class after main() |
| 36 | /// starts (to ensure that the pthread library is fully initialized) but before |
| 37 | /// any uses of ICE_TLS_GET_FIELD or ICE_TLS_SET_FIELD. |
| 38 | /// |
| 39 | /// \def ICE_TLS_GET_FIELD(Type, FieldName) |
| 40 | /// Read the value of the static thread_local field. Must be done within the |
| 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. |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 46 | |
Reed Kotler | af5d44c | 2015-11-09 16:52:56 -0800 | [diff] [blame^] | 47 | /// \todo TODO(stichnot) |
| 48 | /// Limit this define to only the platforms that absolutely require it. And |
| 49 | /// ideally, eventually remove this hack altogether. |
| 50 | /// |
| 51 | |
| 52 | /// |
| 53 | /// \def ICE_THREAD_LOCAL_HACK |
| 54 | /// |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 55 | #define ICE_THREAD_LOCAL_HACK |
| 56 | #ifdef ICE_THREAD_LOCAL_HACK |
| 57 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 58 | // For a static thread_local field F of a class C, instead of declaring and |
| 59 | // defining C::F, we create two static fields: |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 60 | // static pthread_key_t F__key; |
| 61 | // static int F__initStatus; |
| 62 | // |
| 63 | // The F__initStatus field is used to hold the result of the |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 64 | // pthread_key_create() call, where a zero value indicates success, and a |
| 65 | // nonzero value indicates failure or that ICE_TLS_INIT_FIELD() was never |
| 66 | // called. The F__key field is used as the argument to pthread_getspecific() |
| 67 | // and pthread_setspecific(). |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 68 | |
| 69 | #include <pthread.h> |
| 70 | |
| 71 | #define ICE_TLS_DECLARE_FIELD(Type, FieldName) \ |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 72 | using FieldName##__type = Type; \ |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 73 | static pthread_key_t FieldName##__key; \ |
| 74 | static int FieldName##__initStatus |
| 75 | #define ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName) \ |
| 76 | pthread_key_t ClassName::FieldName##__key; \ |
JF Bastien | 8427ea2 | 2015-01-27 12:56:49 -0800 | [diff] [blame] | 77 | int ClassName::FieldName##__initStatus = 1 |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 78 | #define ICE_TLS_INIT_FIELD(FieldName) \ |
| 79 | if (FieldName##__initStatus) { \ |
| 80 | FieldName##__initStatus = pthread_key_create(&FieldName##__key, nullptr); \ |
| 81 | if (FieldName##__initStatus) \ |
| 82 | llvm::report_fatal_error("Failed to create pthread key"); \ |
| 83 | } |
| 84 | #define ICE_TLS_GET_FIELD(FieldName) \ |
| 85 | (assert(FieldName##__initStatus == 0), \ |
| 86 | static_cast<FieldName##__type>(pthread_getspecific(FieldName##__key))) |
| 87 | #define ICE_TLS_SET_FIELD(FieldName, Value) \ |
| 88 | (assert(FieldName##__initStatus == 0), \ |
| 89 | pthread_setspecific(FieldName##__key, (Value))) |
| 90 | |
| 91 | #else // !ICE_THREAD_LOCAL_HACK |
| 92 | |
Reed Kotler | af5d44c | 2015-11-09 16:52:56 -0800 | [diff] [blame^] | 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 | |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 100 | #define ICE_TLS_DECLARE_FIELD(Type, FieldName) \ |
| 101 | static ICE_ATTRIBUTE_TLS Type FieldName |
| 102 | #define ICE_TLS_DEFINE_FIELD(Type, ClassName, FieldName) \ |
| 103 | ICE_ATTRIBUTE_TLS Type ClassName::FieldName = nullptr |
| 104 | #define ICE_TLS_INIT_FIELD(FieldName) |
| 105 | #define ICE_TLS_GET_FIELD(FieldName) (FieldName) |
| 106 | #define ICE_TLS_SET_FIELD(FieldName, Value) (FieldName = (Value)) |
| 107 | |
| 108 | #endif // !ICE_THREAD_LOCAL_HACK |
| 109 | |
Reed Kotler | af5d44c | 2015-11-09 16:52:56 -0800 | [diff] [blame^] | 110 | /// |
| 111 | /// @} |
| 112 | /// |
| 113 | |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 114 | #endif // SUBZERO_SRC_ICETLS_H |