Chromium Code Reviews| Index: src/IceTLS.h |
| diff --git a/src/IceTLS.h b/src/IceTLS.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..de5b10c4e735702d3319642b5e347a1b83831311 |
| --- /dev/null |
| +++ b/src/IceTLS.h |
| @@ -0,0 +1,73 @@ |
| +//===- subzero/src/IceTLS.h - thread_local workaround -----------*- C++ -*-===// |
| +// |
| +// The Subzero Code Generator |
| +// |
| +// This file is distributed under the University of Illinois Open Source |
| +// License. See LICENSE.TXT for details. |
| +// |
| +//===----------------------------------------------------------------------===// |
| +// |
| +// This file defines macros for working around the lack of support for |
| +// thread_local in MacOS 10.6. It assumes std::thread is written in |
| +// terms of pthread. Define ICE_THREAD_LOCAL_HACK to enable the |
| +// pthread workarounds. |
| +// |
| +//===----------------------------------------------------------------------===// |
| + |
| +#ifndef SUBZERO_SRC_ICETLS_H |
| +#define SUBZERO_SRC_ICETLS_H |
| + |
| +// Defines 4 macros for unifying thread_local and pthread: |
| +// |
| +// ICE_TLS_DECLARE_FIELD(FieldName, Type): Declare a static |
| +// thread_local field inside the current class definition. "Type" |
| +// needs to be a pointer type, such as int* or class Foo*. |
| +// |
| +// ICE_TLS_DEFINE_FIELD(ClassName, FieldName, Type): Define a static |
| +// thread_local field outside of its class definition, and initialize |
| +// it to nullptr. |
| +// |
| +// ICE_TLS_GET_FIELD(FieldName, Type): Read the value of the static |
| +// thread_local field. Must be done within the context of its class. |
| +// |
| +// ICE_TLS_SET_FIELD(FieldName, Value): Write a value into the static |
| +// 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.
|
| + |
| +#ifdef ICE_THREAD_LOCAL_HACK |
| + |
| +// For a static thread_local field F of a class C, instead of |
| +// declaring and defining F::C, we create two static fields: |
| +// static pthread_key_t F__key; |
| +// static int F__dummy; |
| +// |
| +// The F__dummy field is just used to hold the result of the |
| +// pthread_key_create() call, which is executed as a static |
| +// initializer. The F__key field is used as the argument to |
| +// pthread_getspecific() and pthread_setspecific(). |
| + |
| +#include <pthread.h> |
| + |
| +#define ICE_TLS_DECLARE_FIELD(FieldName, Type) \ |
| + static pthread_key_t FieldName##__key; \ |
| + static int FieldName##__dummy |
| +#define ICE_TLS_DEFINE_FIELD(ClassName, FieldName, Type) \ |
| + pthread_key_t ClassName::FieldName##__key; \ |
| + int ClassName::FieldName##__dummy = \ |
| + 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
|
| +#define ICE_TLS_GET_FIELD(FieldName, Type) \ |
| + static_cast<Type>(pthread_getspecific(FieldName##__key)) |
| +#define ICE_TLS_SET_FIELD(FieldName, Value) \ |
| + pthread_setspecific(FieldName##__key, (Value)) |
| + |
| +#else // !ICE_THREAD_LOCAL_HACK |
| + |
| +#define ICE_TLS_DECLARE_FIELD(FieldName, Type) \ |
| + static thread_local Type FieldName |
| +#define ICE_TLS_DEFINE_FIELD(ClassName, FieldName, Type) \ |
| + thread_local Type ClassName::FieldName = nullptr |
| +#define ICE_TLS_GET_FIELD(FieldName, Type) (FieldName) |
| +#define ICE_TLS_SET_FIELD(FieldName, Value) (FieldName = (Value)) |
| + |
| +#endif // !ICE_THREAD_LOCAL_HACK |
| + |
| +#endif // SUBZERO_SRC_ICETLS_H |