summaryrefslogtreecommitdiff
path: root/thread_sync.rb
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2022-07-26 17:40:00 +0200
committerJean Boussier <jean.boussier@gmail.com>2022-08-18 10:07:37 +0200
commitfe61cad7490da8a879597f851f4a89856d44838e (patch)
tree29844ee84fe8b5c547ab8c58b8464ab450be0873 /thread_sync.rb
parentb3718edee28d5155ebc383d17ab58867e20f4aa2 (diff)
downloadruby-fe61cad7490da8a879597f851f4a89856d44838e.tar.gz
Implement SizedQueue#push(timeout: sec)
[Feature #18944] If both `non_block=true` and `timeout:` are supplied, ArgumentError is raised.
Diffstat (limited to 'thread_sync.rb')
-rw-r--r--thread_sync.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/thread_sync.rb b/thread_sync.rb
index d567ca51af..7e4c341ad0 100644
--- a/thread_sync.rb
+++ b/thread_sync.rb
@@ -41,5 +41,28 @@ class Thread
end
alias_method :deq, :pop
alias_method :shift, :pop
+
+ # call-seq:
+ # push(object, non_block=false, timeout: nil)
+ # enq(object, non_block=false, timeout: nil)
+ # <<(object)
+ #
+ # Pushes +object+ to the queue.
+ #
+ # If there is no space left in the queue, waits until space becomes
+ # available, unless +non_block+ is true. If +non_block+ is true, the
+ # thread isn't suspended, and +ThreadError+ is raised.
+ #
+ # If +timeout+ seconds have passed and no space is available +nil+ is
+ # returned.
+ # Otherwise it returns +self+.
+ def push(object, 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_push(object, non_block, timeout)
+ end
+ alias_method :enq, :push
+ alias_method :<<, :push
end
end