mutex.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_MUTEX_HPP_INCLUDED__
  25. #define __ZMQ_MUTEX_HPP_INCLUDED__
  26. #include "err.hpp"
  27. #include "macros.hpp"
  28. // Mutex class encapsulates OS mutex in a platform-independent way.
  29. #ifdef ZMQ_HAVE_WINDOWS
  30. #include "windows.hpp"
  31. namespace zmq
  32. {
  33. class mutex_t
  34. {
  35. public:
  36. mutex_t () { InitializeCriticalSection (&_cs); }
  37. ~mutex_t () { DeleteCriticalSection (&_cs); }
  38. void lock () { EnterCriticalSection (&_cs); }
  39. bool try_lock () { return (TryEnterCriticalSection (&_cs)) ? true : false; }
  40. void unlock () { LeaveCriticalSection (&_cs); }
  41. CRITICAL_SECTION *get_cs () { return &_cs; }
  42. private:
  43. CRITICAL_SECTION _cs;
  44. ZMQ_NON_COPYABLE_NOR_MOVABLE (mutex_t)
  45. };
  46. }
  47. #elif defined ZMQ_HAVE_VXWORKS
  48. #include <vxWorks.h>
  49. #include <semLib.h>
  50. namespace zmq
  51. {
  52. class mutex_t
  53. {
  54. public:
  55. inline mutex_t ()
  56. {
  57. _semId =
  58. semMCreate (SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);
  59. }
  60. inline ~mutex_t () { semDelete (_semId); }
  61. inline void lock () { semTake (_semId, WAIT_FOREVER); }
  62. inline bool try_lock ()
  63. {
  64. if (semTake (_semId, NO_WAIT) == OK) {
  65. return true;
  66. }
  67. return false;
  68. }
  69. inline void unlock () { semGive (_semId); }
  70. private:
  71. SEM_ID _semId;
  72. ZMQ_NON_COPYABLE_NOR_MOVABLE (mutex_t)
  73. };
  74. }
  75. #else
  76. #include <pthread.h>
  77. namespace zmq
  78. {
  79. class mutex_t
  80. {
  81. public:
  82. inline mutex_t ()
  83. {
  84. int rc = pthread_mutexattr_init (&_attr);
  85. posix_assert (rc);
  86. rc = pthread_mutexattr_settype (&_attr, PTHREAD_MUTEX_RECURSIVE);
  87. posix_assert (rc);
  88. rc = pthread_mutex_init (&_mutex, &_attr);
  89. posix_assert (rc);
  90. }
  91. inline ~mutex_t ()
  92. {
  93. int rc = pthread_mutex_destroy (&_mutex);
  94. posix_assert (rc);
  95. rc = pthread_mutexattr_destroy (&_attr);
  96. posix_assert (rc);
  97. }
  98. inline void lock ()
  99. {
  100. int rc = pthread_mutex_lock (&_mutex);
  101. posix_assert (rc);
  102. }
  103. inline bool try_lock ()
  104. {
  105. int rc = pthread_mutex_trylock (&_mutex);
  106. if (rc == EBUSY)
  107. return false;
  108. posix_assert (rc);
  109. return true;
  110. }
  111. inline void unlock ()
  112. {
  113. int rc = pthread_mutex_unlock (&_mutex);
  114. posix_assert (rc);
  115. }
  116. inline pthread_mutex_t *get_mutex () { return &_mutex; }
  117. private:
  118. pthread_mutex_t _mutex;
  119. pthread_mutexattr_t _attr;
  120. ZMQ_NON_COPYABLE_NOR_MOVABLE (mutex_t)
  121. };
  122. }
  123. #endif
  124. namespace zmq
  125. {
  126. struct scoped_lock_t
  127. {
  128. scoped_lock_t (mutex_t &mutex_) : _mutex (mutex_) { _mutex.lock (); }
  129. ~scoped_lock_t () { _mutex.unlock (); }
  130. private:
  131. mutex_t &_mutex;
  132. ZMQ_NON_COPYABLE_NOR_MOVABLE (scoped_lock_t)
  133. };
  134. struct scoped_optional_lock_t
  135. {
  136. scoped_optional_lock_t (mutex_t *mutex_) : _mutex (mutex_)
  137. {
  138. if (_mutex != NULL)
  139. _mutex->lock ();
  140. }
  141. ~scoped_optional_lock_t ()
  142. {
  143. if (_mutex != NULL)
  144. _mutex->unlock ();
  145. }
  146. private:
  147. mutex_t *_mutex;
  148. ZMQ_NON_COPYABLE_NOR_MOVABLE (scoped_optional_lock_t)
  149. };
  150. }
  151. #endif