summaryrefslogtreecommitdiffstats
path: root/src/Net/ThreadManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Net/ThreadManager.cpp')
-rw-r--r--src/Net/ThreadManager.cpp105
1 files changed, 41 insertions, 64 deletions
diff --git a/src/Net/ThreadManager.cpp b/src/Net/ThreadManager.cpp
index 3495196..b1f4d4b 100644
--- a/src/Net/ThreadManager.cpp
+++ b/src/Net/ThreadManager.cpp
@@ -20,12 +20,13 @@
#include "ThreadManager.h"
#include "FdManager.h"
+#include <Common/Logger.h>
+#include <Common/LogManager.h>
+
#include <fcntl.h>
#include <sigc++/bind.h>
#include <sigc++/hide.h>
-#include <ignore-value.h>
-
namespace Mad {
namespace Net {
@@ -34,35 +35,31 @@ ThreadManager ThreadManager::threadManager;
void ThreadManager::workerFunc() {
while(true) {
- gl_lock_lock(runLock);
+ boost::unique_lock<boost::mutex> lock(runLock);
- if(!running || !isThisWorkerThread()) {
- gl_lock_unlock(runLock);
- return;
- }
+ if(!running || !isThisWorkerThread())
+ break;
- gl_lock_unlock(runLock);
+ lock.unlock();
- gl_lock_lock(workLock);
+ boost::unique_lock<boost::mutex> lock2(workLock);
while(work.empty()) {
- gl_cond_wait(workCond, workLock);
+ workCond.wait(lock2);
- if(!running) {
- gl_lock_unlock(workLock);
+ if(!running)
return;
- }
}
sigc::slot<void> nextWork = work.front();
work.pop();
- gl_lock_unlock(workLock);
+ lock2.unlock();
nextWork();
}
// And let the new worker thread join us...
- pushWork(sigc::bind(sigc::mem_fun(this, &ThreadManager::threadFinished), (gl_thread_t)gl_thread_self()));
+ // TODO pushWork(sigc::bind(sigc::mem_fun(this, &ThreadManager::threadFinished), (gl_thread_t)gl_thread_self()));
}
void ThreadManager::detach() {
@@ -71,49 +68,43 @@ void ThreadManager::detach() {
return;
}
- gl_lock_lock(runLock);
+ runLock.lock();
bool isRunning = running;
- gl_lock_unlock(runLock);
+ runLock.unlock();
if(!isRunning) // There's no point in creating a new worker thread when we aren't running anymore
return;
- gl_lock_lock(threadLock);
+ threadLock.lock();
+
+ if(workerThread->get_id() == boost::this_thread::get_id()) {// Already detached?
+ threads.add_thread(workerThread);
- if(workerThread == (gl_thread_t)gl_thread_self()) {// Already detached?
- threads.insert(workerThread);
- workerThread = gl_thread_create(&ThreadManager::workerStart, 0);
+ workerThread = new boost::thread(std::mem_fun(&ThreadManager::workerFunc), this);
}
- gl_lock_unlock(threadLock);
+ threadLock.unlock();
}
void ThreadManager::pushWork(const sigc::slot<void> &newWork) {
- gl_lock_lock(workLock);
-
+ workLock.lock();
work.push(newWork);
+ workLock.unlock();
- gl_cond_signal(workCond);
- gl_lock_unlock(workLock);
+ workCond.notify_one();
}
void ThreadManager::doInit() {
- gl_lock_init(threadLock);
- gl_lock_init(runLock);
-
- gl_lock_init(workLock);
- gl_cond_init(workCond);
-
running = true;
- gl_lock_lock(threadLock);
+ threadLock.lock();
- mainThread = (gl_thread_t)gl_thread_self();
- workerThread = gl_thread_create(&ThreadManager::workerStart, 0);
- loggerThread = gl_thread_create(&ThreadManager::loggerStart, 0);
- ioThread = gl_thread_create(&ThreadManager::ioStart, 0);
+ mainThreadId = boost::this_thread::get_id();
+ workerThread = new boost::thread(std::mem_fun(&ThreadManager::workerFunc), this);
+ loggerThread = new boost::thread(std::mem_fun(&Common::LogManager::loggerThread), Common::LogManager::get());
+ ioThread = new boost::thread(std::mem_fun(&FdManager::ioThread), FdManager::get());
- gl_lock_unlock(threadLock);
+ threadLock.unlock();
}
void ThreadManager::doDeinit() {
@@ -123,44 +114,30 @@ void ThreadManager::doDeinit() {
}
// First set running = false so the worker threads quit
- gl_lock_lock(runLock);
- gl_lock_lock(workLock);
-
+ boost::lock(runLock, workLock);
running = false;
- gl_cond_signal(workCond);
- gl_lock_unlock(workLock);
- gl_lock_unlock(runLock);
+ workLock.unlock();
+ runLock.unlock();
+
+ workCond.notify_one();
// We don't have to lock threadLock as detach() won't change workerThread when running is false
- gl_thread_join(workerThread, 0);
+ workerThread->join();
+ delete workerThread;
// Now wait for all detached threads
- gl_lock_lock(threadLock);
- while(!threads.empty()) {
- gl_thread_t thread = *threads.begin();
- gl_lock_unlock(threadLock);
-
- gl_thread_join(thread, 0);
-
- gl_lock_lock(threadLock);
- threads.erase(thread);
- }
- gl_lock_unlock(threadLock);
+ threads.join_all();
// IO thread is next
FdManager::get()->stopIOThread();
- gl_thread_join(ioThread, 0);
+ ioThread->join();
+ delete ioThread;
// Finally, the logger thread has to die
Common::LogManager::get()->stopLoggerThread();
- gl_thread_join(loggerThread, 0);
-
- gl_cond_destroy(workCond);
- gl_lock_destroy(workLock);
-
- gl_lock_destroy(runLock);
- gl_lock_destroy(threadLock);
+ loggerThread->join();
+ delete loggerThread;
}
}