25 #ifndef LOG4CPLUS_THREAD_IMPL_TLS_H
26 #define LOG4CPLUS_THREAD_IMPL_TLS_H
30 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
36 #include <system_error>
38 #if ! defined (INSIDE_LOG4CPLUS)
39 # error "This header must not be be used outside log4cplus' implementation files."
42 #ifdef LOG4CPLUS_USE_PTHREADS
45 #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
48 #elif defined (LOG4CPLUS_SINGLE_THREADED)
54 namespace log4cplus {
namespace thread {
namespace impl {
59 #ifdef LOG4CPLUS_USE_PTHREADS
60 typedef pthread_key_t * tls_key_type;
61 typedef void (* tls_init_cleanup_func_type)(
void *);
63 #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
64 typedef DWORD tls_key_type;
65 typedef PFLS_CALLBACK_FUNCTION tls_init_cleanup_func_type;
67 #elif defined (LOG4CPLUS_SINGLE_THREADED)
68 typedef std::size_t tls_key_type;
69 typedef void (* tls_init_cleanup_func_type)(
void *);
74 inline tls_key_type
tls_init (tls_init_cleanup_func_type);
80 #if defined (LOG4CPLUS_USE_PTHREADS)
82 tls_init (tls_init_cleanup_func_type cleanupfunc)
84 pthread_key_t * key =
new pthread_key_t;
85 int ret = pthread_key_create (key, cleanupfunc);
87 throw std::system_error(ret, std::system_category (),
88 "pthread_key_create() failed");
96 return pthread_getspecific (*key);
103 pthread_setspecific(*key, value);
110 pthread_key_delete (*key);
115 #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
117 tls_init (tls_init_cleanup_func_type cleanupfunc)
119 DWORD
const slot = FlsAlloc (cleanupfunc);
122 DWORD
const eno = GetLastError ();
123 throw std::system_error (
static_cast<int>(eno),
124 std::system_category (),
"FlsAlloc() failed");
132 return FlsGetValue (k);
139 FlsSetValue (k, value);
150 #elif defined (LOG4CPLUS_SINGLE_THREADED)
151 extern std::vector<tls_value_type> * tls_single_threaded_values;
155 tls_init (tls_init_cleanup_func_type)
157 if (! tls_single_threaded_values)
158 tls_single_threaded_values =
new std::vector<tls_value_type>;
159 tls_key_type key = tls_single_threaded_values->size ();
160 tls_single_threaded_values->resize (key + 1);
168 assert (k < tls_single_threaded_values->size ());
169 return (*tls_single_threaded_values)[k];
176 assert (k < tls_single_threaded_values->size ());
177 (*tls_single_threaded_values)[k] = val;
184 assert (k < tls_single_threaded_values->size ());
185 (*tls_single_threaded_values)[k] = 0;
#define LOG4CPLUS_UNLIKELY(cond)
tls_key_type tls_init(tls_init_cleanup_func_type)
void tls_set_value(tls_key_type, tls_value_type)
tls_value_type tls_get_value(tls_key_type)
void tls_cleanup(tls_key_type)