summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/Local.h12
-rw-r--r--src/Local.cc28
-rw-r--r--src/Task.cc2
-rw-r--r--src/Threads.cc2
4 files changed, 24 insertions, 20 deletions
diff --git a/includes/Local.h b/includes/Local.h
index 6a598ab..9221384 100644
--- a/includes/Local.h
+++ b/includes/Local.h
@@ -8,7 +8,15 @@ class TLSManager {
public:
virtual void * getTLS();
virtual void * setTLS(void * val);
- void * createTLS();
+};
+
+class PThreadsTLSManager : public TLSManager {
+ public:
+ virtual void * getTLS();
+ virtual void * setTLS(void * val);
+ void init();
+ private:
+ pthread_key_t m_key;
};
extern TLSManager * g_tlsManager;
@@ -16,6 +24,7 @@ extern TLSManager * g_tlsManager;
class Local : public AtStart {
public:
static int getSize() { return s_size; }
+ static void * createTLS() { void * r = calloc(s_size * sizeof(void *), 1); return r; }
protected:
Local() : AtStart(0) { }
void * getGlobal() { return m_globals[m_idx]; }
@@ -26,7 +35,6 @@ class Local : public AtStart {
void set(void * obj) { void * r = getTLS(); if (r) setLocal(obj); else setGlobal(obj); }
int getIndex() { return m_idx; }
private:
- static void * create() { void * r = calloc(s_size * sizeof(void *), 1); return r; }
static void * getTLS() { return g_tlsManager->getTLS(); }
static void * setTLS(void * val) { return g_tlsManager->setTLS(val); }
virtual void doStart();
diff --git a/src/Local.cc b/src/Local.cc
index 9729c35..a9eccdd 100644
--- a/src/Local.cc
+++ b/src/Local.cc
@@ -13,10 +13,6 @@ void * Balau::TLSManager::setTLS(void * val) {
return r;
}
-void * Balau::TLSManager::createTLS() {
- return Local::create();
-}
-
static Balau::TLSManager dummyTLSManager;
Balau::TLSManager * Balau::g_tlsManager = &dummyTLSManager;
@@ -29,31 +25,31 @@ void Balau::Local::doStart() {
m_globals[m_idx] = 0;
}
-class PThreadsTLSManager : public Balau::TLSManager, public Balau::AtStart {
+class GlobalPThreadsTLSManager : public Balau::PThreadsTLSManager, public Balau::AtStart {
public:
- PThreadsTLSManager() : AtStart(0) { }
- virtual void * getTLS();
- virtual void * setTLS(void * val);
- virtual void doStart();
- private:
- pthread_key_t m_key;
+ GlobalPThreadsTLSManager() : AtStart(0) { }
+ void doStart();
};
-PThreadsTLSManager pthreadsTLSManager;
+GlobalPThreadsTLSManager pthreadsTLSManager;
+
+void GlobalPThreadsTLSManager::doStart() {
+ init();
+ Balau::g_tlsManager = this;
+}
-void PThreadsTLSManager::doStart() {
+void Balau::PThreadsTLSManager::init() {
int r;
r = pthread_key_create(&m_key, NULL);
RAssert(r == 0, "Unable to create a pthtread_key: %i", r);
- Balau::g_tlsManager = this;
}
-void * PThreadsTLSManager::getTLS() {
+void * Balau::PThreadsTLSManager::getTLS() {
return pthread_getspecific(m_key);
}
-void * PThreadsTLSManager::setTLS(void * val) {
+void * Balau::PThreadsTLSManager::setTLS(void * val) {
void * r = pthread_getspecific(m_key);
pthread_setspecific(m_key, val);
return r;
diff --git a/src/Task.cc b/src/Task.cc
index 6278fd5..aba29b8 100644
--- a/src/Task.cc
+++ b/src/Task.cc
@@ -35,7 +35,7 @@ void Balau::Task::setup(TaskMan * taskMan, void * stack) {
m_taskMan = taskMan;
- m_tls = g_tlsManager->createTLS();
+ m_tls = Local::createTLS();
void * oldTLS = g_tlsManager->getTLS();
g_tlsManager->setTLS(m_tls);
localTask.set(this);
diff --git a/src/Threads.cc b/src/Threads.cc
index fe90394..9ecfff5 100644
--- a/src/Threads.cc
+++ b/src/Threads.cc
@@ -39,7 +39,7 @@ Balau::RWLock::RWLock() {
}
void * Balau::ThreadHelper::threadProc(void * arg) {
- void * tls = g_tlsManager->createTLS();
+ void * tls = Local::createTLS();
g_tlsManager->setTLS(tls);
Balau::Thread * thread = reinterpret_cast<Balau::Thread *>(arg);
void * r = thread->proc();