summaryrefslogtreecommitdiff
path: root/src/mbgl/util/thread_context.cpp
blob: b611b3d02303d9ba633da9f8e76dc8c445011911 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <mbgl/util/thread_context.hpp>

#include <mbgl/util/thread_local.hpp>

#include <cassert>

namespace {

using namespace mbgl::util;
static ThreadLocal<ThreadContext>& current = *new ThreadLocal<ThreadContext>;

} // namespace

namespace mbgl {
namespace util {

ThreadContext::ThreadContext(const std::string& name_, ThreadType type_, ThreadPriority priority_)
    : name(name_),
      type(type_),
      priority(priority_) {
}

void ThreadContext::Set(ThreadContext* context) {
    current.set(context);
}

bool ThreadContext::currentlyOn(ThreadType type) {
    return current.get()->type == type;
}

std::string ThreadContext::getName() {
    if (current.get() != nullptr) {
        return current.get()->name;
    } else {
        return "Unknown";
    }
}

ThreadPriority ThreadContext::getPriority() {
    if (current.get() != nullptr) {
        return current.get()->priority;
    } else {
        return ThreadPriority::Regular;
    }
}

FileSource* ThreadContext::getFileSource() {
    if (current.get() != nullptr) {
        return current.get()->fileSource;
    } else {
        return nullptr;
    }
}

void ThreadContext::setFileSource(FileSource* fileSource) {
    if (current.get() != nullptr) {
        current.get()->fileSource = fileSource;
    } else {
        throw std::runtime_error("Current thread has no current ThreadContext.");
    }
}

GLObjectStore* ThreadContext::getGLObjectStore() {
    if (current.get() != nullptr) {
        return current.get()->glObjectStore;
    } else {
        return nullptr;
    }
}

void ThreadContext::setGLObjectStore(GLObjectStore* glObjectStore) {
    if (current.get() != nullptr) {
        current.get()->glObjectStore = glObjectStore;
    } else {
        throw std::runtime_error("Current thread has no current ThreadContext.");
    }
}

class MainThreadContextRegistrar {
public:
    MainThreadContextRegistrar() : context("Main", ThreadType::Main, ThreadPriority::Regular) {
        ThreadContext::Set(&context);
    }

    ~MainThreadContextRegistrar() {
        ThreadContext::Set(nullptr);
    }

private:
    ThreadContext context;
};

// Will auto register the main thread context
// at startup. Must be instantiated after the
// ThreadContext::current object.
MainThreadContextRegistrar registrar;

} // namespace util
} // namespace mbgl