Kea  1.5.0
sync.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef KEA_THREAD_SYNC_H
8 #define KEA_THREAD_SYNC_H
9 
10 #include <exceptions/exceptions.h>
11 
12 #include <boost/noncopyable.hpp>
13 
14 #include <cstdlib> // for NULL.
15 
16 namespace isc {
17 namespace util {
18 namespace thread {
19 class CondVar;
20 
39 class Mutex : boost::noncopyable {
40 public:
54  Mutex();
55 
61  ~Mutex();
62 
72  class Locker : boost::noncopyable {
73  public:
77  AlreadyLocked(const char* file, size_t line, const char* what) :
78  isc::InvalidParameter(file, line, what)
79  {}
80  };
81 
92  Locker(Mutex& mutex, bool block = true) :
93  mutex_(mutex)
94  {
95  if (block) {
96  mutex.lock();
97  } else {
98  if (!mutex.tryLock()) {
99  isc_throw(AlreadyLocked, "The mutex is already locked");
100  }
101  }
102  }
103 
108  mutex_.unlock();
109  }
110  private:
111  Mutex& mutex_;
112  };
120  bool locked() const;
121 
122 private:
126  void lock();
127 
134  bool tryLock();
135 
137  void unlock();
138 
139 private:
140  friend class CondVar;
141 
142  // Commonly called after acquiring the lock, checking and updating
143  // internal state for debug.
144  //
145  // Note that this method is only available when the build is
146  // configured with debugging support.
147  void postLockAction();
148 
149  // Commonly called before releasing the lock, checking and updating
150  // internal state for debug.
151  //
152  // If throw_ok is true, it throws \c isc::InvalidOperation when the check
153  // fails; otherwise it aborts the process. This parameter must be set
154  // to false if the call to this shouldn't result in an exception (e.g.
155  // when called from a destructor).
156  //
157  // Note that this method is only available when the build is
158  // configured with debugging support.
159  void preUnlockAction(bool throw_ok);
160 
161  class Impl;
162  Impl* impl_;
163 };
164 
205 class CondVar : boost::noncopyable {
206 public:
211  CondVar();
212 
218  ~CondVar();
219 
234  void wait(Mutex& mutex);
235 
243  void signal();
244 private:
245  class Impl;
246  Impl* impl_;
247 };
248 
249 } // namespace thread
250 } // namespace util
251 } // namespace isc
252 
253 #endif
254 
255 // Local Variables:
256 // mode: c++
257 // End:
isc::util::thread::CondVar::wait
void wait(Mutex &mutex)
Wait on the condition variable.
Definition: sync.cc:225
isc::util::thread::CondVar::~CondVar
~CondVar()
Destructor.
Definition: sync.cc:220
isc::util::thread::Mutex::Locker
This holds a lock on a Mutex.
Definition: sync.h:72
isc::util::thread::CondVar::Impl
Definition: sync.cc:194
isc::util::thread::Mutex
Mutex with very simple interface.
Definition: sync.h:39
isc::util::thread::Mutex::Locker::Locker
Locker(Mutex &mutex, bool block=true)
Constructor.
Definition: sync.h:92
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::util::thread::Mutex::~Mutex
~Mutex()
Destructor.
Definition: sync.cc:99
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::InvalidParameter
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
Definition: exceptions/exceptions.h:124
isc::util::thread::Mutex::locked
bool locked() const
If the mutex is currently locked.
isc::util::thread::Mutex::Locker::AlreadyLocked::AlreadyLocked
AlreadyLocked(const char *file, size_t line, const char *what)
Definition: sync.h:77
isc::util::thread::CondVar::CondVar
CondVar()
Constructor.
Definition: sync.cc:217
isc::util::thread::CondVar
Encapsulation for a condition variable.
Definition: sync.h:205
isc::util::thread::Mutex::Locker::AlreadyLocked
Exception thrown when the mutex is already locked and a non-blocking locker is attempted around it.
Definition: sync.h:76
isc::util::thread::Mutex::Mutex
Mutex()
Constructor.
Definition: sync.cc:57
exceptions.h
isc::util::thread::CondVar::signal
void signal()
Unblock a thread waiting for the condition variable.
Definition: sync.cc:242
isc::util::thread::Mutex::Locker::~Locker
~Locker()
Destructor.
Definition: sync.h:107
isc::util::thread::Mutex::Impl
Definition: sync.cc:26