Class: Prosody::CancellationToken
- Inherits:
-
Object
- Object
- Prosody::CancellationToken
- Defined in:
- lib/prosody/processor.rb
Overview
Provides a mechanism for canceling asynchronous tasks.
This class implements a simple cancellation mechanism using a Ruby Queue, allowing tasks to be safely canceled while they’re in progress. Each token maintains its own queue for signaling cancellation.
Instance Method Summary collapse
-
#cancel ⇒ Object
Signals that the associated task should be canceled.
-
#initialize ⇒ CancellationToken
constructor
Creates a new cancellation token with an internal queue for signaling.
-
#wait ⇒ Boolean
Blocks until cancellation is requested.
Constructor Details
#initialize ⇒ CancellationToken
Creates a new cancellation token with an internal queue for signaling.
16 17 18 |
# File 'lib/prosody/processor.rb', line 16 def initialize @queue = Queue.new end |
Instance Method Details
#cancel ⇒ Object
Signals that the associated task should be canceled.
This method pushes a cancellation signal to the internal queue, which will wake up any threads waiting on #wait.
24 25 26 |
# File 'lib/prosody/processor.rb', line 24 def cancel @queue.push(:cancel) end |
#wait ⇒ Boolean
Blocks until cancellation is requested.
This method blocks the current thread until the token is canceled by another thread calling #cancel.
34 35 36 37 |
# File 'lib/prosody/processor.rb', line 34 def wait @queue.pop true end |