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