summaryrefslogtreecommitdiff
path: root/src/mbgl/map/backend_scope.cpp
blob: 824ad4498b7ea060259ca2df8544b615feba8c66 (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
#include <mbgl/map/backend_scope.hpp>
#include <mbgl/map/backend.hpp>
#include <mbgl/util/thread_local.hpp>

#include <cassert>

namespace mbgl {

static util::ThreadLocal<BackendScope> currentScope;

BackendScope::BackendScope(Backend& backend_, ScopeType scopeType_)
    : priorScope(currentScope.get()),
      nextScope(nullptr),
      backend(backend_),
      scopeType(scopeType_) {
    if (priorScope) {
        assert(priorScope->nextScope == nullptr);
        priorScope->nextScope = this;
    }
    if (scopeType == ScopeType::Explicit) {
        backend.activate();
    }

    currentScope.set(this);
}

BackendScope::~BackendScope() {
    assert(nextScope == nullptr);
    if (priorScope) {
        priorScope->backend.activate();
        currentScope.set(priorScope);
        assert(priorScope->nextScope == this);
        priorScope->nextScope = nullptr;
    } else {
        if (scopeType == ScopeType::Explicit) {
            backend.deactivate();
        }

        currentScope.set(nullptr);
    }
}

bool BackendScope::exists() {
    return currentScope.get();
}

} // namespace mbgl