summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2012-05-14 04:30:00 +0000
committerDavid Robillard <d@drobilla.net>2012-05-14 04:30:00 +0000
commit79deafe642561936ebb3bbcf585f2c6f26b456d3 (patch)
tree896f4486237286d09440399c25c50ca97e940636 /src
parent52e92126896c9d4e2ed2e0f3db7dd70809a473d6 (diff)
downloadraul-79deafe642561936ebb3bbcf585f2c6f26b456d3.tar.gz
raul-79deafe642561936ebb3bbcf585f2c6f26b456d3.tar.bz2
raul-79deafe642561936ebb3bbcf585f2c6f26b456d3.zip
Remove Thread context stuff and add a thread-specific variable class, ThreadVar, which can be used for this and many other things.
ClientBroadcaster => Broadcaster. git-svn-id: http://svn.drobilla.net/lad/trunk/raul@4405 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src')
-rw-r--r--src/Thread.cpp32
1 files changed, 8 insertions, 24 deletions
diff --git a/src/Thread.cpp b/src/Thread.cpp
index 353e9b7..4a3e7bf 100644
--- a/src/Thread.cpp
+++ b/src/Thread.cpp
@@ -21,6 +21,7 @@
#include "raul/log.hpp"
#include "raul/Thread.hpp"
+#include "raul/ThreadVar.hpp"
#define LOG(s) (s("[")(_name)("] "))
@@ -32,12 +33,7 @@ struct ThreadImpl {
pthread_t pthread;
};
-static pthread_once_t s_thread_key_once = PTHREAD_ONCE_INIT;
-static pthread_key_t s_thread_key;
-
-static void thread_key_alloc() {
- pthread_key_create(&s_thread_key, NULL);
-}
+static ThreadVar<Thread*> self(NULL);
Thread::Thread(const std::string& name)
: _exit_flag(false)
@@ -46,8 +42,6 @@ Thread::Thread(const std::string& name)
, _thread_exists(false)
, _own_thread(true)
{
- pthread_once(&s_thread_key_once, thread_key_alloc);
- pthread_setspecific(s_thread_key, this);
}
/** Must be called from thread */
@@ -59,8 +53,6 @@ Thread::Thread(pthread_t thread, const std::string& name)
, _own_thread(false)
{
_impl->pthread = thread;
- pthread_once(&s_thread_key_once, thread_key_alloc);
- pthread_setspecific(s_thread_key, this);
}
Thread::~Thread()
@@ -69,29 +61,21 @@ Thread::~Thread()
delete _impl;
}
-Thread*
-Thread::create_for_this_thread(const std::string& name)
-{
- return new Thread(pthread_self(), name);
-}
-
Thread&
-Thread::get()
+Thread::get(const std::string& name)
{
- pthread_once(&s_thread_key_once, thread_key_alloc);
- Thread* this_thread = reinterpret_cast<Thread*>(
- pthread_getspecific(s_thread_key));
- if (!this_thread)
- this_thread = create_for_this_thread("");
+ if (!self) {
+ self = new Thread(pthread_self(), name);
+ }
- return *this_thread;
+ return *self;
}
void*
Thread::_static_run(void* thread)
{
Thread* me = static_cast<Thread*>(thread);
- pthread_setspecific(s_thread_key, thread);
+ self = me;
me->_run();
me->_thread_exists = false;
return NULL;