20 using std::unique_ptr;
31 #endif // ENABLE_DEBUG 37 #endif // ENABLE_DEBUG 42 struct Deinitializer {
43 Deinitializer(pthread_mutexattr_t& attributes):
47 const int result = pthread_mutexattr_destroy(&
attributes_);
60 pthread_mutexattr_t attributes;
61 int result = pthread_mutexattr_init(&attributes);
66 throw std::bad_alloc();
70 Deinitializer deinitializer(attributes);
77 result = pthread_mutexattr_settype(&attributes, PTHREAD_MUTEX_ERRORCHECK);
79 result = pthread_mutexattr_settype(&attributes, PTHREAD_MUTEX_NORMAL);
80 #endif // ENABLE_DEBUG 86 result = pthread_mutex_init(&
impl->mutex, &attributes);
89 impl_ =
impl.release();
93 throw std::bad_alloc();
101 const int result = pthread_mutex_destroy(&impl_->
mutex);
104 const bool locked = impl_->locked_count != 0;
105 #endif // ENABLE_DEBUG 120 #endif // ENABLE_DEBUG 127 Mutex::postLockAction() {
128 assert(impl_->locked_count == 0);
129 ++impl_->locked_count;
133 Mutex::preUnlockAction(
bool throw_ok) {
134 if (impl_->locked_count == 0) {
137 "Unlock attempt for unlocked mutex");
142 --impl_->locked_count;
147 return (impl_->locked_count != 0);
150 #endif // ENABLE_DEBUG 154 assert(impl_ != NULL);
155 const int result = pthread_mutex_lock(&impl_->
mutex);
161 #endif // ENABLE_DEBUG 166 assert(impl_ != NULL);
167 const int result = pthread_mutex_trylock(&impl_->
mutex);
173 if (result == EBUSY || result == EDEADLK) {
175 }
else if (result != 0) {
180 #endif // ENABLE_DEBUG 186 assert(impl_ != NULL);
188 preUnlockAction(
false);
189 #endif // ENABLE_DEBUG 190 const int result = pthread_mutex_unlock(&impl_->
mutex);
197 const int result = pthread_cond_init(&
cond_, NULL);
200 << std::strerror(result));
204 const int result = pthread_cond_destroy(&
cond_);
227 mutex.preUnlockAction(
true);
228 const int result = pthread_cond_wait(&impl_->
cond_, &mutex.impl_->mutex);
229 mutex.postLockAction();
231 const int result = pthread_cond_wait(&impl_->
cond_, &mutex.impl_->mutex);
237 std::strerror(result));
243 const int result = pthread_cond_signal(&impl_->
cond_);
void wait(Mutex &mutex)
Wait on the condition variable.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when an unexpected error condition occurs.
bool locked() const
If the mutex is currently locked.
Defines the logger used by the top-level component of kea-dhcp-ddns.
Mutex with very simple interface.
pthread_mutexattr_t & attributes_
void signal()
Unblock a thread waiting for the condition variable.
A generic exception that is thrown if a function is called in a prohibited way.