#ifndef _SINGLEDISH_PTHREAD_UTILS_H_
#define _SINGLEDISH_PTHREAD_UTILS_H_
#define THROW_IF(condition, msg) \
throw runtime_error((msg)); \
mutex_(PTHREAD_MUTEX_INITIALIZER) {
int ret = pthread_mutex_init(&mutex_, NULL);
THROW_IF(ret != 0, "Mutex::Mutex() failed to initalize mutex");
~Mutex() noexcept(false) {
int ret = pthread_mutex_destroy(&mutex_);
THROW_IF(ret != 0, "Mutex::~Mutex() failed to destroy mutex");
int ret = pthread_mutex_lock(&mutex_);
THROW_IF(ret != 0, "Mutex::lock() failed to lock mutex");
int ret = pthread_mutex_unlock(&mutex_);
THROW_IF(ret != 0, "Mutex::unlock() failed to unlock mutex");
return pthread_mutex_trylock(&mutex_);
PCondition(Mutex *mutex) :
mutex_(&(mutex->mutex_)) {
int ret = pthread_cond_init(&cond_, NULL);
"PCondition::PCondition() failed to initialize pthread_cond_t");
virtual ~PCondition() noexcept(false) {
int ret = pthread_cond_destroy(&cond_);
"PCondition::~PCondition() failed to destroy pthread_cond_t");
return pthread_mutex_lock(mutex_);
return pthread_mutex_unlock(mutex_);
int ret = pthread_cond_wait(&cond_, mutex_);
THROW_IF(ret != 0, "PCondition::wait() failed to block pthread_cond_t");