[ruby/json] Use RB_TYPE_P
[ruby.git] / thread_sync.rb
blobf8fa69900b39d0452b6c0adc02377fbfc927aa66
1 class Thread
2   class Queue
3     # call-seq:
4     #   pop(non_block=false, timeout: nil)
5     #
6     # Retrieves data from the queue.
7     #
8     # If the queue is empty, the calling thread is suspended until data is pushed
9     # onto the queue. If +non_block+ is true, the thread isn't suspended, and
10     # +ThreadError+ is raised.
11     #
12     # If +timeout+ seconds have passed and no data is available +nil+ is
13     # returned. If +timeout+ is +0+ it returns immediately.
14     def pop(non_block = false, timeout: nil)
15       if non_block && timeout
16         raise ArgumentError, "can't set a timeout if non_block is enabled"
17       end
18       Primitive.rb_queue_pop(non_block, timeout)
19     end
20     alias_method :deq, :pop
21     alias_method :shift, :pop
22   end
24   class SizedQueue
25     # call-seq:
26     #   pop(non_block=false, timeout: nil)
27     #
28     # Retrieves data from the queue.
29     #
30     # If the queue is empty, the calling thread is suspended until data is
31     # pushed onto the queue. If +non_block+ is true, the thread isn't
32     # suspended, and +ThreadError+ is raised.
33     #
34     # If +timeout+ seconds have passed and no data is available +nil+ is
35     # returned. If +timeout+ is +0+ it returns immediately.
36     def pop(non_block = false, timeout: nil)
37       if non_block && timeout
38         raise ArgumentError, "can't set a timeout if non_block is enabled"
39       end
40       Primitive.rb_szqueue_pop(non_block, timeout)
41     end
42     alias_method :deq, :pop
43     alias_method :shift, :pop
45     # call-seq:
46     #   push(object, non_block=false, timeout: nil)
47     #   enq(object, non_block=false, timeout: nil)
48     #   <<(object)
49     #
50     # Pushes +object+ to the queue.
51     #
52     # If there is no space left in the queue, waits until space becomes
53     # available, unless +non_block+ is true.  If +non_block+ is true, the
54     # thread isn't suspended, and +ThreadError+ is raised.
55     #
56     # If +timeout+ seconds have passed and no space is available +nil+ is
57     # returned. If +timeout+ is +0+ it returns immediately.
58     # Otherwise it returns +self+.
59     def push(object, non_block = false, timeout: nil)
60       if non_block && timeout
61         raise ArgumentError, "can't set a timeout if non_block is enabled"
62       end
63       Primitive.rb_szqueue_push(object, non_block, timeout)
64     end
65     alias_method :enq, :push
66     alias_method :<<, :push
67   end
68 end