summaryrefslogtreecommitdiff
path: root/src/Local.cc
blob: 805ecdbbccdf7c87602f27e468a2c2d44f4ff0a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "Local.h"
#include "Main.h"

static void * dummyTLS = NULL;

void * Balau::TLSManager::getTLS() {
    return dummyTLS;
}

void * Balau::TLSManager::setTLS(void * val) {
    void * r = dummyTLS;
    dummyTLS = val;
    return r;
}

void * Balau::TLSManager::createTLS() {
    return Local::create();
}

static Balau::TLSManager dummyTLSManager;
Balau::TLSManager * Balau::g_tlsManager = &dummyTLSManager;

int Balau::Local::s_size = 0;
void ** Balau::Local::m_globals = 0;

void Balau::Local::doStart() {
    Assert(Main::status() == Main::STARTING);
    m_idx = s_size++;
    m_globals = reinterpret_cast<void **>(realloc(m_globals, s_size * sizeof(void *)));
    m_globals[m_idx] = 0;
}

class PThreadsTLSManager : public Balau::TLSManager, public Balau::AtStart {
  public:
      PThreadsTLSManager() : AtStart(0) { }
    virtual void * getTLS();
    virtual void * setTLS(void * val);
    virtual void doStart();
  private:
    pthread_key_t m_key;
};

PThreadsTLSManager pthreadsTLSManager;

void PThreadsTLSManager::doStart() {
    int r;

    r = pthread_key_create(&m_key, NULL);
    Assert(r == 0);
    Balau::g_tlsManager = this;
}

void * PThreadsTLSManager::getTLS() {
    return pthread_getspecific(m_key);
}

void * PThreadsTLSManager::setTLS(void * val) {
    void * r = pthread_getspecific(m_key);
    pthread_setspecific(m_key, val);
    return r;
}