From 67432fe6501e1ae011870310b1dbcfb49b5233a8 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sun, 4 Dec 2011 11:53:55 -0800 Subject: Refactored the Thread code a bit, and created the GlobalThread class, for threads that are created on startup. --- includes/Threads.h | 15 +++++++++++---- src/Socket.cc | 9 ++------- src/TaskMan.cc | 9 ++------- src/Threads.cc | 2 -- 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 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 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() { } -- cgit v1.2.3