tls.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // -*- C++ -*-
  2. // Copyright (C) 2010-2017, Vaclav Haisman. All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without modifica-
  5. // tion, are permitted provided that the following conditions are met:
  6. //
  7. // 1. Redistributions of source code must retain the above copyright notice,
  8. // this list of conditions and the following disclaimer.
  9. //
  10. // 2. Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. //
  14. // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  15. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  17. // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  18. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  19. // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  20. // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. #ifndef LOG4CPLUS_THREAD_IMPL_TLS_H
  25. #define LOG4CPLUS_THREAD_IMPL_TLS_H
  26. #include <log4cplus/config.hxx>
  27. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  28. #pragma once
  29. #endif
  30. #include <new>
  31. #include <cassert>
  32. #include <system_error>
  33. #if ! defined (INSIDE_LOG4CPLUS)
  34. # error "This header must not be be used outside log4cplus' implementation files."
  35. #endif
  36. #ifdef LOG4CPLUS_USE_PTHREADS
  37. # include <pthread.h>
  38. #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
  39. # include <log4cplus/config/windowsh-inc.h>
  40. #elif defined (LOG4CPLUS_SINGLE_THREADED)
  41. # include <vector>
  42. #endif
  43. namespace log4cplus { namespace thread { namespace impl {
  44. typedef void * tls_value_type;
  45. #ifdef LOG4CPLUS_USE_PTHREADS
  46. typedef pthread_key_t * tls_key_type;
  47. typedef void (* tls_init_cleanup_func_type)(void *);
  48. #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
  49. typedef DWORD tls_key_type;
  50. typedef PFLS_CALLBACK_FUNCTION tls_init_cleanup_func_type;
  51. #elif defined (LOG4CPLUS_SINGLE_THREADED)
  52. typedef std::size_t tls_key_type;
  53. typedef void (* tls_init_cleanup_func_type)(void *);
  54. #endif
  55. inline tls_key_type tls_init (tls_init_cleanup_func_type);
  56. inline tls_value_type tls_get_value (tls_key_type);
  57. inline void tls_set_value (tls_key_type, tls_value_type);
  58. inline void tls_cleanup (tls_key_type);
  59. #if defined (LOG4CPLUS_USE_PTHREADS)
  60. tls_key_type
  61. tls_init (tls_init_cleanup_func_type cleanupfunc)
  62. {
  63. pthread_key_t * key = new pthread_key_t;
  64. int ret = pthread_key_create (key, cleanupfunc);
  65. if (LOG4CPLUS_UNLIKELY (ret != 0))
  66. throw std::system_error(ret, std::system_category (),
  67. "pthread_key_create() failed");
  68. return key;
  69. }
  70. tls_value_type
  71. tls_get_value (tls_key_type key)
  72. {
  73. return pthread_getspecific (*key);
  74. }
  75. void
  76. tls_set_value (tls_key_type key, tls_value_type value)
  77. {
  78. pthread_setspecific(*key, value);
  79. }
  80. void
  81. tls_cleanup (tls_key_type key)
  82. {
  83. pthread_key_delete (*key);
  84. delete key;
  85. }
  86. #elif defined (LOG4CPLUS_USE_WIN32_THREADS)
  87. tls_key_type
  88. tls_init (tls_init_cleanup_func_type cleanupfunc)
  89. {
  90. DWORD const slot = FlsAlloc (cleanupfunc);
  91. if (LOG4CPLUS_UNLIKELY (slot == FLS_OUT_OF_INDEXES))
  92. {
  93. DWORD const eno = GetLastError ();
  94. throw std::system_error (static_cast<int>(eno),
  95. std::system_category (), "FlsAlloc() failed");
  96. }
  97. return slot;
  98. }
  99. tls_value_type tls_get_value (tls_key_type k)
  100. {
  101. return FlsGetValue (k);
  102. }
  103. void
  104. tls_set_value (tls_key_type k, tls_value_type value)
  105. {
  106. FlsSetValue (k, value);
  107. }
  108. void
  109. tls_cleanup (tls_key_type k)
  110. {
  111. FlsFree (k);
  112. }
  113. #elif defined (LOG4CPLUS_SINGLE_THREADED)
  114. extern std::vector<tls_value_type> * tls_single_threaded_values;
  115. tls_key_type
  116. tls_init (tls_init_cleanup_func_type)
  117. {
  118. if (! tls_single_threaded_values)
  119. tls_single_threaded_values = new std::vector<tls_value_type>;
  120. tls_key_type key = tls_single_threaded_values->size ();
  121. tls_single_threaded_values->resize (key + 1);
  122. return key;
  123. }
  124. tls_value_type
  125. tls_get_value (tls_key_type k)
  126. {
  127. assert (k < tls_single_threaded_values->size ());
  128. return (*tls_single_threaded_values)[k];
  129. }
  130. void
  131. tls_set_value (tls_key_type k, tls_value_type val)
  132. {
  133. assert (k < tls_single_threaded_values->size ());
  134. (*tls_single_threaded_values)[k] = val;
  135. }
  136. void
  137. tls_cleanup (tls_key_type k)
  138. {
  139. assert (k < tls_single_threaded_values->size ());
  140. (*tls_single_threaded_values)[k] = 0;
  141. }
  142. #endif
  143. } } } // namespace log4cplus { namespace thread { namespace impl {
  144. #endif // LOG4CPLUS_THREAD_IMPL_TLS_H