Class: Task::Queue
- Inherits:
-
Object
- Object
- Task::Queue
- Defined in:
- mrbgems/mruby-task/mrblib/queue.rb
Instance Method Summary collapse
-
#pop(non_block = false, timeout_ms: nil) ⇒ Object
(also: #deq, #shift)
Blocks until an item is available (default), or raises if non_block is true.
-
#push(obj) ⇒ Object
(also: #enq, #<<)
WAIT_RETRY is defined in C (task_queue.c gem init).
Instance Method Details
#pop(non_block = false, timeout_ms: nil) ⇒ Object Also known as: deq, shift
Blocks until an item is available (default), or raises if non_block is true. Returns nil if the queue is closed and empty.
The loop is not a busy-wait. When pop_try finds the queue empty it moves the current task to WAITING and sets switching=TRUE before returning WAIT_RETRY. The VM detects switching at the next opcode boundary and exits mrb_vm_exec, handing control back to the scheduler. This task does not run again until a push (or close) moves it back to READY. The loop body therefore executes at most once per wakeup event. When timeout_ms is given, pop returns nil once the deadline passes. Note that a closed-and-empty queue also returns nil, so a nil result does not distinguish "timed out" from "closed" -- this mirrors CRuby's Thread::Queue#pop(timeout:).
__deadline converts the timeout into a fixed absolute tick once, and __pop_try compares it against the current tick with wrap-safe arithmetic in C. Keeping the deadline in C tick space (rather than recomputing a remaining time in Ruby) avoids the 32-bit wrap-around of Task.tick.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'mrbgems/mruby-task/mrblib/queue.rb', line 30 def pop(non_block = false, timeout_ms: nil) unless timeout_ms.nil? raise ArgumentError, "timeout cannot be combined with non_block" if non_block raise TypeError, "timeout_ms must be an Integer" unless Integer === timeout_ms raise ArgumentError, "timeout_ms must be non-negative" if timeout_ms < 0 end deadline = timeout_ms.nil? ? nil : __deadline(timeout_ms) while true v = __pop_try(non_block, deadline) return nil if v.equal?(WAIT_TIMEOUT) return v unless v.equal?(WAIT_RETRY) end end |
#push(obj) ⇒ Object Also known as: enq, <<
WAIT_RETRY is defined in C (task_queue.c gem init)
5 6 7 8 |
# File 'mrbgems/mruby-task/mrblib/queue.rb', line 5 def push(obj) __push(obj) self end |