You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
7.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: unixstl/synch/tss_index.hpp
  3. *
  4. * Purpose: Wrapper class for UNIX PThreads TSS key.
  5. *
  6. * Created: 21st January 1999
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1999-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file unixstl/synch/tss_index.hpp
  40. *
  41. * \brief [C++ only] Definition of the unixstl::tss_index class
  42. * (\ref group__library__synch "Synchronisation" Library).
  43. */
  44. #ifndef UNIXSTL_INCL_UNIXSTL_SYNCH_HPP_TSS_INDEX
  45. #define UNIXSTL_INCL_UNIXSTL_SYNCH_HPP_TSS_INDEX
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define UNIXSTL_VER_UNIXSTL_SYNCH_HPP_TSS_INDEX_MAJOR 3
  48. # define UNIXSTL_VER_UNIXSTL_SYNCH_HPP_TSS_INDEX_MINOR 1
  49. # define UNIXSTL_VER_UNIXSTL_SYNCH_HPP_TSS_INDEX_REVISION 3
  50. # define UNIXSTL_VER_UNIXSTL_SYNCH_HPP_TSS_INDEX_EDIT 49
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
  56. # include <unixstl/unixstl.h>
  57. #endif /* !UNIXSTL_INCL_UNIXSTL_H_UNIXSTL */
  58. #ifndef UNIXSTL_INCL_UNIXSTL_SYNCH_UTIL_H_FEATURES
  59. # include <unixstl/synch/util/features.h>
  60. #endif /* !UNIXSTL_INCL_UNIXSTL_SYNCH_UTIL_H_FEATURES */
  61. #ifndef UNIXSTL_USING_PTHREADS
  62. # error unixstl/synch/tss_index.hpp cannot be included in non-multithreaded compilation. _REENTRANT and/or _POSIX_THREADS must be defined
  63. #endif /* !UNIXSTL_USING_PTHREADS */
  64. #ifndef STLSOFT_CF_EXCEPTION_SUPPORT
  65. # error unixstl/synch/tss_index.hpp cannot be compiled without exception-support enabled
  66. #endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */
  67. #ifndef UNIXSTL_INCL_UNIXSTL_SYNCH_ERROR_HPP_EXCEPTIONS
  68. # include <unixstl/synch/error/exceptions.hpp>
  69. #endif /* !UNIXSTL_INCL_UNIXSTL_SYNCH_ERROR_HPP_EXCEPTIONS */
  70. #ifndef STLSOFT_INCL_H_PTHREAD
  71. # define STLSOFT_INCL_H_PTHREAD
  72. # include <pthread.h>
  73. #endif /* !STLSOFT_INCL_H_PTHREAD */
  74. /* /////////////////////////////////////////////////////////////////////////
  75. * Namespace
  76. */
  77. #ifndef _UNIXSTL_NO_NAMESPACE
  78. # if defined(_STLSOFT_NO_NAMESPACE) || \
  79. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  80. /* There is no stlsoft namespace, so must define ::unixstl */
  81. namespace unixstl
  82. {
  83. # else
  84. /* Define stlsoft::unixstl_project */
  85. namespace stlsoft
  86. {
  87. namespace unixstl_project
  88. {
  89. # endif /* _STLSOFT_NO_NAMESPACE */
  90. #endif /* !_UNIXSTL_NO_NAMESPACE */
  91. /* /////////////////////////////////////////////////////////////////////////
  92. * Classes
  93. */
  94. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  95. /** \brief Indicates that a TSS key cannot be allocated.
  96. *
  97. * \note This exception indicates an unrecoverable condition.
  98. *
  99. * \ingroup group__library__synch
  100. */
  101. class tss_exception
  102. : public synchronisation_exception
  103. {
  104. /// \name Types
  105. /// @{
  106. public:
  107. typedef synchronisation_exception parent_class_type;
  108. typedef tss_exception class_type;
  109. typedef parent_class_type::error_code_type error_code_type;
  110. /// @}
  111. /// \name Construction
  112. /// @{
  113. public:
  114. ss_explicit_k tss_exception(error_code_type err)
  115. : parent_class_type("", err)
  116. {}
  117. /// @}
  118. /// \name Accessors
  119. /// @{
  120. public:
  121. virtual char const* what() const stlsoft_throw_0()
  122. {
  123. return "Failed to allocate a TSS key";
  124. }
  125. /// @}
  126. /// \name Not to be implemented
  127. /// @{
  128. private:
  129. class_type& operator =(class_type const&);
  130. /// @}
  131. };
  132. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  133. /** \brief Wrapper for a UNIX PThreads TSS key.
  134. *
  135. * \ingroup group__library__synch
  136. */
  137. class tss_index
  138. {
  139. /// \name Types
  140. /// @{
  141. public:
  142. /// \brief This class
  143. typedef tss_index class_type;
  144. /// \brief The type of the TSS key
  145. typedef pthread_key_t key_type;
  146. /// \brief The type of the TSS key
  147. ///
  148. /// \deprecated Deprecated in favour of key_type
  149. typedef key_type index_type;
  150. /// \brief The type of the slot values
  151. typedef void* value_type;
  152. /// @}
  153. /// \name Construction
  154. /// @{
  155. public:
  156. /// \brief Allocates a TSS key
  157. ss_explicit_k tss_index()
  158. : m_index(index_create_())
  159. {}
  160. /// \brief Releases the TSS key
  161. ~tss_index() stlsoft_throw_0()
  162. {
  163. index_destroy_(m_index);
  164. }
  165. /// @}
  166. /// \name Operations
  167. /// @{
  168. public:
  169. /// \brief Sets the value in the slot for the current thread
  170. void set_value(value_type value)
  171. {
  172. class_type::set_slot_value_(m_index, value);
  173. }
  174. /// \brief Gets the value in the slot for the current thread
  175. value_type get_value() const
  176. {
  177. return class_type::get_slot_value_(m_index);
  178. }
  179. /// @}
  180. /// \name Accessors
  181. /// @{
  182. public:
  183. /// \brief Implicit conversion operator to the
  184. operator key_type () const
  185. {
  186. return m_index;
  187. }
  188. /// @}
  189. /// \name Implementation
  190. /// @{
  191. private:
  192. static key_type index_create_()
  193. {
  194. key_type key;
  195. int res = ::pthread_key_create(&key, NULL);
  196. if(0 != res)
  197. {
  198. STLSOFT_THROW_X(tss_exception(res));
  199. }
  200. return key;
  201. }
  202. static void index_destroy_(key_type key)
  203. {
  204. ::pthread_key_delete(key);
  205. }
  206. static void set_slot_value_(key_type key, value_type value)
  207. {
  208. ::pthread_setspecific(key, value);
  209. }
  210. static value_type get_slot_value_(key_type key)
  211. {
  212. return ::pthread_getspecific(key);
  213. }
  214. /// @}
  215. /// \name Members
  216. /// @{
  217. private:
  218. key_type m_index;
  219. /// @}
  220. /// \name Not to be implemented
  221. /// @{
  222. private:
  223. tss_index(class_type const&);
  224. class_type& operator =(class_type const&);
  225. /// @}
  226. };
  227. /* ////////////////////////////////////////////////////////////////////// */
  228. #ifndef _UNIXSTL_NO_NAMESPACE
  229. # if defined(_STLSOFT_NO_NAMESPACE) || \
  230. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  231. } // namespace unixstl
  232. # else
  233. } // namespace unixstl_project
  234. } // namespace stlsoft
  235. # endif /* _STLSOFT_NO_NAMESPACE */
  236. #endif /* !_UNIXSTL_NO_NAMESPACE */
  237. /* ////////////////////////////////////////////////////////////////////// */
  238. #endif /* UNIXSTL_INCL_UNIXSTL_SYNCH_HPP_TSS_INDEX */
  239. /* ///////////////////////////// end of file //////////////////////////// */