libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
mutex.hpp
Go to the documentation of this file.
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  inline mutex_t ()
100  {
101  int rc = pthread_mutexattr_init(&attr);
102  posix_assert (rc);
103 
104  rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
105  posix_assert (rc);
106 
107  rc = pthread_mutex_init (&mutex, &attr);
108  posix_assert (rc);
109  }
110 
111  inline ~mutex_t ()
112  {
113  int rc = pthread_mutex_destroy (&mutex);
114  posix_assert (rc);
115 
116  rc = pthread_mutexattr_destroy (&attr);
117  posix_assert (rc);
118  }
119 
120  inline void lock ()
121  {
122  int rc = pthread_mutex_lock (&mutex);
123  posix_assert (rc);
124  }
125 
126  inline bool try_lock ()
127  {
128  int rc = pthread_mutex_trylock (&mutex);
129  if (rc == EBUSY)
130  return false;
131 
132  posix_assert (rc);
133  return true;
134  }
135 
136  inline void unlock ()
137  {
138  int rc = pthread_mutex_unlock (&mutex);
139  posix_assert (rc);
140  }
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 {
165  {
167  : mutex (mutex_)
168  {
169  mutex.lock ();
170  }
171 
173  {
174  mutex.unlock ();
175  }
176 
177  private:
178 
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
#define posix_assert(x)
Definition: err.hpp:139
const mutex_t & operator=(const mutex_t &)
bool try_lock()
Definition: mutex.hpp:126
pthread_mutexattr_t attr
Definition: mutex.hpp:150
void unlock()
Definition: mutex.hpp:136
scoped_lock_t(mutex_t &mutex_)
Definition: mutex.hpp:166
pthread_mutex_t * get_mutex()
Definition: mutex.hpp:142
void lock()
Definition: mutex.hpp:120
mutex_t & mutex
Definition: mutex.hpp:179
Definition: address.hpp:35
pthread_mutex_t mutex
Definition: mutex.hpp:149