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