Skip to content

Deprecate Atomic::Flag #15805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/atomic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ end
# flag.test_and_set # => true
# ```
struct Atomic::Flag
@[Deprecated("Use Atomic(Bool) instead.")]
Comment on lines 461 to +462
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Shouldn't we deprecate the entire type instead of just its constructor?

Suggested change
struct Atomic::Flag
@[Deprecated("Use Atomic(Bool) instead.")]
@[Deprecated("Use Atomic(Bool) instead.")]
struct Atomic::Flag

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but then I don't get any warning, which is odd.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def initialize
@value = Atomic(Bool).new(false)
end
Expand Down
8 changes: 4 additions & 4 deletions src/crystal/event_loop/epoll.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Crystal::EventLoop::Epoll < Crystal::EventLoop::Polling
@epoll = System::Epoll.new

# notification to interrupt a run
@interrupted = Atomic::Flag.new
@interrupted = Atomic(Bool).new(false)
@eventfd = System::EventFD.new
@epoll.add(@eventfd.fd, LibC::EPOLLIN, u64: @eventfd.fd.to_u64!)

Expand Down Expand Up @@ -41,7 +41,7 @@ class Crystal::EventLoop::Epoll < Crystal::EventLoop::Polling
# create new fds
@epoll = System::Epoll.new

@interrupted.clear
@interrupted.set(false, :relaxed)
@eventfd = System::EventFD.new
@epoll.add(@eventfd.fd, LibC::EPOLLIN, u64: @eventfd.fd.to_u64!)

Expand Down Expand Up @@ -72,7 +72,7 @@ class Crystal::EventLoop::Epoll < Crystal::EventLoop::Polling
# TODO: panic if epoll_event.value.events != LibC::EPOLLIN (could be EPOLLERR or EPLLHUP)
Crystal.trace :evloop, "interrupted"
@eventfd.read
@interrupted.clear
@interrupted.set(false, :relaxed)
when @timerfd.fd
# TODO: panic if epoll_event.value.events != LibC::EPOLLIN (could be EPOLLERR or EPLLHUP)
Crystal.trace :evloop, "timer"
Expand Down Expand Up @@ -113,7 +113,7 @@ class Crystal::EventLoop::Epoll < Crystal::EventLoop::Polling

def interrupt : Nil
# the atomic makes sure we only write once
@eventfd.write(1) if @interrupted.test_and_set
@eventfd.write(1) unless @interrupted.swap(true, :relaxed)
end

protected def system_add(fd : Int32, index : Polling::Arena::Index) : Nil
Expand Down
10 changes: 5 additions & 5 deletions src/crystal/event_loop/kqueue.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Crystal::EventLoop::Kqueue < Crystal::EventLoop::Polling
@kqueue = System::Kqueue.new

# notification to interrupt a run
@interrupted = Atomic::Flag.new
@interrupted = Atomic(Bool).new(false)

{% if LibC.has_constant?(:EVFILT_USER) %}
@kqueue.kevent(
Expand Down Expand Up @@ -50,7 +50,7 @@ class Crystal::EventLoop::Kqueue < Crystal::EventLoop::Polling
@kqueue.close
@kqueue = System::Kqueue.new

@interrupted.clear
@interrupted.set(false, :relaxed)

{% if LibC.has_constant?(:EVFILT_USER) %}
@kqueue.kevent(
Expand Down Expand Up @@ -100,15 +100,15 @@ class Crystal::EventLoop::Kqueue < Crystal::EventLoop::Polling
private def process_interrupt?(kevent)
{% if LibC.has_constant?(:EVFILT_USER) %}
if kevent.value.filter == LibC::EVFILT_USER
@interrupted.clear if kevent.value.ident == INTERRUPT_IDENTIFIER
@interrupted.set(false, :relaxed) if kevent.value.ident == INTERRUPT_IDENTIFIER
return true
end
{% else %}
if kevent.value.filter == LibC::EVFILT_READ && kevent.value.ident == @pipe[0]
ident = 0
ret = LibC.read(@pipe[0], pointerof(ident), sizeof(Int32))
raise RuntimeError.from_errno("read") if ret == -1
@interrupted.clear if ident == INTERRUPT_IDENTIFIER
@interrupted.set(false, :relaxed) if ident == INTERRUPT_IDENTIFIER
return true
end
{% end %}
Expand Down Expand Up @@ -156,7 +156,7 @@ class Crystal::EventLoop::Kqueue < Crystal::EventLoop::Polling
end

def interrupt : Nil
return unless @interrupted.test_and_set
return if @interrupted.swap(true, :relaxed)

{% if LibC.has_constant?(:EVFILT_USER) %}
@kqueue.kevent(INTERRUPT_IDENTIFIER, LibC::EVFILT_USER, 0, LibC::NOTE_TRIGGER)
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/thread.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Thread

@system_handle : Crystal::System::Thread::Handle
@exception : Exception?
@detached = Atomic::Flag.new
@detached = Atomic(Bool).new(false)

# Returns the Fiber representing the thread's main stack.
getter! main_fiber : Fiber
Expand Down Expand Up @@ -155,7 +155,7 @@ class Thread
end

private def detach(&)
if @detached.test_and_set
unless @detached.swap(true, :relaxed)
yield
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/crystal/system/unix/signal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ module Crystal::System::Signal
LibC._exit(1)
end

@@setup_default_handlers = Atomic::Flag.new
@@setup_segfault_handler = Atomic::Flag.new
@@setup_default_handlers = Atomic(Bool).new(false)
@@setup_segfault_handler = Atomic(Bool).new(false)
@@segfault_handler = LibC::SigactionHandlerT.new { |sig, info, data|
# Capture fault signals (SEGV, BUS) and finish the process printing a backtrace first

Expand Down Expand Up @@ -202,7 +202,7 @@ module Crystal::System::Signal
}

def self.setup_default_handlers : Nil
return unless @@setup_default_handlers.test_and_set
return if @@setup_default_handlers.swap(true, :relaxed)
@@sigset.clear
start_loop

Expand All @@ -219,7 +219,7 @@ module Crystal::System::Signal
end

def self.setup_segfault_handler
return unless @@setup_segfault_handler.test_and_set
return if @@setup_segfault_handler.swap(true, :relaxed)

altstack = LibC::StackT.new
altstack.ss_sp = LibC.malloc(LibC::SIGSTKSZ)
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/win32/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct Crystal::System::Process
@@interrupt_handler : Proc(::Process::ExitReason, Nil)?
@@interrupt_count = Crystal::AtomicSemaphore.new
@@win32_interrupt_handler : LibC::PHANDLER_ROUTINE?
@@setup_interrupt_handler = Atomic::Flag.new
@@setup_interrupt_handler = Atomic(Bool).new(false)
@@last_interrupt = ::Process::ExitReason::Interrupted

def initialize(process_info)
Expand Down Expand Up @@ -201,7 +201,7 @@ struct Crystal::System::Process
end

def self.start_interrupt_loop : Nil
return unless @@setup_interrupt_handler.test_and_set
return if @@setup_interrupt_handler.swap(true, :relaxed)

spawn(name: "interrupt-signal-loop") do
while true
Expand Down