summaryrefslogtreecommitdiffstats
path: root/src/threads
diff options
context:
space:
mode:
authorLubos Dolezel <lubos@dolezel.info>2015-07-07 23:08:03 +0200
committerLubos Dolezel <lubos@dolezel.info>2015-07-07 23:08:03 +0200
commit38bb6b72da1d3f5cfa4c72d8bc9597229423c0e3 (patch)
tree14186c138dbd8c90dc94f41484c4d1b2ac1d7818 /src/threads
parentea71ccfcfab52f74469231cab4e70552333b590a (diff)
downloadtwinkle-38bb6b72da1d3f5cfa4c72d8bc9597229423c0e3.tar
twinkle-38bb6b72da1d3f5cfa4c72d8bc9597229423c0e3.tar.gz
twinkle-38bb6b72da1d3f5cfa4c72d8bc9597229423c0e3.tar.lz
twinkle-38bb6b72da1d3f5cfa4c72d8bc9597229423c0e3.tar.xz
twinkle-38bb6b72da1d3f5cfa4c72d8bc9597229423c0e3.zip
Partial lock rework in t_phone (#17). If the app starts crashing or freezing, this commit is probably to blame.
Diffstat (limited to 'src/threads')
-rw-r--r--src/threads/mutex.cpp37
-rw-r--r--src/threads/mutex.h42
2 files changed, 79 insertions, 0 deletions
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 <iostream>
#include "mutex.h"
#include "thread.h"
+#include <stdexcept>
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);
+}
diff --git a/src/threads/mutex.h b/src/threads/mutex.h
index 9970e08..16f314d 100644
--- a/src/threads/mutex.h
+++ b/src/threads/mutex.h
@@ -21,6 +21,7 @@
#include <errno.h>
#include <pthread.h>
+// #include <iostream>
/**
* @file
@@ -83,4 +84,45 @@ public:
~t_mutex_guard();
};
+class t_rwmutex {
+protected:
+ pthread_rwlock_t _lock;
+public:
+ t_rwmutex();
+ ~t_rwmutex();
+
+ void lockRead();
+ void lockWrite();
+ void unlock();
+};
+
+class t_rwmutex_reader {
+private:
+ t_rwmutex& _mutex;
+public:
+ t_rwmutex_reader(t_rwmutex& mutex) : _mutex(mutex) {
+ // std::cout << "mtx rd lock " << (void*)&_mutex << std::endl;
+ _mutex.lockRead();
+ }
+ ~t_rwmutex_reader() {
+ // std::cout << "mtx rd unlock " << (void*)&_mutex << std::endl;
+ _mutex.unlock();
+ }
+};
+
+class t_rwmutex_writer {
+private:
+ t_rwmutex& _mutex;
+public:
+ t_rwmutex_writer(t_rwmutex& mutex) : _mutex(mutex) {
+ // std::cout << "mtx wr lock " << (void*)&_mutex << std::endl;
+ _mutex.lockWrite();
+ }
+ ~t_rwmutex_writer() {
+ // std::cout << "mtx wr unlock " << (void*)&_mutex << std::endl;
+ _mutex.unlock();
+ }
+};
+
+
#endif