From ff4c3ff14e76e5b06f1b4c44f03f900e1bd4ac50 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 14 May 2012 05:45:15 +0000 Subject: Clean up Thread interface. git-svn-id: http://svn.drobilla.net/lad/trunk/raul@4411 a436a847-0d15-0410-975c-d299462d15a1 --- raul/Slave.hpp | 2 +- raul/Thread.hpp | 24 +++++++++++++++--------- src/Thread.cpp | 30 ++++++++++++------------------ test/queue_test.cpp | 22 ++++++++++++++-------- test/sem_test.cpp | 6 +----- test/thread_test.cpp | 6 +----- 6 files changed, 44 insertions(+), 46 deletions(-) diff --git a/raul/Slave.hpp b/raul/Slave.hpp index e148cff..593d5e2 100644 --- a/raul/Slave.hpp +++ b/raul/Slave.hpp @@ -32,7 +32,7 @@ namespace Raul { class Slave : public Thread { public: - Slave() : _whip(0) {} + explicit Slave(const std::string& name="") : Thread(name), _whip(0) {} /** Tell the slave to do whatever work it does. Realtime safe. */ inline void whip() { _whip.post(); } diff --git a/raul/Thread.hpp b/raul/Thread.hpp index 9b61048..021bf86 100644 --- a/raul/Thread.hpp +++ b/raul/Thread.hpp @@ -39,27 +39,32 @@ class Thread : Noncopyable public: virtual ~Thread(); - static Thread* create(const std::string& name="") - { return new Thread(name); } - - static Thread* create_for_this_thread(const std::string& name=""); + /** Create a new thread. */ + static Thread* create(const std::string& name="") { + return new Thread(name); + } /** Return the calling thread. * * If the calling thread does not yet have a Thread object associated with - * it, one will be created. + * it yet, one will be created with the given name. */ static Thread& get(const std::string& name=""); + /** Start the thread if it is not already running. */ virtual void start(); + + /** Stop the thread if it is running. */ virtual void stop(); + /** Wait until the thread exits. */ virtual void join(); - void set_scheduling(int policy, unsigned int priority); + /** Set the scheduling policy for this thread. */ + virtual void set_scheduling(bool realtime, unsigned priority); + /** Return the name of this thread. */ const std::string& name() const { return _name; } - void set_name(const std::string& name) { _name = name; } protected: explicit Thread(const std::string& name=""); @@ -75,8 +80,6 @@ protected: */ virtual void _run() {} - bool _exit_flag; - private: static void* _static_run(void* me); @@ -84,6 +87,9 @@ private: std::string _name; bool _thread_exists; bool _own_thread; + +protected: + bool _exit_flag; }; } // namespace Raul diff --git a/src/Thread.cpp b/src/Thread.cpp index 4a3e7bf..34d7b65 100644 --- a/src/Thread.cpp +++ b/src/Thread.cpp @@ -36,21 +36,21 @@ struct ThreadImpl { static ThreadVar self(NULL); Thread::Thread(const std::string& name) - : _exit_flag(false) - , _impl(new ThreadImpl()) + : _impl(new ThreadImpl()) , _name(name) , _thread_exists(false) , _own_thread(true) + , _exit_flag(false) { } /** Must be called from thread */ Thread::Thread(pthread_t thread, const std::string& name) - : _exit_flag(false) - , _impl(new ThreadImpl()) + : _impl(new ThreadImpl()) , _name(name) , _thread_exists(true) , _own_thread(false) + , _exit_flag(false) { _impl->pthread = thread; } @@ -81,7 +81,6 @@ Thread::_static_run(void* thread) return NULL; } -/** Launch and start the thread. */ void Thread::start() { @@ -97,7 +96,6 @@ Thread::start() } } -/** Stop and terminate the thread. */ void Thread::stop() { @@ -119,23 +117,19 @@ Thread::join() } void -Thread::set_scheduling(int policy, unsigned int priority) +Thread::set_scheduling(bool realtime, unsigned priority) { sched_param sp; sp.sched_priority = priority; - int result = pthread_setschedparam(_impl->pthread, policy, &sp); + const int policy = realtime ? SCHED_FIFO : SCHED_OTHER; + const int result = pthread_setschedparam(_impl->pthread, policy, &sp); if (!result) { - LOG(info) << "Set scheduling policy to "; - switch (policy) { - case SCHED_FIFO: info << "SCHED_FIFO"; break; - case SCHED_RR: info << "SCHED_RR"; break; - case SCHED_OTHER: info << "SCHED_OTHER"; break; - default: info << "UNKNOWN"; break; - } - info << ", priority " << sp.sched_priority << endl; + LOG(info) << (fmt("Set scheduling policy to %1% priority %2%\n") + % (realtime ? "realtime" : "normal") + % sp.sched_priority); } else { - LOG(info) << "Unable to set scheduling policy (" - << strerror(result) << ")" << endl; + LOG(info) << (fmt("Unable to set scheduling policy (%1%)\n") + % strerror(result)); } } diff --git a/test/queue_test.cpp b/test/queue_test.cpp index 8685f15..e4ad230 100644 --- a/test/queue_test.cpp +++ b/test/queue_test.cpp @@ -14,17 +14,20 @@ along with Raul. If not, see . */ +#include +#include +#include + +#include #include #include #include -#include -#include -#include -#include -#include "raul/SRSWQueue.hpp" + +#include "raul/AtomicInt.hpp" #include "raul/SRMWQueue.hpp" +#include "raul/SRSWQueue.hpp" #include "raul/Thread.hpp" -#include "raul/AtomicInt.hpp" +#include "raul/log.hpp" using namespace std; using namespace Raul; @@ -59,6 +62,9 @@ struct WriteAction { SRMWQueue queue(QUEUE_SIZE); class WriteThread : public Thread { +public: + WriteThread(const std::string& name) : Thread(name) {} + protected: void _run() { while (true) { @@ -140,10 +146,10 @@ main() } cout << "Testing concurrent reading/writing" << endl; - vector writers(NUM_WRITERS, new WriteThread()); + vector writers(NUM_WRITERS, NULL); for (unsigned i=0; i < NUM_WRITERS; ++i) { - writers[i]->set_name(string("Writer ") + static_cast('0' + i)); + writers[i] = new WriteThread((Raul::fmt("Writer %1%") % i).str()); writers[i]->start(); } diff --git a/test/sem_test.cpp b/test/sem_test.cpp index f3ed065..f8b0741 100644 --- a/test/sem_test.cpp +++ b/test/sem_test.cpp @@ -24,8 +24,7 @@ using namespace Raul; class Waiter : public Raul::Thread { public: - Waiter(Semaphore& sem) : _sem(sem) { - Thread::set_name("Waiter"); + Waiter(Semaphore& sem) : Raul::Thread("Waiter"), _sem(sem) { } private: @@ -47,9 +46,6 @@ private: int main() { - Thread& this_thread = Thread::get(); - this_thread.set_name("Main"); - Semaphore sem(0); Waiter waiter(sem); waiter.start(); diff --git a/test/thread_test.cpp b/test/thread_test.cpp index 738b30c..b74db72 100644 --- a/test/thread_test.cpp +++ b/test/thread_test.cpp @@ -23,8 +23,7 @@ using namespace Raul; class Waiter : public Raul::Thread { public: - Waiter(Semaphore& sem) : _sem(sem) { - Thread::set_name("Waiter"); + Waiter(Semaphore& sem) : Raul::Thread("Waiter"), _sem(sem) { } private: @@ -40,9 +39,6 @@ private: int main() { - Thread& this_thread = Thread::get(); - this_thread.set_name("Main"); - Semaphore sem(0); Waiter waiter(sem); waiter.start(); -- cgit v1.2.1