#ifndef UTIL_MUTEX_H_
#define UTIL_MUTEX_H_
#if !defined(_WIN32)
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#include <unistd.h>
#endif
#if defined(MUTEX_IS_PTHREAD_RWLOCK)
#error We really dont want to include pthread
#include <pthread.h>
#include <stdlib.h>
typedef pthread_rwlock_t MutexType;
#else
#include <mutex>
typedef std::mutex MutexType;
#endif
namespace lbug {
namespace regex {
class Mutex {
public:
inline Mutex();
inline ~Mutex();
inline void Lock(); inline void Unlock(); inline void ReaderLock(); inline void ReaderUnlock(); inline void WriterLock() { Lock(); } inline void WriterUnlock() { Unlock(); }
private:
MutexType mutex_;
Mutex(Mutex* ignored);
Mutex(const Mutex&) = delete;
Mutex& operator=(const Mutex&) = delete;
};
#if defined(MUTEX_IS_PTHREAD_RWLOCK)
#define SAFE_PTHREAD(fncall) \
do { \
if ((fncall) != 0) \
abort(); \
} while (0)
Mutex::Mutex() {
SAFE_PTHREAD(pthread_rwlock_init(&mutex_, NULL));
}
Mutex::~Mutex() {
SAFE_PTHREAD(pthread_rwlock_destroy(&mutex_));
}
void Mutex::Lock() {
SAFE_PTHREAD(pthread_rwlock_wrlock(&mutex_));
}
void Mutex::Unlock() {
SAFE_PTHREAD(pthread_rwlock_unlock(&mutex_));
}
void Mutex::ReaderLock() {
SAFE_PTHREAD(pthread_rwlock_rdlock(&mutex_));
}
void Mutex::ReaderUnlock() {
SAFE_PTHREAD(pthread_rwlock_unlock(&mutex_));
}
#undef SAFE_PTHREAD
#else
Mutex::Mutex() {}
Mutex::~Mutex() {}
void Mutex::Lock() {
mutex_.lock();
}
void Mutex::Unlock() {
mutex_.unlock();
}
void Mutex::ReaderLock() {
Lock();
} void Mutex::ReaderUnlock() {
Unlock();
}
#endif
class MutexLock {
public:
explicit MutexLock(Mutex* mu) : mu_(mu) { mu_->Lock(); }
~MutexLock() { mu_->Unlock(); }
private:
Mutex* const mu_;
MutexLock(const MutexLock&) = delete;
MutexLock& operator=(const MutexLock&) = delete;
};
class ReaderMutexLock {
public:
explicit ReaderMutexLock(Mutex* mu) : mu_(mu) { mu_->ReaderLock(); }
~ReaderMutexLock() { mu_->ReaderUnlock(); }
private:
Mutex* const mu_;
ReaderMutexLock(const ReaderMutexLock&) = delete;
ReaderMutexLock& operator=(const ReaderMutexLock&) = delete;
};
class WriterMutexLock {
public:
explicit WriterMutexLock(Mutex* mu) : mu_(mu) { mu_->WriterLock(); }
~WriterMutexLock() { mu_->WriterUnlock(); }
private:
Mutex* const mu_;
WriterMutexLock(const WriterMutexLock&) = delete;
WriterMutexLock& operator=(const WriterMutexLock&) = delete;
};
#define MutexLock(x) static_assert(false, "MutexLock declaration missing variable name")
#define ReaderMutexLock(x) static_assert(false, "ReaderMutexLock declaration missing variable name")
#define WriterMutexLock(x) static_assert(false, "WriterMutexLock declaration missing variable name")
} } #endif