Class: Farce::TimerQueue
- Inherits:
-
Abstract::TimerQueue
- Object
- Abstract::TimerQueue
- Farce::TimerQueue
- Includes:
- Shareable::Unfreezable, Internal::ManagedQueue
- Defined in:
- lib/farce/timer_queue.rb
Overview
A shareable timer queue with transfer modes for unshareable values.
Instance Method Summary collapse
-
#initialize(capacity: nil, mode: :copy, track_age: false) ⇒ BasicObject
constructor
Valid modes are: *
:copy- The value will be copied between Ractors. -
#peek { ... } ⇒ BasicObject?
Return the earliest value without removing it, whether or not it is due.
-
#pop(non_block = false, timeout: nil) { ... } ⇒ BasicObject?
Remove the earliest value, waiting until its timestamp is reached.
-
#push(value, non_block = false, mode: nil, timeout: nil, **time_options) ⇒ Boolean
Add a value to become available at the given time.
-
#try_pop { ... } ⇒ BasicObject?
Try to remove the earliest value only if its timestamp has been reached.
-
#try_push(value, mode: nil, **time_options) { ... } ⇒ Boolean, BasicObject
Try to add a value without waiting for capacity.
Methods included from Shareable
Constructor Details
#initialize(capacity: nil, mode: :copy, track_age: false) ⇒ BasicObject
Valid modes are:
:copy- The value will be copied between Ractors. This is the default mode.:make_shareable- The value will be made Ractor-shareable using Ractor.make_shareable.:move- The value will be moved between Ractors. This saves memory compared to copying, and supports values that can't be copied but moved (like IO objects). However, the value will no longer be accessible on the Ractor that pushed it.:mutable- A Mutable instance will be created for the value. This isn't done recursively and thus will fail for nested unshareable values.:local- The value will be kept local to the Ractor that pushed it. Another ractor trying to receive it will get an error. Useful for usage contained within a single Ractor.:proxy- The value will be wrapped in aFarce::Proxythat executes calls in the original Ractor.:raise- An error will be raised if the value is not Ractor-shareable. Useful for enforcing shareability.:dedup- The value will be deduplicated using Farce.dedup, then made Ractor-shareable. This may update and freeze the original. Already-shareable values pass through unchanged.:shareable_copy- The value will be copied and the copy will be made Ractor-shareable.
14 15 16 17 |
# File 'lib/farce/timer_queue.rb', line 14 def initialize(capacity: nil, mode: :copy, track_age: false) @manager = ModeManager.new(mode:) super(capacity:, track_age:) end |
Instance Method Details
#peek { ... } ⇒ BasicObject?
Peeking at a moved value claims it for this Ractor while leaving it queued.
Return the earliest value without removing it, whether or not it is due.
59 60 61 62 63 64 |
# File 'lib/farce/timer_queue.rb', line 59 def peek empty = false value = @queue.peek { empty = true } return @manager.unwrap(value) unless empty yield if block_given? end |
#pop(non_block = false, timeout: nil) { ... } ⇒ BasicObject?
Remove the earliest value, waiting until its timestamp is reached.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/farce/timer_queue.rb', line 37 def pop(non_block = false, timeout: nil) # rubocop:disable Style/OptionalBooleanParameter return try_pop { raise ThreadError, "queue empty" } if non_block deadline = timeout_at(timeout) unless timeout.nil? while true empty = false value = @queue.pop_before(Clock.now) { empty = true } return @manager.unwrap(value) unless empty return block_given? ? yield : nil if UNDEFINED.equal?((deadline)) end end |
#push(value, non_block = false, mode: nil, timeout: nil, **time_options) ⇒ Boolean
Add a value to become available at the given time. Valid modes are:
:copy- The value will be copied between Ractors. This is the default mode.:make_shareable- The value will be made Ractor-shareable using Ractor.make_shareable.:move- The value will be moved between Ractors. This saves memory compared to copying, and supports values that can't be copied but moved (like IO objects). However, the value will no longer be accessible on the Ractor that pushed it.:mutable- A Mutable instance will be created for the value. This isn't done recursively and thus will fail for nested unshareable values.:local- The value will be kept local to the Ractor that pushed it. Another ractor trying to receive it will get an error. Useful for usage contained within a single Ractor.:proxy- The value will be wrapped in aFarce::Proxythat executes calls in the original Ractor.:raise- An error will be raised if the value is not Ractor-shareable. Useful for enforcing shareability.:dedup- The value will be deduplicated using Farce.dedup, then made Ractor-shareable. This may update and freeze the original. Already-shareable values pass through unchanged.:shareable_copy- The value will be copied and the copy will be made Ractor-shareable.
22 23 24 25 |
# File 'lib/farce/timer_queue.rb', line 22 def push(value, non_block = false, mode: nil, timeout: nil, **) # rubocop:disable Style/OptionalBooleanParameter at = Clock.parse() push_to_storage(at, non_block, @manager.wrap(value, mode:), timeout:) end |
#try_pop { ... } ⇒ BasicObject?
Try to remove the earliest value only if its timestamp has been reached.
50 51 52 53 54 55 |
# File 'lib/farce/timer_queue.rb', line 50 def try_pop empty = false value = @queue.pop_before(Clock.now) { empty = true } return @manager.unwrap(value) unless empty yield if block_given? end |
#try_push(value, mode: nil, **time_options) { ... } ⇒ Boolean, BasicObject
Try to add a value without waiting for capacity. Valid modes are:
:copy- The value will be copied between Ractors. This is the default mode.:make_shareable- The value will be made Ractor-shareable using Ractor.make_shareable.:move- The value will be moved between Ractors. This saves memory compared to copying, and supports values that can't be copied but moved (like IO objects). However, the value will no longer be accessible on the Ractor that pushed it.:mutable- A Mutable instance will be created for the value. This isn't done recursively and thus will fail for nested unshareable values.:local- The value will be kept local to the Ractor that pushed it. Another ractor trying to receive it will get an error. Useful for usage contained within a single Ractor.:proxy- The value will be wrapped in aFarce::Proxythat executes calls in the original Ractor.:raise- An error will be raised if the value is not Ractor-shareable. Useful for enforcing shareability.:dedup- The value will be deduplicated using Farce.dedup, then made Ractor-shareable. This may update and freeze the original. Already-shareable values pass through unchanged.:shareable_copy- The value will be copied and the copy will be made Ractor-shareable.
30 31 32 33 34 |
# File 'lib/farce/timer_queue.rb', line 30 def try_push(value, mode: nil, **) at = Clock.parse() return true if @queue.push(at, @manager.wrap(value, mode:)) block_given? ? yield : false end |