summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2011-12-04 11:53:55 -0800
committerPixel <pixel@nobis-crew.org>2011-12-04 11:53:55 -0800
commit67432fe6501e1ae011870310b1dbcfb49b5233a8 (patch)
treefded68e0b61d4fcdfd2f424f53a5c3b8370d477c
parentf99192cb59bb3e09ae4841bb790c57969fbe2003 (diff)
Refactored the Thread code a bit, and created the GlobalThread class, for threads that are created on startup.
-rw-r--r--includes/Threads.h15
-rw-r--r--src/Socket.cc9
-rw-r--r--src/TaskMan.cc9
-rw-r--r--src/Threads.cc2
4 files changed, 15 insertions, 20 deletions
diff --git a/includes/Threads.h b/includes/Threads.h
index bc2670d..f6aed5a 100644
--- a/includes/Threads.h
+++ b/includes/Threads.h
@@ -22,23 +22,30 @@ class Lock {
class ThreadHelper;
-class Thread : public AtExit {
+class Thread {
public:
virtual ~Thread();
void threadStart();
void * join();
protected:
- Thread(bool registerAtExit = false) : AtExit(registerAtExit ? 1 : -1), m_joined(false) { }
+ Thread() : m_joined(false) { }
virtual void * proc() = 0;
- virtual void threadExit();
+ virtual void threadExit() { };
private:
pthread_t m_thread;
volatile bool m_joined;
- virtual void doExit() { join(); }
friend class ThreadHelper;
};
+class GlobalThread : public Thread, public AtStart, public AtExit {
+ protected:
+ GlobalThread(int startOrder = 10) : AtStart(startOrder), AtExit(1) { }
+ private:
+ virtual void doStart() { threadStart(); }
+ virtual void doExit() { join(); }
+};
+
template<class T>
class Queue {
public:
diff --git a/src/Socket.cc b/src/Socket.cc
index 2f9fa17..56e2dfb 100644
--- a/src/Socket.cc
+++ b/src/Socket.cc
@@ -175,22 +175,17 @@ static const char * inet_ntop(int af, const void * src, char * dst, socklen_t si
#if 0
// TODO: use getaddrinfo_a, if available.
#else
-class ResolverThread : public Balau::Thread, public Balau::AtStart {
+class ResolverThread : public Balau::GlobalThread {
public:
- ResolverThread() : Thread(true), AtStart(8), m_stopping(false) { }
+ ResolverThread() : GlobalThread(8), m_stopping(false) { }
void pushRequest(DNSRequest * req) { m_queue.push(req); }
private:
virtual void * proc();
- virtual void doStart();
virtual void threadExit();
Balau::Queue<DNSRequest> m_queue;
volatile bool m_stopping;
};
-void ResolverThread::doStart() {
- threadStart();
-}
-
void ResolverThread::threadExit() {
m_stopping = true;
DNSRequest req;
diff --git a/src/TaskMan.cc b/src/TaskMan.cc
index ba74c8f..a1d8255 100644
--- a/src/TaskMan.cc
+++ b/src/TaskMan.cc
@@ -27,12 +27,11 @@ static const int TOO_MANY_STACKS = 1024;
namespace Balau {
-class TaskScheduler : public Thread, public AtStart {
+class TaskScheduler : public GlobalThread {
public:
- TaskScheduler() : Thread(true), AtStart(100), m_stopping(false) { }
+ TaskScheduler() : GlobalThread(100), m_stopping(false) { }
void registerTask(Task * t);
virtual void * proc();
- virtual void doStart();
virtual void threadExit();
void registerTaskMan(TaskMan * t);
void unregisterTaskMan(TaskMan * t);
@@ -120,10 +119,6 @@ void * Balau::TaskScheduler::proc() {
return NULL;
}
-void Balau::TaskScheduler::doStart() {
- threadStart();
-}
-
void Balau::TaskScheduler::threadExit() {
Task * s = NULL;
m_queue.push(s);
diff --git a/src/Threads.cc b/src/Threads.cc
index df19da2..de70933 100644
--- a/src/Threads.cc
+++ b/src/Threads.cc
@@ -57,5 +57,3 @@ void Balau::Thread::threadStart() {
r = pthread_attr_destroy(&attr);
RAssert(r == 0, "Couldn't destroy pthread attribute; r = %i", r);
}
-
-void Balau::Thread::threadExit() { }