Skip to content

Commit fc5af4f

Browse files
committed
IVar: documentation.
1 parent fd5b6d5 commit fc5af4f

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

lib/concurrent/ivar.rb

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,34 @@
55

66
module Concurrent
77

8+
# An `IVar` is a single-element container that is normally created empty, and
9+
# can only be set once. The I in `IVar` stands for immutable. Reading an `IVar`
10+
# normally blocks until it is set. It is safe to set and read an `IVar` from
11+
# different threads.
12+
#
13+
# If you want to have some parallel task set the value in an `IVar`, you want
14+
# a `Future`. If you want to create a graph of parallel tasks all executed when
15+
# the values they depend on are ready you want `dataflow`. `IVar` is generally
16+
# a low-level primitive.
17+
#
18+
# @example Create, set and get an `IVar`
19+
# ivar = Concurrent::IVar.new
20+
# ivar.set 14
21+
# ivar.get #=> 14
22+
# ivar.set 2 # would now be an error
823
class IVar
24+
25+
# Error that indicates that an `IVar` was set twice. Each `IVar` can only
26+
# be set once - they are immutable.
927
MultipleAssignmentError = Class.new(StandardError)
1028

1129
include Obligation
12-
include Concurrent::Observable
30+
include Observable
1331

14-
NO_VALUE = Object.new
32+
# @!visibility private
33+
NO_VALUE = Object.new # :nodoc:
1534

16-
# Create a new `Ivar` in the `:pending` state with the (optional) initial value.
35+
# Create a new `IVar` in the `:pending` state with the (optional) initial value.
1736
#
1837
# @param [Object] value the initial value
1938
# @param [Hash] opts the options to create a message with
@@ -63,15 +82,22 @@ def add_observer(observer = nil, func = :update, &block)
6382
observer
6483
end
6584

85+
# Set the `IVar` to a value and wake or notify all threads waiting on it.
86+
# @param [Object] the value to store in the `IVar`
87+
# @raise [MultipleAssignmentError] if the `IVar` has already been set or otherwise completed
6688
def set(value)
6789
complete(true, value, nil)
6890
end
6991

92+
# Set the `IVar` to failed due to some error and wake or notify all threads waiting on it.
93+
# @option [Object] reason for the failure
94+
# @raise [MultipleAssignmentError] if the `IVar` has already been set or otherwise completed
7095
def fail(reason = StandardError.new)
7196
complete(false, nil, reason)
7297
end
7398

74-
def complete(success, value, reason)
99+
# @!visibility private
100+
def complete(success, value, reason) # :nodoc:
75101
mutex.synchronize do
76102
raise MultipleAssignmentError.new('multiple assignment') if [:fulfilled, :rejected].include? @state
77103
set_state(success, value, reason)

0 commit comments

Comments
 (0)