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.

195 lines
6.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: platformstl/synch/process_mutex.hpp
  3. *
  4. * Purpose: Definition of the process_mutex type.
  5. *
  6. * Created: 20th March 2005
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-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 platformstl/synch/process_mutex.hpp
  40. *
  41. * \brief [C++ only] Definition of the platformstl::process_mutex type
  42. * (\ref group__library__synch "Synchronisation" Library).
  43. */
  44. #ifndef PLATFORMSTL_INCL_PLATFORMSTL_SYNCH_HPP_PROCESS_MUTEX
  45. #define PLATFORMSTL_INCL_PLATFORMSTL_SYNCH_HPP_PROCESS_MUTEX
  46. /* File version */
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define PLATFORMSTL_VER_PLATFORMSTL_SYNCH_HPP_PROCESS_MUTEX_MAJOR 2
  49. # define PLATFORMSTL_VER_PLATFORMSTL_SYNCH_HPP_PROCESS_MUTEX_MINOR 2
  50. # define PLATFORMSTL_VER_PLATFORMSTL_SYNCH_HPP_PROCESS_MUTEX_REVISION 1
  51. # define PLATFORMSTL_VER_PLATFORMSTL_SYNCH_HPP_PROCESS_MUTEX_EDIT 18
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef PLATFORMSTL_INCL_PLATFORMSTL_HPP_PLATFORMSTL
  57. # include <platformstl/platformstl.hpp>
  58. #endif /* !PLATFORMSTL_INCL_PLATFORMSTL_HPP_PLATFORMSTL */
  59. #if defined(PLATFORMSTL_OS_IS_UNIX)
  60. # ifndef UNIXSTL_INCL_UNIXSTL_SYNCH_HPP_PROCESS_MUTEX
  61. # include <unixstl/synch/process_mutex.hpp>
  62. # endif /* !UNIXSTL_INCL_UNIXSTL_SYNCH_HPP_PROCESS_MUTEX */
  63. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  64. # ifndef WINSTL_INCL_WINSTL_SYNCH_HPP_PROCESS_MUTEX
  65. # include <winstl/synch/process_mutex.hpp>
  66. # endif /* !WINSTL_INCL_WINSTL_SYNCH_HPP_PROCESS_MUTEX */
  67. #else /* ? operating system */
  68. # error Operating system not discriminated
  69. #endif /* operating system */
  70. /* /////////////////////////////////////////////////////////////////////////
  71. * Namespace
  72. */
  73. #if defined(_STLSOFT_NO_NAMESPACE) || \
  74. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  75. /* There is no stlsoft namespace, so must define ::platformstl */
  76. namespace platformstl
  77. {
  78. #else
  79. /* Define stlsoft::platformstl_project */
  80. namespace stlsoft
  81. {
  82. namespace platformstl_project
  83. {
  84. #endif /* _STLSOFT_NO_NAMESPACE */
  85. /* ////////////////////////////////////////////////////////////////////// */
  86. #if defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  87. /** \brief A process mutex class
  88. *
  89. * The class is not actually defined in the
  90. * \link ::platformstl platformstl\endlink namespace. Rather, it
  91. * resolves to the appropriate type for the given platform, relying on
  92. * \ref section__principle__conformance__intersecting_conformance "Intersecting Conformance"
  93. * of the resolved platform-specific types.
  94. *
  95. * When compiling on UNIX platforms, the platformstl::process_mutex
  96. * type resolves to the unixstl::process_mutex class. On Windows
  97. * platforms it resolves to the winstl::process_mutex class. It is not
  98. * defined for other platforms.
  99. */
  100. class process_mutex
  101. {
  102. /// \name Member Types
  103. /// @{
  104. public:
  105. #if defined(PLATFORMSTL_OS_IS_UNIX)
  106. typedef pthread_mutex_t *handle_type;
  107. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  108. typedef HANDLE handle_type;
  109. #else /* ? OS */
  110. # error Operating system not discriminated
  111. #endif /* OS */
  112. typedef process_mutex class_type;
  113. typedef handle_type resource_type;
  114. /// @}
  115. /// \name Construction
  116. /// @{
  117. public:
  118. /// \brief Constructs a mutex object
  119. process_mutex();
  120. /// \brief Destroys a mutex object
  121. ~process_mutex();
  122. /// @}
  123. /// \name Operations
  124. /// @{
  125. public:
  126. /// \brief Acquires a lock on the mutex, pending the thread until the lock is aquired
  127. void lock();
  128. /// \brief Attempts to lock the mutex
  129. ///
  130. /// \return <b>true</b> if the mutex was aquired, or <b>false</b> if not
  131. bool try_lock();
  132. /// \brief Releases an aquired lock on the mutex
  133. void unlock();
  134. /// @}
  135. /// \name Accessors
  136. /// @{
  137. public:
  138. /// \brief The underlying kernel object handle
  139. handle_type get();
  140. /// @}
  141. };
  142. #elif defined(PLATFORMSTL_OS_IS_UNIX)
  143. # ifdef _UNIXSTL_NO_NAMESPACE
  144. using ::process_mutex;
  145. # else /* ? _UNIXSTL_NO_NAMESPACE */
  146. using ::unixstl::process_mutex;
  147. # endif /* _UNIXSTL_NO_NAMESPACE */
  148. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  149. # ifdef _WINSTL_NO_NAMESPACE
  150. using ::process_mutex;
  151. # else /* ? _WINSTL_NO_NAMESPACE */
  152. using ::winstl::process_mutex;
  153. # endif /* _WINSTL_NO_NAMESPACE */
  154. #else /* ? operating system */
  155. # error Operating system not discriminated
  156. #endif /* operating system */
  157. /* ////////////////////////////////////////////////////////////////////// */
  158. #if defined(_STLSOFT_NO_NAMESPACE) || \
  159. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  160. } // namespace platformstl
  161. #else
  162. } // namespace platformstl_project
  163. } // namespace stlsoft
  164. #endif /* _STLSOFT_NO_NAMESPACE */
  165. /* ////////////////////////////////////////////////////////////////////// */
  166. #endif /* !PLATFORMSTL_INCL_PLATFORMSTL_SYNCH_HPP_PROCESS_MUTEX */
  167. /* ///////////////////////////// end of file //////////////////////////// */