Line data Source code
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 "platform.hpp"
34 : #include "err.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 :
42 : namespace zmq
43 : {
44 :
45 : class mutex_t
46 : {
47 : public:
48 : inline mutex_t ()
49 : {
50 : InitializeCriticalSection (&cs);
51 : }
52 :
53 : inline ~mutex_t ()
54 : {
55 : DeleteCriticalSection (&cs);
56 : }
57 :
58 : inline void lock ()
59 : {
60 : EnterCriticalSection (&cs);
61 : }
62 :
63 : inline bool try_lock ()
64 : {
65 : return (TryEnterCriticalSection (&cs)) ? true : false;
66 : }
67 :
68 : inline void unlock ()
69 : {
70 : LeaveCriticalSection (&cs);
71 : }
72 :
73 : inline CRITICAL_SECTION* get_cs()
74 : {
75 : return &cs;
76 : }
77 :
78 : private:
79 :
80 : CRITICAL_SECTION cs;
81 :
82 : // Disable copy construction and assignment.
83 : mutex_t (const mutex_t&);
84 : void operator = (const mutex_t&);
85 : };
86 :
87 : }
88 :
89 : #else
90 :
91 : #include <pthread.h>
92 :
93 : namespace zmq
94 : {
95 :
96 : class mutex_t
97 : {
98 : public:
99 25490 : inline mutex_t ()
100 : {
101 25490 : int rc = pthread_mutexattr_init(&attr);
102 25490 : posix_assert (rc);
103 :
104 25490 : rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
105 25490 : posix_assert (rc);
106 :
107 25490 : rc = pthread_mutex_init (&mutex, &attr);
108 25490 : posix_assert (rc);
109 25490 : }
110 :
111 25490 : inline ~mutex_t ()
112 : {
113 25490 : int rc = pthread_mutex_destroy (&mutex);
114 25490 : posix_assert (rc);
115 :
116 25490 : rc = pthread_mutexattr_destroy (&attr);
117 25490 : posix_assert (rc);
118 25490 : }
119 :
120 1377004 : inline void lock ()
121 : {
122 1377004 : int rc = pthread_mutex_lock (&mutex);
123 1384749 : posix_assert (rc);
124 1384749 : }
125 :
126 63 : inline bool try_lock ()
127 : {
128 63 : int rc = pthread_mutex_trylock (&mutex);
129 63 : if (rc == EBUSY)
130 : return false;
131 :
132 63 : posix_assert (rc);
133 : return true;
134 : }
135 :
136 1384953 : inline void unlock ()
137 : {
138 1384953 : int rc = pthread_mutex_unlock (&mutex);
139 1385259 : posix_assert (rc);
140 1385259 : }
141 :
142 : inline pthread_mutex_t* get_mutex()
143 : {
144 : return &mutex;
145 : }
146 :
147 : private:
148 :
149 : pthread_mutex_t mutex;
150 : pthread_mutexattr_t attr;
151 :
152 : // Disable copy construction and assignment.
153 : mutex_t (const mutex_t&);
154 : const mutex_t &operator = (const mutex_t&);
155 : };
156 :
157 : }
158 :
159 : #endif
160 :
161 :
162 : namespace zmq
163 : {
164 : struct scoped_lock_t
165 : {
166 : scoped_lock_t (mutex_t& mutex_)
167 15 : : mutex (mutex_)
168 : {
169 15 : mutex.lock ();
170 : }
171 :
172 : ~scoped_lock_t ()
173 : {
174 15 : mutex.unlock ();
175 : }
176 :
177 : private:
178 :
179 : mutex_t& mutex;
180 :
181 : // Disable copy construction and assignment.
182 : scoped_lock_t (const scoped_lock_t&);
183 : const scoped_lock_t &operator = (const scoped_lock_t&);
184 : };
185 : }
186 :
187 : #endif
|