Skip to content

Commit 7d1ad55

Browse files
committed
Better mutexes
1 parent cf242de commit 7d1ad55

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

offload/liboffload/src/OffloadImpl.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ using PlatformVecT = SmallVector<ol_platform_impl_t, 4>;
101101
static PlatformVecT *PlatformList;
102102
PlatformVecT &Platforms() { return *PlatformList; }
103103

104-
static std::atomic_int &GlobalRefCount() {
105-
static std::atomic_int Ref{0};
106-
return Ref;
107-
}
104+
static std::mutex InitDeinitMtx;
105+
static uint32_t RefCount = 0;
108106

109107
ol_device_handle_t HostDevice() {
110108
// The host platform is always inserted last
@@ -173,20 +171,18 @@ void initPlugins() {
173171
}
174172

175173
Error olInit_impl() {
176-
// While the refcount increment ensures that only thread performs
177-
// initialization, we need to ensure that other threads are blocked until it
178-
// is completed - hence this mutex.
179-
static std::mutex Init{};
180-
std::lock_guard<std::mutex> Guard{Init};
174+
std::lock_guard<std::mutex> Guard{InitDeinitMtx};
181175

182-
if (++GlobalRefCount() == 1)
176+
if (++RefCount == 1)
183177
initPlugins();
184178

185179
return Error::success();
186180
}
187181

188182
Error olShutDown_impl() {
189-
if (--GlobalRefCount() != 0)
183+
std::lock_guard<std::mutex> Guard{InitDeinitMtx};
184+
185+
if (--RefCount != 0)
190186
return Error::success();
191187

192188
llvm::Error Result = Error::success();

0 commit comments

Comments
 (0)