From 2f36082049c30562f2cb3061438c4aaebc8d8fe5 Mon Sep 17 00:00:00 2001 From: Pixel Date: Mon, 5 Dec 2011 09:16:42 -0800 Subject: Adding read-write locks, and applying them immediately to the http server. --- includes/HttpServer.h | 2 +- includes/Threads.h | 11 +++++++++++ src/HttpServer.cc | 6 +++--- src/Threads.cc | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/includes/HttpServer.h b/includes/HttpServer.h index 3867252..37353ac 100644 --- a/includes/HttpServer.h +++ b/includes/HttpServer.h @@ -55,7 +55,7 @@ class HttpServer { String m_local; typedef std::list ActionList; ActionList m_actions; - Lock m_actionsLock; + RWLock m_actionsLock; friend class HttpWorker; }; diff --git a/includes/Threads.h b/includes/Threads.h index f6aed5a..67143ca 100644 --- a/includes/Threads.h +++ b/includes/Threads.h @@ -20,6 +20,17 @@ class Lock { friend class Queue; }; +class RWLock { + public: + RWLock(); + ~RWLock() { pthread_rwlock_destroy(&m_lock); } + void enterR() { pthread_rwlock_rdlock(&m_lock); } + void enterW() { pthread_rwlock_wrlock(&m_lock); } + void leave() { pthread_rwlock_unlock(&m_lock); } + private: + pthread_rwlock_t m_lock; +}; + class ThreadHelper; class Thread { diff --git a/src/HttpServer.cc b/src/HttpServer.cc index d96dd2b..e2ff180 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -534,14 +534,14 @@ void Balau::HttpServer::stop() { } void Balau::HttpServer::registerAction(Action * action) { - m_actionsLock.enter(); + m_actionsLock.enterW(); action->ref(); m_actions.push_front(action); m_actionsLock.leave(); } void Balau::HttpServer::flushAllActions() { - m_actionsLock.enter(); + m_actionsLock.enterW(); Action * a; while (!m_actions.empty()) { a = m_actions.front(); @@ -563,7 +563,7 @@ Balau::HttpServer::Action::ActionMatch Balau::HttpServer::Action::matches(const } Balau::HttpServer::ActionFound Balau::HttpServer::findAction(const char * uri, const char * host) { - m_actionsLock.enter(); + m_actionsLock.enterR(); ActionList::iterator i; ActionFound r; diff --git a/src/Threads.cc b/src/Threads.cc index de70933..fe90394 100644 --- a/src/Threads.cc +++ b/src/Threads.cc @@ -22,6 +22,20 @@ Balau::Lock::Lock() { RAssert(r == 0, "Couldn't set mutex attribute; r = %i", r); r = pthread_mutex_init(&m_lock, &attr); RAssert(r == 0, "Couldn't initialize mutex; r = %i", r); + r = pthread_mutexattr_destroy(&attr); + RAssert(r == 0, "Couldn't destroy mutex attribute; r = %i", r); +} + +Balau::RWLock::RWLock() { + int r; + pthread_rwlockattr_t attr; + + r = pthread_rwlockattr_init(&attr); + RAssert(r == 0, "Couldn't initialize rwlock attribute; r = %i", r); + r = pthread_rwlock_init(&m_lock, &attr); + RAssert(r == 0, "Couldn't initialize mutex; r = %i", r); + r = pthread_rwlockattr_destroy(&attr); + RAssert(r == 0, "Couldn't destroy rwlock attribute; r = %i", r); } void * Balau::ThreadHelper::threadProc(void * arg) { -- cgit v1.2.3