Class: Concurrent::MVar
Constant Summary
collapse
- EMPTY =
Object.new
- TIMEOUT =
Object.new
Instance Method Summary
collapse
#init_mutex, #mutex, #set_deref_options, #value
Constructor Details
#initialize(value = EMPTY, opts = {}) ⇒ MVar
Returns a new instance of MVar.
13
14
15
16
17
18
19
|
# File 'lib/concurrent/mvar.rb', line 13
def initialize(value = EMPTY, opts = {})
@value = value
@mutex = Mutex.new
@empty_condition = Condition.new
@full_condition = Condition.new
set_deref_options(opts)
end
|
Instance Method Details
#empty? ⇒ Boolean
119
120
121
|
# File 'lib/concurrent/mvar.rb', line 119
def empty?
@mutex.synchronize { @value == EMPTY }
end
|
#full? ⇒ Boolean
123
124
125
|
# File 'lib/concurrent/mvar.rb', line 123
def full?
not empty?
end
|
#modify(timeout = nil) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/concurrent/mvar.rb', line 52
def modify(timeout = nil)
raise ArgumentError.new('no block given') unless block_given?
@mutex.synchronize do
wait_for_full(timeout)
if unlocked_full?
value = @value
@value = yield value
@full_condition.signal
apply_deref_options(value)
else
TIMEOUT
end
end
end
|
#modify! ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/concurrent/mvar.rb', line 104
def modify!
raise ArgumentError.new('no block given') unless block_given?
@mutex.synchronize do
value = @value
@value = yield value
if unlocked_empty?
@empty_condition.signal
else
@full_condition.signal
end
apply_deref_options(value)
end
end
|
#put(value, timeout = nil) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/concurrent/mvar.rb', line 37
def put(value, timeout = nil)
@mutex.synchronize do
wait_for_empty(timeout)
if unlocked_empty?
@value = value
@full_condition.signal
apply_deref_options(value)
else
TIMEOUT
end
end
end
|
#set!(value) ⇒ Object
95
96
97
98
99
100
101
102
|
# File 'lib/concurrent/mvar.rb', line 95
def set!(value)
@mutex.synchronize do
old_value = @value
@value = value
@full_condition.signal
apply_deref_options(old_value)
end
end
|
#take(timeout = nil) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/concurrent/mvar.rb', line 21
def take(timeout = nil)
@mutex.synchronize do
wait_for_full(timeout)
if unlocked_full?
value = @value
@value = EMPTY
@empty_condition.signal
apply_deref_options(value)
else
TIMEOUT
end
end
end
|
#try_put!(value) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/concurrent/mvar.rb', line 83
def try_put!(value)
@mutex.synchronize do
if unlocked_empty?
@value = value
@full_condition.signal
true
else
false
end
end
end
|
#try_take! ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/concurrent/mvar.rb', line 70
def try_take!
@mutex.synchronize do
if unlocked_full?
value = @value
@value = EMPTY
@empty_condition.signal
apply_deref_options(value)
else
EMPTY
end
end
end
|