Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/crystal/once.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
# :nodoc:
class Crystal::OnceState
@rec = [] of Bool*
{% if flag?(:preview_mt) %}
@mutex = Mutex.new(:reentrant)
{% end %}

def once(flag : Bool*, initializer : Void*)
unless flag.value
Expand All @@ -29,7 +26,13 @@ class Crystal::OnceState
end
end

{% if flag?(:preview_mt) %}
# on Win32, `Crystal::System::FileDescriptor#@@reader_thread` spawns a new
# thread even without the `preview_mt` flag, and the thread can also reference
# Crystal constants, leading to race conditions, so we always enable the mutex
# TODO: can this be improved?
{% if flag?(:preview_mt) || flag?(:win32) %}
@mutex = Mutex.new(:reentrant)

def once(flag : Bool*, initializer : Void*)
unless flag.value
@mutex.synchronize do
Expand Down