1 /* 2 Copyright 2005-2014 Intel Corporation. All Rights Reserved. 3 4 This file is part of Threading Building Blocks. Threading Building Blocks is free software; 5 you can redistribute it and/or modify it under the terms of the GNU General Public License 6 version 2 as published by the Free Software Foundation. Threading Building Blocks is 7 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the 8 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 See the GNU General Public License for more details. You should have received a copy of 10 the GNU General Public License along with Threading Building Blocks; if not, write to the 11 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 12 13 As a special exception, you may use this file as part of a free software library without 14 restriction. Specifically, if other files instantiate templates or use macros or inline 15 functions from this file, or you compile this file and link it with other files to produce 16 an executable, this file does not by itself cause the resulting executable to be covered 17 by the GNU General Public License. This exception does not however invalidate any other 18 reasons why the executable file might be covered by the GNU General Public License. 19 */ 20 21 #ifndef _TBB_tls_H 22 #define _TBB_tls_H 23 24 #if USE_PTHREAD 25 #include <pthread.h> 26 #else /* assume USE_WINTHREAD */ 27 #include "tbb/machine/windows_api.h" 28 #endif 29 30 namespace tbb { 31 32 namespace internal { 33 34 typedef void (*tls_dtor_t)(void*); 35 36 //! Basic cross-platform wrapper class for TLS operations. 37 template <typename T> 38 class basic_tls { 39 #if USE_PTHREAD 40 typedef pthread_key_t tls_key_t; 41 public: 42 int create( tls_dtor_t dtor = NULL ) { 43 return pthread_key_create(&my_key, dtor); 44 } destroy()45 int destroy() { return pthread_key_delete(my_key); } set(T value)46 void set( T value ) { pthread_setspecific(my_key, (void*)value); } get()47 T get() { return (T)pthread_getspecific(my_key); } 48 #else /* USE_WINTHREAD */ 49 typedef DWORD tls_key_t; 50 public: 51 #if !__TBB_WIN8UI_SUPPORT 52 int create() { 53 tls_key_t tmp = TlsAlloc(); 54 if( tmp==TLS_OUT_OF_INDEXES ) 55 return TLS_OUT_OF_INDEXES; 56 my_key = tmp; 57 return 0; 58 } 59 int destroy() { TlsFree(my_key); my_key=0; return 0; } 60 void set( T value ) { TlsSetValue(my_key, (LPVOID)value); } 61 T get() { return (T)TlsGetValue(my_key); } 62 #else /*!__TBB_WIN8UI_SUPPORT*/ 63 int create() { 64 tls_key_t tmp = FlsAlloc(NULL); 65 if( tmp== (DWORD)0xFFFFFFFF ) 66 return (DWORD)0xFFFFFFFF; 67 my_key = tmp; 68 return 0; 69 } 70 int destroy() { FlsFree(my_key); my_key=0; return 0; } 71 void set( T value ) { FlsSetValue(my_key, (LPVOID)value); } 72 T get() { return (T)FlsGetValue(my_key); } 73 #endif /* !__TBB_WIN8UI_SUPPORT */ 74 #endif /* USE_WINTHREAD */ 75 private: 76 tls_key_t my_key; 77 }; 78 79 //! More advanced TLS support template class. 80 /** It supports RAII and to some extent mimic __declspec(thread) variables. */ 81 template <typename T> 82 class tls : public basic_tls<T> { 83 typedef basic_tls<T> base; 84 public: tls()85 tls() { base::create(); } ~tls()86 ~tls() { base::destroy(); } 87 T operator=(T value) { base::set(value); return value; } T()88 operator T() { return base::get(); } 89 }; 90 91 template <typename T> 92 class tls<T*> : basic_tls<T*> { 93 typedef basic_tls<T*> base; internal_dtor(void * ptr)94 static void internal_dtor(void* ptr) { 95 if (ptr) delete (T*)ptr; 96 } internal_get()97 T* internal_get() { 98 T* result = base::get(); 99 if (!result) { 100 result = new T; 101 base::set(result); 102 } 103 return result; 104 } 105 public: tls()106 tls() { 107 #if USE_PTHREAD 108 base::create( internal_dtor ); 109 #else 110 base::create(); 111 #endif 112 } ~tls()113 ~tls() { base::destroy(); } 114 T* operator=(T* value) { base::set(value); return value; } 115 operator T*() { return internal_get(); } 116 T* operator->() { return internal_get(); } 117 T& operator*() { return *internal_get(); } 118 }; 119 120 } // namespace internal 121 122 } // namespace tbb 123 124 #endif /* _TBB_tls_H */ 125