Skip to content

Commit b7e8cf2

Browse files
committed
Use lock-guard instead of manual mutex un-/locking
1 parent 4e5513b commit b7e8cf2

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

source/globjects/source/globjects.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,32 @@ namespace globjects
9898

9999
void init()
100100
{
101-
g_mutex.lock();
102-
if (!g_globjectsIsInitialized)
103101
{
104-
initializeCallbacks();
102+
std::lock_guard<std::mutex> lock(g_mutex);
105103

106-
g_globjectsIsInitialized = true;
107-
}
104+
if (!g_globjectsIsInitialized)
105+
{
106+
initializeCallbacks();
108107

109-
g_mutex.unlock();
108+
g_globjectsIsInitialized = true;
109+
}
110+
}
110111

111112
registerCurrentContext();
112113
}
113114

114115
void init(const glbinding::ContextHandle sharedContextId)
115116
{
116-
g_mutex.lock();
117-
if (!g_globjectsIsInitialized)
118117
{
119-
initializeCallbacks();
118+
std::lock_guard<std::mutex> lock(g_mutex);
119+
120+
if (!g_globjectsIsInitialized)
121+
{
122+
initializeCallbacks();
120123

121-
g_globjectsIsInitialized = true;
124+
g_globjectsIsInitialized = true;
125+
}
122126
}
123-
g_mutex.unlock();
124127

125128
registerCurrentContext(sharedContextId);
126129
}

source/globjects/source/registry/Registry.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,14 @@ void Registry::registerContext(const glbinding::ContextHandle contextId, const g
4646
globjects::debug() << "OpenGL context " << contextId << " is already registered";
4747
}
4848

49-
g_mutex.lock();
49+
std::lock_guard<std::recursive_mutex> lock(g_mutex);
50+
5051
auto it = s_registries.find(sharedContextId);
5152
assert(it != s_registries.end());
52-
g_mutex.unlock();
5353

5454
Registry * registry = new Registry(it->second);
5555

56-
g_mutex.lock();
5756
s_registries[contextId] = registry;
58-
g_mutex.unlock();
5957

6058
t_currentRegistry = registry;
6159
//registry->initialize();
@@ -73,13 +71,11 @@ void Registry::setCurrentContext(const glbinding::ContextHandle contextId)
7371

7472
bool Registry::isCurrentContext(glbinding::ContextHandle contextId)
7573
{
76-
g_mutex.lock();
74+
std::lock_guard<std::recursive_mutex> lock(g_mutex);
7775

7876
const auto it = s_registries.find(contextId);
7977
const auto result = it != s_registries.end() && it->second == t_currentRegistry;
8078

81-
g_mutex.unlock();
82-
8379
return result;
8480
}
8581

@@ -92,13 +88,13 @@ void Registry::deregisterContext(const glbinding::ContextHandle contextId)
9288
return;
9389
}
9490

95-
g_mutex.lock();
96-
delete s_registries[contextId];
97-
g_mutex.unlock();
91+
{
92+
std::lock_guard<std::recursive_mutex> lock(g_mutex);
9893

99-
g_mutex.lock();
100-
s_registries[contextId] = nullptr;
101-
g_mutex.unlock();
94+
delete s_registries[contextId];
95+
96+
s_registries[contextId] = nullptr;
97+
}
10298

10399
t_currentRegistry = nullptr;
104100
}
@@ -112,33 +108,28 @@ Registry & Registry::current()
112108

113109
bool Registry::isContextRegistered(const glbinding::ContextHandle contextId)
114110
{
115-
g_mutex.lock();
111+
std::lock_guard<std::recursive_mutex> lock(g_mutex);
112+
116113
bool found = s_registries.find(contextId) != s_registries.end();
117-
g_mutex.unlock();
118114

119115
return found;
120116
}
121117

122118
void Registry::setCurrentRegistry(const glbinding::ContextHandle contextId)
123119
{
124-
g_mutex.lock();
120+
std::lock_guard<std::recursive_mutex> lock(g_mutex);
121+
125122
auto it = s_registries.find(contextId);
126123

127124
if (it != s_registries.end())
128125
{
129126
t_currentRegistry = it->second;
130-
131-
g_mutex.unlock();
132127
}
133128
else
134129
{
135-
g_mutex.unlock();
136-
137130
Registry * registry = new Registry();
138131

139-
g_mutex.lock();
140132
s_registries[contextId] = registry;
141-
g_mutex.unlock();
142133

143134
t_currentRegistry = registry;
144135
registry->initialize();

0 commit comments

Comments
 (0)