Module: RactorQueue::Interface

Included in:
RactorQueue
Defined in:
lib/ractor_queue/interface.rb

Instance Method Summary collapse

Instance Method Details

#empty?Boolean

True if queue appears empty (approximate).

Returns:

  • (Boolean)


41
# File 'lib/ractor_queue/interface.rb', line 41

def empty?  = was_empty

#full?Boolean

True if queue appears full (approximate).

Returns:

  • (Boolean)


44
# File 'lib/ractor_queue/interface.rb', line 44

def full?   = was_full

#pop(timeout: nil) ⇒ Object

Blocking pop. Spins until an element is available. Returns the object. Raises RactorQueue::TimeoutError if timeout expires.



33
34
35
# File 'lib/ractor_queue/interface.rb', line 33

def pop(timeout: nil)
  blocking_pop(timeout)
end

#push(obj, timeout: nil) ⇒ Object

Blocking push. Spins until space is available. Returns self. Raises RactorQueue::TimeoutError if timeout expires. Ruby interrupt-aware via Thread.pass between retries.



26
27
28
29
# File 'lib/ractor_queue/interface.rb', line 26

def push(obj, timeout: nil)
  validate_shareable!(obj) if @validate_shareable
  blocking_push(obj, timeout)
end

#sizeObject

Approximate current element count.



38
# File 'lib/ractor_queue/interface.rb', line 38

def size    = was_size

#try_popObject

Non-blocking pop. Returns the object, or RactorQueue::EMPTY if the queue is empty. EMPTY is a unique frozen sentinel distinct from nil, so nil is an unambiguous payload value.

entry = q.try_pop
if entry.equal?(RactorQueue::EMPTY)
# queue was empty
else
process(entry)   # entry may be nil if nil was pushed
end


19
20
21
# File 'lib/ractor_queue/interface.rb', line 19

def try_pop
  c_try_pop
end

#try_push(obj) ⇒ Object

Non-blocking push. Returns true if enqueued, false if full.



4
5
6
7
# File 'lib/ractor_queue/interface.rb', line 4

def try_push(obj)
  validate_shareable!(obj) if @validate_shareable
  c_try_push(obj)
end