Skip to content

Commit b88af96

Browse files
committed
Replaced Channel::Buffer::NO_VALUE with Concurrent::NULL
1 parent 0df8cdb commit b88af96

File tree

11 files changed

+48
-47
lines changed

11 files changed

+48
-47
lines changed

lib/concurrent/channel.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,20 @@ def offer?(item)
117117

118118
def take
119119
item = do_take
120-
item == Buffer::NO_VALUE ? nil : item
120+
item == Concurrent::NULL ? nil : item
121121
end
122122
alias_method :receive, :take
123123
alias_method :~, :take
124124

125125
def take!
126126
item = do_take
127-
raise Error if item == Buffer::NO_VALUE
127+
raise Error if item == Concurrent::NULL
128128
item
129129
end
130130

131131
def take?
132132
item = do_take
133-
item = if item == Buffer::NO_VALUE
133+
item = if item == Concurrent::NULL
134134
Concurrent::Maybe.nothing
135135
else
136136
Concurrent::Maybe.just(item)
@@ -155,13 +155,13 @@ def take?
155155
# end
156156
def next
157157
item, more = do_next
158-
item = nil if item == Buffer::NO_VALUE
158+
item = nil if item == Concurrent::NULL
159159
return item, more
160160
end
161161

162162
def next?
163163
item, more = do_next
164-
item = if item == Buffer::NO_VALUE
164+
item = if item == Concurrent::NULL
165165
Concurrent::Maybe.nothing
166166
else
167167
Concurrent::Maybe.just(item)
@@ -170,17 +170,17 @@ def next?
170170
end
171171

172172
def poll
173-
(item = do_poll) == Buffer::NO_VALUE ? nil : item
173+
(item = do_poll) == Concurrent::NULL ? nil : item
174174
end
175175

176176
def poll!
177177
item = do_poll
178-
raise Error if item == Buffer::NO_VALUE
178+
raise Error if item == Concurrent::NULL
179179
item
180180
end
181181

182182
def poll?
183-
if (item = do_poll) == Buffer::NO_VALUE
183+
if (item = do_poll) == Concurrent::NULL
184184
Concurrent::Maybe.nothing
185185
else
186186
Concurrent::Maybe.just(item)
@@ -191,7 +191,7 @@ def each
191191
raise ArgumentError.new('no block given') unless block_given?
192192
loop do
193193
item, more = do_next
194-
if item != Buffer::NO_VALUE
194+
if item != Concurrent::NULL
195195
yield(item)
196196
elsif !more
197197
break

lib/concurrent/channel/buffer/base.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ module Concurrent
44
class Channel
55
module Buffer
66

7-
# Placeholder for when a buffer slot contains no value.
8-
NO_VALUE = Object.new
9-
107
# Abstract base class for all Channel buffers.
118
#
129
# {Concurrent::Channel} objects maintain an internal, queue-like
@@ -118,7 +115,7 @@ def offer(item)
118115
# are available the remaining items can still be taken. Once the
119116
# buffer closes, no remaining items can be taken.
120117
#
121-
# @return [Object] the item removed from the buffer; `NO_VALUE` once
118+
# @return [Object] the item removed from the buffer; `Concurrent::NULL` once
122119
# the buffer has closed.
123120
#
124121
# @raise [NotImplementedError] until overridden in a subclass.
@@ -137,7 +134,7 @@ def take
137134
# values, "more" (a boolean), will always be `true` when the buffer is
138135
# open. The "more" value will be `false` when the channel has been
139136
# closed and all values have already been received. When "more" is
140-
# false the returned item will be `NO_VALUE`.
137+
# false the returned item will be `Concurrent::NULL`.
141138
#
142139
# Note that when multiple threads access the same channel a race
143140
# condition can occur when using this method. A call to `next` from
@@ -161,7 +158,7 @@ def next
161158
# immediately. Failing to return a value does not necessarily
162159
# indicate that the buffer is closed, just that it is empty.
163160
#
164-
# @return [Object] the next item from the buffer or `NO_VALUE` if
161+
# @return [Object] the next item from the buffer or `Concurrent::NULL` if
165162
# the buffer is empty.
166163
#
167164
# @raise [NotImplementedError] until overridden in a subclass.

lib/concurrent/channel/buffer/buffered.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'concurrent/constants'
12
require 'concurrent/channel/buffer/base'
23

34
module Concurrent
@@ -56,7 +57,7 @@ def next
5657
loop do
5758
synchronize do
5859
if ns_closed? && ns_empty?
59-
return NO_VALUE, false
60+
return Concurrent::NULL, false
6061
elsif !ns_empty?
6162
item = buffer.shift
6263
return item, true
@@ -70,7 +71,7 @@ def next
7071
def poll
7172
synchronize do
7273
if ns_empty?
73-
NO_VALUE
74+
Concurrent::NULL
7475
else
7576
buffer.shift
7677
end

lib/concurrent/channel/buffer/ticker.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'concurrent/constants'
12
require 'concurrent/utility/monotonic_time'
23
require 'concurrent/channel/tick'
34
require 'concurrent/channel/buffer/base'
@@ -20,7 +21,7 @@ def take
2021
# a Go timer will block forever if stopped
2122
loop do
2223
tick = do_poll
23-
return tick if tick != NO_VALUE
24+
return tick if tick != Concurrent::NULL
2425
Thread.pass
2526
end
2627
end
@@ -30,7 +31,7 @@ def next
3031
# it will always return `true` for more
3132
loop do
3233
tick = do_poll
33-
return tick, true if tick != NO_VALUE
34+
return tick, true if tick != Concurrent::NULL
3435
Thread.pass
3536
end
3637
end
@@ -66,7 +67,7 @@ def do_poll
6667
@next_tick = now + @interval
6768
return tick
6869
else
69-
return NO_VALUE
70+
return Concurrent::NULL
7071
end
7172
end
7273
end

lib/concurrent/channel/buffer/timer.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'concurrent/constants'
12
require 'concurrent/utility/monotonic_time'
23
require 'concurrent/channel/tick'
34
require 'concurrent/channel/buffer/base'
@@ -20,7 +21,7 @@ def take
2021
# a Go timer will block forever if stopped
2122
loop do
2223
tick = do_poll
23-
return tick if tick != NO_VALUE
24+
return tick if tick != Concurrent::NULL
2425
Thread.pass
2526
end
2627
end
@@ -30,7 +31,7 @@ def next
3031
# it will always return `true` for more
3132
loop do
3233
tick = do_poll
33-
return tick, true if tick != NO_VALUE
34+
return tick, true if tick != Concurrent::NULL
3435
Thread.pass
3536
end
3637
end
@@ -65,7 +66,7 @@ def do_poll
6566
self.closed = true
6667
return Concurrent::Channel::Tick.new(@tick)
6768
else
68-
return NO_VALUE
69+
return Concurrent::NULL
6970
end
7071
end
7172
end

lib/concurrent/channel/buffer/unbuffered.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'concurrent/constants'
12
require 'concurrent/channel/buffer/base'
23
require 'concurrent/atomic/atomic_reference'
34

@@ -87,7 +88,7 @@ def offer(item)
8788
# and this method will return.
8889
def take
8990
mine = synchronize do
90-
return NO_VALUE if ns_closed? && putting.empty?
91+
return Concurrent::NULL if ns_closed? && putting.empty?
9192

9293
ref = Concurrent::AtomicReference.new(nil)
9394
if putting.empty?
@@ -112,10 +113,10 @@ def take
112113
# waiting to {#put} items onto the buffer. When there is a thread
113114
# waiting to put an item this method will take the item and return
114115
# it immediately. When there are no threads waiting to put or the
115-
# buffer is closed, this method will return `NO_VALUE` immediately.
116+
# buffer is closed, this method will return `Concurrent::NULL` immediately.
116117
def poll
117118
synchronize do
118-
return NO_VALUE if putting.empty?
119+
return Concurrent::NULL if putting.empty?
119120

120121
put = putting.shift
121122
value = put.value
@@ -133,7 +134,7 @@ def poll
133134
# @see {#take}
134135
def next
135136
item = take
136-
more = (item != NO_VALUE)
137+
more = (item != Concurrent::NULL)
137138
return item, more
138139
end
139140

spec/concurrent/channel/buffer/base_shared.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@
6565
end
6666

6767
context '#take' do
68-
it 'returns NO_VALUE when closed' do
68+
it 'returns Concurrent::NULL when closed' do
6969
subject.close
70-
expect(subject.take).to eq Concurrent::Channel::Buffer::NO_VALUE
70+
expect(subject.take).to eq Concurrent::NULL
7171
end
7272
end
7373

7474
context '#next' do
75-
it 'returns NO_VALUE, false when closed' do
75+
it 'returns Concurrent::NULL, false when closed' do
7676
subject.close
7777
item, more = subject.next
78-
expect(item).to eq Concurrent::Channel::Buffer::NO_VALUE
78+
expect(item).to eq Concurrent::NULL
7979
expect(more).to be false
8080
end
8181
end
@@ -92,13 +92,13 @@
9292
expect(subject.poll).to eq 42
9393
end
9494

95-
it 'returns NO_VALUE immediately if no item is available' do
96-
expect(subject.poll).to eq Concurrent::Channel::Buffer::NO_VALUE
95+
it 'returns Concurrent::NULL immediately if no item is available' do
96+
expect(subject.poll).to eq Concurrent::NULL
9797
end
9898

99-
it 'returns NO_VALUE when closed' do
99+
it 'returns Concurrent::NULL when closed' do
100100
subject.close
101-
expect(subject.poll).to eq Concurrent::Channel::Buffer::NO_VALUE
101+
expect(subject.poll).to eq Concurrent::NULL
102102
end
103103
end
104104

spec/concurrent/channel/buffer/buffered_shared.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@
9090
expect(t.status).to be false
9191
end
9292

93-
it 'returns NO_VALUE when closed and empty' do
93+
it 'returns Concurrent::NULL when closed and empty' do
9494
subject.close
95-
expect(subject.take).to eq Concurrent::Channel::Buffer::NO_VALUE
95+
expect(subject.take).to eq Concurrent::NULL
9696
end
9797
end
9898

@@ -158,7 +158,7 @@
158158
end
159159
end
160160

161-
it 'returns NO_VALUE, false when closed and no items remain' do
161+
it 'returns Concurrent::NULL, false when closed and no items remain' do
162162
capacity = subject.capacity
163163
expect(capacity).to be >= 1
164164

@@ -168,7 +168,7 @@
168168
capacity.times { subject.next }
169169

170170
item, more = subject.next
171-
expect(item).to eq Concurrent::Channel::Buffer::NO_VALUE
171+
expect(item).to eq Concurrent::NULL
172172
expect(more).to be false
173173
end
174174
end

spec/concurrent/channel/buffer/timer_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Concurrent::Channel::Buffer
1919
context '#poll' do
2020
it 'closes automatically on first take' do
2121
loop do
22-
break if subject.poll != NO_VALUE
22+
break if subject.poll != Concurrent::NULL
2323
end
2424
expect(subject).to be_closed
2525
end
@@ -29,7 +29,7 @@ module Concurrent::Channel::Buffer
2929
it 'closes automatically on first take' do
3030
loop do
3131
value, _ = subject.next
32-
break if value != NO_VALUE
32+
break if value != Concurrent::NULL
3333
end
3434
expect(subject).to be_closed
3535
end

spec/concurrent/channel/buffer/timing_buffer_shared.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@
8383

8484
context '#poll' do
8585

86-
it 'returns NO_VALUE when the timer is not ready' do
86+
it 'returns Concurrent::NULL when the timer is not ready' do
8787
subject = described_class.new(0.1)
88-
expect(subject.poll).to eq Concurrent::Channel::Buffer::NO_VALUE
88+
expect(subject.poll).to eq Concurrent::NULL
8989
end
9090

9191
it 'returns a Tick' do
@@ -94,9 +94,9 @@
9494
expect(subject.poll).to be_a Concurrent::Channel::Tick
9595
end
9696

97-
it 'returns NO_VALUE when closed' do
97+
it 'returns Concurrent::NULL when closed' do
9898
subject.close
99-
expect(subject.poll).to eq Concurrent::Channel::Buffer::NO_VALUE
99+
expect(subject.poll).to eq Concurrent::NULL
100100
end
101101

102102
it 'triggers after the specified time interval' do

0 commit comments

Comments
 (0)