1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/thread_sync.rb
Jean Boussier e3aabe93aa Implement Queue#pop(timeout: sec)
[Feature #18774]

As well as `SizedQueue#pop(timeout: sec)`

If both `non_block=true` and `timeout:` are supplied, ArgumentError
is raised.
2022-08-02 11:04:28 +02:00

45 lines
1.4 KiB
Ruby

class Thread
class Queue
# call-seq:
# pop(non_block=false, timeout: nil)
#
# Retrieves data from the queue.
#
# If the queue is empty, the calling thread is suspended until data is pushed
# onto the queue. If +non_block+ is true, the thread isn't suspended, and
# +ThreadError+ is raised.
#
# If +timeout+ seconds have passed and no data is available +nil+ is
# returned.
def pop(non_block = false, timeout: nil)
if non_block && timeout
raise ArgumentError, "can't set a timeout if non_block is enabled"
end
Primitive.rb_queue_pop(non_block, timeout)
end
alias_method :deq, :pop
alias_method :shift, :pop
end
class SizedQueue
# call-seq:
# pop(non_block=false, timeout: nil)
#
# Retrieves data from the queue.
#
# If the queue is empty, the calling thread is suspended until data is
# pushed onto the queue. If +non_block+ is true, the thread isn't
# suspended, and +ThreadError+ is raised.
#
# If +timeout+ seconds have passed and no data is available +nil+ is
# returned.
def pop(non_block = false, timeout: nil)
if non_block && timeout
raise ArgumentError, "can't set a timeout if non_block is enabled"
end
Primitive.rb_szqueue_pop(non_block, timeout)
end
alias_method :deq, :pop
alias_method :shift, :pop
end
end