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
11
12#include <boost/noncopyable.hpp>
13
14#include <cstdlib> // for NULL.
15
16namespace isc {
17namespace util {
18namespace thread {
19class CondVar;
20
39class Mutex : boost::noncopyable {
40public:
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
122private:
126 void lock();
127
134 bool tryLock();
135
137 void unlock();
138
139private:
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
205class CondVar : boost::noncopyable {
206public:
211 CondVar();
212
218 ~CondVar();
219
234 void wait(Mutex& mutex);
235
243 void signal();
244private:
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:
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
Encapsulation for a condition variable.
Definition: sync.h:205
void wait(Mutex &mutex)
Wait on the condition variable.
Definition: sync.cc:225
~CondVar()
Destructor.
Definition: sync.cc:220
CondVar()
Constructor.
Definition: sync.cc:217
void signal()
Unblock a thread waiting for the condition variable.
Definition: sync.cc:242
This holds a lock on a Mutex.
Definition: sync.h:72
Locker(Mutex &mutex, bool block=true)
Constructor.
Definition: sync.h:92
Mutex with very simple interface.
Definition: sync.h:39
~Mutex()
Destructor.
Definition: sync.cc:99
bool locked() const
If the mutex is currently locked.
Mutex()
Constructor.
Definition: sync.cc:57
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-dhcp-ddns.
Exception thrown when the mutex is already locked and a non-blocking locker is attempted around it.
Definition: sync.h:76
AlreadyLocked(const char *file, size_t line, const char *what)
Definition: sync.h:77