mutex.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifndef __ZMQ_MUTEX_HPP_INCLUDED__
4 #define __ZMQ_MUTEX_HPP_INCLUDED__
5 
6 #include "err.hpp"
7 #include "macros.hpp"
8 
9 // Mutex class encapsulates OS mutex in a platform-independent way.
10 
11 #if defined(ZMQ_HAVE_WINDOWS) && !defined(ZMQ_USE_CV_IMPL_PTHREADS)
12 
13 #include "windows.hpp"
14 
15 namespace zmq
16 {
17 class mutex_t
18 {
19  public:
20  mutex_t () { InitializeCriticalSection (&_cs); }
21 
22  ~mutex_t () { DeleteCriticalSection (&_cs); }
23 
24  void lock () { EnterCriticalSection (&_cs); }
25 
26  bool try_lock () { return (TryEnterCriticalSection (&_cs)) ? true : false; }
27 
28  void unlock () { LeaveCriticalSection (&_cs); }
29 
30  CRITICAL_SECTION *get_cs () { return &_cs; }
31 
32  private:
33  CRITICAL_SECTION _cs;
34 
36 };
37 }
38 
39 #elif defined ZMQ_HAVE_VXWORKS
40 
41 #include <vxWorks.h>
42 #include <semLib.h>
43 
44 namespace zmq
45 {
46 class mutex_t
47 {
48  public:
49  inline mutex_t ()
50  {
51  _semId =
52  semMCreate (SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);
53  }
54 
55  inline ~mutex_t () { semDelete (_semId); }
56 
57  inline void lock () { semTake (_semId, WAIT_FOREVER); }
58 
59  inline bool try_lock ()
60  {
61  if (semTake (_semId, NO_WAIT) == OK) {
62  return true;
63  }
64  return false;
65  }
66 
67  inline void unlock () { semGive (_semId); }
68 
69  private:
70  SEM_ID _semId;
71 
73 };
74 }
75 
76 #else
77 
78 #include <pthread.h>
79 
80 namespace zmq
81 {
82 class mutex_t
83 {
84  public:
85  inline mutex_t ()
86  {
87  int rc = pthread_mutexattr_init (&_attr);
88  posix_assert (rc);
89 
90  rc = pthread_mutexattr_settype (&_attr, PTHREAD_MUTEX_RECURSIVE);
91  posix_assert (rc);
92 
93  rc = pthread_mutex_init (&_mutex, &_attr);
94  posix_assert (rc);
95  }
96 
97  inline ~mutex_t ()
98  {
99  int rc = pthread_mutex_destroy (&_mutex);
100  posix_assert (rc);
101 
102  rc = pthread_mutexattr_destroy (&_attr);
103  posix_assert (rc);
104  }
105 
106  inline void lock ()
107  {
108  int rc = pthread_mutex_lock (&_mutex);
109  posix_assert (rc);
110  }
111 
112  inline bool try_lock ()
113  {
114  int rc = pthread_mutex_trylock (&_mutex);
115  if (rc == EBUSY)
116  return false;
117 
118  posix_assert (rc);
119  return true;
120  }
121 
122  inline void unlock ()
123  {
124  int rc = pthread_mutex_unlock (&_mutex);
125  posix_assert (rc);
126  }
127 
128  inline pthread_mutex_t *get_mutex () { return &_mutex; }
129 
130  private:
131  pthread_mutex_t _mutex;
132  pthread_mutexattr_t _attr;
133 
135 };
136 }
137 
138 #endif
139 
140 
141 namespace zmq
142 {
144 {
146 
148 
149  private:
151 
153 };
154 
155 
157 {
159  {
160  if (_mutex != NULL)
161  _mutex->lock ();
162  }
163 
165  {
166  if (_mutex != NULL)
167  _mutex->unlock ();
168  }
169 
170  private:
172 
174 };
175 }
176 
177 #endif
zmq::mutex_t::lock
void lock()
Definition: mutex.hpp:106
NULL
NULL
Definition: test_security_zap.cpp:405
zmq::mutex_t::_mutex
pthread_mutex_t _mutex
Definition: mutex.hpp:131
zmq::scoped_optional_lock_t::_mutex
mutex_t * _mutex
Definition: mutex.hpp:171
zmq::mutex_t::~mutex_t
~mutex_t()
Definition: mutex.hpp:97
zmq::scoped_lock_t::_mutex
mutex_t & _mutex
Definition: mutex.hpp:150
zmq::scoped_optional_lock_t::scoped_optional_lock_t
scoped_optional_lock_t(mutex_t *mutex_)
Definition: mutex.hpp:158
zmq
Definition: zmq.hpp:229
zmq::scoped_optional_lock_t::~scoped_optional_lock_t
~scoped_optional_lock_t()
Definition: mutex.hpp:164
zmq::mutex_t::_attr
pthread_mutexattr_t _attr
Definition: mutex.hpp:132
macros.hpp
windows.hpp
zmq::mutex_t::unlock
void unlock()
Definition: mutex.hpp:122
ZMQ_NON_COPYABLE_NOR_MOVABLE
#define ZMQ_NON_COPYABLE_NOR_MOVABLE(classname)
Definition: macros.hpp:58
zmq::scoped_optional_lock_t
Definition: mutex.hpp:156
zmq::scoped_lock_t::scoped_lock_t
scoped_lock_t(mutex_t &mutex_)
Definition: mutex.hpp:145
mutex_
internal::WrappedMutex mutex_
Definition: src/google/protobuf/message.cc:579
zmq::mutex_t
Definition: mutex.hpp:82
zmq::mutex_t::get_mutex
pthread_mutex_t * get_mutex()
Definition: mutex.hpp:128
posix_assert
#define posix_assert(x)
Definition: err.hpp:124
zmq::scoped_lock_t::~scoped_lock_t
~scoped_lock_t()
Definition: mutex.hpp:147
err.hpp
zmq::mutex_t::try_lock
bool try_lock()
Definition: mutex.hpp:112
true
#define true
Definition: cJSON.c:65
zmq::mutex_t::mutex_t
mutex_t()
Definition: mutex.hpp:85
zmq::scoped_lock_t
Definition: mutex.hpp:143


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57