summaryrefslogtreecommitdiff
path: root/src/Local.cc
blob: a9eccdd600e228f0348c027f06e890e9d557608b (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
#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;
}

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() {
    m_idx = s_size++;
    m_globals = reinterpret_cast<void **>(realloc(m_globals, s_size * sizeof(void *)));
    m_globals[m_idx] = 0;
}

class GlobalPThreadsTLSManager : public Balau::PThreadsTLSManager, public Balau::AtStart {
  public:
      GlobalPThreadsTLSManager() : AtStart(0) { }
    void doStart();
};

GlobalPThreadsTLSManager pthreadsTLSManager;

void GlobalPThreadsTLSManager::doStart() {
    init();
    Balau::g_tlsManager = this;
}

void Balau::PThreadsTLSManager::init() {
    int r;

    r = pthread_key_create(&m_key, NULL);
    RAssert(r == 0, "Unable to create a pthtread_key: %i", r);
}

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

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