Skip to content

Commit 68c47c7

Browse files
projediSpace Team
authored and
Space Team
committed
[K/N] Use SpinLock for GlobalData::init() on Windows
There is a weird bug on Windows when sometimes GlobalData has started initialization and created the timer thread for the GC scheduler; but when the timer thread tries to wait for the GlobalData initialization to finish, it crashes on trying to lock the synchronization mutex. GlobalData initialization does successfully take the synchronization mutex, so the mutex should definitely be initialized. Replace std::mutex with SpinLock on Windows to workaround the problem. ^KT-71644
1 parent 85ad07a commit 68c47c7

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

kotlin-native/runtime/src/mm/cpp/GlobalData.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include "CompilerConstants.hpp"
1212
#include "Porting.h"
1313

14+
#if KONAN_WINDOWS
15+
#include "concurrent/Mutex.hpp"
16+
#endif
17+
1418
using namespace kotlin;
1519

1620
namespace {
@@ -33,8 +37,17 @@ std::atomic<InitState> globalDataInitState = InitState::kUninitialized;
3337
std::atomic<std::thread::id> globalDataInitializingThread{};
3438
ManuallyScoped<mm::GlobalData> globalDataInstance{};
3539

40+
#if KONAN_WINDOWS
41+
// On winpthreads, there's a weird bug if this is a regular `std::mutex`:
42+
// even though `constructGlobalDataInstance()` has already started (and so,
43+
// has already successfully tried locking this mutex), `waitGlobalDataInitialized`
44+
// may crash trying to lock it too.
45+
SpinLock globalDataInitMutex;
46+
std::condition_variable_any globalDataInitCV;
47+
#else
3648
std::mutex globalDataInitMutex;
3749
std::condition_variable globalDataInitCV;
50+
#endif
3851

3952
void constructGlobalDataInstance() noexcept {
4053
std::unique_lock guard{globalDataInitMutex};

0 commit comments

Comments
 (0)