From 38bb6b72da1d3f5cfa4c72d8bc9597229423c0e3 Mon Sep 17 00:00:00 2001 From: Lubos Dolezel Date: Tue, 7 Jul 2015 23:08:03 +0200 Subject: Partial lock rework in t_phone (#17). If the app starts crashing or freezing, this commit is probably to blame. --- src/threads/mutex.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/threads/mutex.cpp') diff --git a/src/threads/mutex.cpp b/src/threads/mutex.cpp index 0ae2a99..159f887 100644 --- a/src/threads/mutex.cpp +++ b/src/threads/mutex.cpp @@ -20,6 +20,7 @@ #include #include "mutex.h" #include "thread.h" +#include using namespace std; @@ -88,3 +89,39 @@ t_mutex_guard::t_mutex_guard(t_mutex &mutex) : mutex_(mutex) { t_mutex_guard::~t_mutex_guard() { mutex_.unlock(); } + + +/////////////////////////// +// t_rwmutex +/////////////////////////// + +t_rwmutex::t_rwmutex() +{ + int ret = pthread_rwlock_init(&_lock, nullptr); + if (ret != 0) throw string( + "t_rwmutex::t_rwmutex failed to create a r/w mutex."); +} + +t_rwmutex::~t_rwmutex() +{ + pthread_rwlock_destroy(&_lock); +} + +void t_rwmutex::lockRead() +{ + int err = pthread_rwlock_rdlock(&_lock); + if (err != 0) + throw std::logic_error("Mutex lock failed"); +} + +void t_rwmutex::lockWrite() +{ + int err = pthread_rwlock_wrlock(&_lock); + if (err != 0) + throw std::logic_error("Mutex lock failed"); +} + +void t_rwmutex::unlock() +{ + pthread_rwlock_unlock(&_lock); +} -- cgit v1.2.3