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.

266 lines
7.3 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/synch/tss_index.hpp (originally in MWTlsFns.h, ::SynesisWin)
  3. *
  4. * Purpose: Wrapper class for Win32 TSS key.
  5. *
  6. * Created: 20th 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 winstl/synch/tss_index.hpp
  40. *
  41. * \brief [C++ only] Definition of winstl::tss_index class
  42. * (\ref group__library__synch "Synchronisation" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_SYNCH_HPP_TSS_INDEX
  45. #define WINSTL_INCL_WINSTL_SYNCH_HPP_TSS_INDEX
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_SYNCH_HPP_TSS_INDEX_MAJOR 4
  48. # define WINSTL_VER_WINSTL_SYNCH_HPP_TSS_INDEX_MINOR 0
  49. # define WINSTL_VER_WINSTL_SYNCH_HPP_TSS_INDEX_REVISION 4
  50. # define WINSTL_VER_WINSTL_SYNCH_HPP_TSS_INDEX_EDIT 37
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  56. # include <winstl/winstl.h>
  57. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  58. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  59. # ifndef WINSTL_INCL_WINSTL_SYNCH_ERROR_HPP_EXCEPTIONS
  60. # include <winstl/synch/error/exceptions.hpp>
  61. # endif /* !WINSTL_INCL_WINSTL_SYNCH_ERROR_HPP_EXCEPTIONS */
  62. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  63. /* /////////////////////////////////////////////////////////////////////////
  64. * Namespace
  65. */
  66. #ifndef _WINSTL_NO_NAMESPACE
  67. # if defined(_STLSOFT_NO_NAMESPACE) || \
  68. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  69. /* There is no stlsoft namespace, so must define ::winstl */
  70. namespace winstl
  71. {
  72. # else
  73. /* Define stlsoft::winstl_project */
  74. namespace stlsoft
  75. {
  76. namespace winstl_project
  77. {
  78. # endif /* _STLSOFT_NO_NAMESPACE */
  79. #endif /* !_WINSTL_NO_NAMESPACE */
  80. /* /////////////////////////////////////////////////////////////////////////
  81. * Classes
  82. */
  83. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  84. /** \brief Indicates that a TSS key cannot be allocated.
  85. *
  86. * \note This exception indicates an irrecoverable condition.
  87. *
  88. * \ingroup group__library__synch
  89. */
  90. class tss_exception
  91. : public synchronisation_exception
  92. {
  93. /// \name Types
  94. /// @{
  95. public:
  96. typedef synchronisation_exception parent_class_type;
  97. typedef tss_exception class_type;
  98. typedef parent_class_type::error_code_type error_code_type;
  99. /// @}
  100. /// \name Construction
  101. /// @{
  102. public:
  103. ss_explicit_k tss_exception(error_code_type err)
  104. : parent_class_type("", err)
  105. {}
  106. /// @}
  107. /// \name Accessors
  108. /// @{
  109. public:
  110. virtual char const* what() const stlsoft_throw_0()
  111. {
  112. return "Failed to allocate a TSS key";
  113. }
  114. /// @}
  115. /// \name Not to be implemented
  116. /// @{
  117. private:
  118. class_type& operator =(class_type const&);
  119. /// @}
  120. };
  121. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  122. /** \brief Wrapper for a Win32 TSS key (TLS index).
  123. *
  124. * \ingroup group__library__synch
  125. */
  126. class tss_index
  127. {
  128. /// \name Types
  129. /// @{
  130. public:
  131. /// \brief This class
  132. typedef tss_index class_type;
  133. /// \brief The type of the TSS key
  134. typedef ws_dword_t key_type;
  135. /// \brief The type of the TSS key
  136. ///
  137. /// \deprecated Deprecated in favour of key_type
  138. typedef key_type index_type;
  139. /// \brief The type of the slot values
  140. typedef void* value_type;
  141. /// @}
  142. /// \name Construction
  143. /// @{
  144. public:
  145. /// \brief Allocates a TSS key
  146. ss_explicit_k tss_index()
  147. : m_index(index_create_())
  148. {}
  149. /// \brief Releases the TSS key
  150. ~tss_index() stlsoft_throw_0()
  151. {
  152. index_destroy_(m_index);
  153. }
  154. /// @}
  155. /// \name Operations
  156. /// @{
  157. public:
  158. /// \brief Sets the value in the slot for the current thread
  159. void set_value(value_type value)
  160. {
  161. class_type::set_slot_value_(m_index, value);
  162. }
  163. /// \brief Gets the value in the slot for the current thread
  164. value_type get_value() const
  165. {
  166. return class_type::get_slot_value_(m_index);
  167. }
  168. /// @}
  169. /// \name Accessors
  170. /// @{
  171. public:
  172. /// \brief Implicit conversion operator to the
  173. operator key_type () const
  174. {
  175. return m_index;
  176. }
  177. /// @}
  178. /// \name Implementation
  179. /// @{
  180. private:
  181. static key_type index_create_()
  182. {
  183. key_type key = ::TlsAlloc();
  184. if(0xFFFFFFFF == key)
  185. {
  186. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  187. STLSOFT_THROW_X(tss_exception(::GetLastError()));
  188. #else /* ? STLSOFT_CF_EXCEPTION_SUPPORT */
  189. ::OutputDebugStringA("Fatal: Could not allocate a TSS key!\n");
  190. ::RaiseException(STATUS_NO_MEMORY, EXCEPTION_NONCONTINUABLE, 0, 0);
  191. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  192. }
  193. return key;
  194. }
  195. static void index_destroy_(key_type key)
  196. {
  197. ::TlsFree(key);
  198. }
  199. static void set_slot_value_(key_type key, value_type value)
  200. {
  201. ::TlsSetValue(key, value);
  202. }
  203. static value_type get_slot_value_(key_type key)
  204. {
  205. return ::TlsGetValue(key);
  206. }
  207. /// @}
  208. /// \name Members
  209. /// @{
  210. private:
  211. key_type m_index;
  212. /// @}
  213. /// \name Not to be implemented
  214. /// @{
  215. private:
  216. tss_index(class_type const&);
  217. class_type& operator =(class_type const&);
  218. /// @}
  219. };
  220. /* ////////////////////////////////////////////////////////////////////// */
  221. #ifndef _WINSTL_NO_NAMESPACE
  222. # if defined(_STLSOFT_NO_NAMESPACE) || \
  223. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  224. } // namespace winstl
  225. # else
  226. } // namespace winstl_project
  227. } // namespace stlsoft
  228. # endif /* _STLSOFT_NO_NAMESPACE */
  229. #endif /* !_WINSTL_NO_NAMESPACE */
  230. /* ////////////////////////////////////////////////////////////////////// */
  231. #endif /* WINSTL_INCL_WINSTL_SYNCH_HPP_TSS_INDEX */
  232. /* ///////////////////////////// end of file //////////////////////////// */