concurrent-ruby 0.9.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -1
- data/README.md +23 -19
- data/lib/concurrent/atomic/atomic_fixnum.rb +29 -8
- data/lib/concurrent/atomic/read_write_lock.rb +11 -11
- data/lib/concurrent/atomic/reentrant_read_write_lock.rb +375 -0
- data/lib/concurrent/atomic/thread_local_var.rb +181 -51
- data/lib/concurrent/collection/copy_on_notify_observer_set.rb +1 -1
- data/lib/concurrent/configuration.rb +8 -4
- data/lib/concurrent/delay.rb +0 -1
- data/lib/concurrent/errors.rb +4 -0
- data/lib/concurrent/executor/executor.rb +2 -1
- data/lib/concurrent/executor/ruby_thread_pool_executor.rb +9 -10
- data/lib/concurrent/scheduled_task.rb +0 -2
- data/lib/concurrent/synchronization/monitor_object.rb +1 -0
- data/lib/concurrent/synchronization/object.rb +5 -0
- data/lib/concurrent/synchronization/rbx_object.rb +21 -8
- data/lib/concurrent/utility/at_exit.rb +1 -1
- data/lib/concurrent/utility/native_extension_loader.rb +1 -4
- data/lib/concurrent/version.rb +2 -2
- metadata +3 -3
- data/lib/concurrent/atomic/thread_local_var/weak_key_map.rb +0 -236
@@ -25,9 +25,6 @@ module Concurrent
|
|
25
25
|
# may be a Windows cross-compiled native gem
|
26
26
|
require "concurrent/#{RUBY_VERSION[0..2]}/extension"
|
27
27
|
@c_ext_loaded = true
|
28
|
-
end,
|
29
|
-
lambda do
|
30
|
-
warn 'Performance on MRI may be improved with the concurrent-ruby-ext gem. Please see http://concurrent-ruby.com'
|
31
28
|
end]
|
32
29
|
|
33
30
|
tries.each do |try|
|
@@ -45,7 +42,7 @@ module Concurrent
|
|
45
42
|
require 'concurrent_ruby_ext'
|
46
43
|
@java_ext_loaded = true
|
47
44
|
rescue LoadError
|
48
|
-
|
45
|
+
# move on with pure-Ruby implementations
|
49
46
|
end
|
50
47
|
end
|
51
48
|
end
|
data/lib/concurrent/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: concurrent-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jerry D'Antonio
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-08-09 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: |
|
15
15
|
Modern concurrency tools including agents, futures, promises, thread pools, actors, supervisors, and more.
|
@@ -38,9 +38,9 @@ files:
|
|
38
38
|
- lib/concurrent/atomic/cyclic_barrier.rb
|
39
39
|
- lib/concurrent/atomic/event.rb
|
40
40
|
- lib/concurrent/atomic/read_write_lock.rb
|
41
|
+
- lib/concurrent/atomic/reentrant_read_write_lock.rb
|
41
42
|
- lib/concurrent/atomic/semaphore.rb
|
42
43
|
- lib/concurrent/atomic/thread_local_var.rb
|
43
|
-
- lib/concurrent/atomic/thread_local_var/weak_key_map.rb
|
44
44
|
- lib/concurrent/atomic_reference/concurrent_update_error.rb
|
45
45
|
- lib/concurrent/atomic_reference/direct_update.rb
|
46
46
|
- lib/concurrent/atomic_reference/jruby.rb
|
@@ -1,236 +0,0 @@
|
|
1
|
-
# Copyright (c) 2013 Brian Durand
|
2
|
-
#
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
# a copy of this software and associated documentation files (the
|
5
|
-
# "Software"), to deal in the Software without restriction, including
|
6
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
# the following conditions:
|
10
|
-
#
|
11
|
-
# The above copyright notice and this permission notice shall be
|
12
|
-
# included in all copies or substantial portions of the Software.
|
13
|
-
#
|
14
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
-
|
22
|
-
module Concurrent
|
23
|
-
|
24
|
-
# @!visibility private
|
25
|
-
class AbstractThreadLocalVar
|
26
|
-
|
27
|
-
begin
|
28
|
-
require 'weakref'
|
29
|
-
|
30
|
-
# @!visibility private
|
31
|
-
class WeakReference
|
32
|
-
|
33
|
-
# The object id of the object being referenced.
|
34
|
-
attr_reader :referenced_object_id
|
35
|
-
|
36
|
-
# This implementation of a weak reference simply wraps the standard WeakRef implementation
|
37
|
-
# that comes with the Ruby standard library.
|
38
|
-
def initialize(obj)
|
39
|
-
@referenced_object_id = obj.__id__
|
40
|
-
@ref = ::WeakRef.new(obj)
|
41
|
-
end
|
42
|
-
|
43
|
-
def object
|
44
|
-
@ref.__getobj__
|
45
|
-
rescue => e
|
46
|
-
# Jruby implementation uses RefError while MRI uses WeakRef::RefError
|
47
|
-
if (defined?(RefError) && e.is_a?(RefError)) || (defined?(::WeakRef::RefError) && e.is_a?(::WeakRef::RefError))
|
48
|
-
nil
|
49
|
-
else
|
50
|
-
raise e
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
rescue LoadError
|
56
|
-
|
57
|
-
require 'monitor'
|
58
|
-
|
59
|
-
# This is a pure ruby implementation of a weak reference. It is much more
|
60
|
-
# efficient than the WeakRef implementation bundled in MRI 1.8 and 1.9
|
61
|
-
# subclass Delegator which is very heavy to instantiate and utilizes a
|
62
|
-
# because it does not fair amount of memory under Ruby 1.8.
|
63
|
-
#
|
64
|
-
# @!visibility private
|
65
|
-
class WeakReference
|
66
|
-
|
67
|
-
# The object id of the object being referenced.
|
68
|
-
attr_reader :referenced_object_id
|
69
|
-
|
70
|
-
# @!visibility private
|
71
|
-
class ReferencePointer
|
72
|
-
def initialize(object)
|
73
|
-
@referenced_object_id = object.__id__
|
74
|
-
add_backreference(object)
|
75
|
-
end
|
76
|
-
|
77
|
-
def cleanup
|
78
|
-
obj = ObjectSpace._id2ref(@referenced_object_id) rescue nil
|
79
|
-
remove_backreference(obj) if obj
|
80
|
-
end
|
81
|
-
|
82
|
-
def object
|
83
|
-
obj = ObjectSpace._id2ref(@referenced_object_id)
|
84
|
-
obj if verify_backreferences(obj)
|
85
|
-
rescue RangeError
|
86
|
-
nil
|
87
|
-
end
|
88
|
-
|
89
|
-
private
|
90
|
-
|
91
|
-
# Verify that the object is the same one originally set for the weak reference.
|
92
|
-
def verify_backreferences(obj)
|
93
|
-
return nil unless supports_backreference?(obj)
|
94
|
-
backreferences = obj.instance_variable_get(:@__weak_backreferences__) if obj.instance_variable_defined?(:@__weak_backreferences__)
|
95
|
-
backreferences && backreferences.include?(object_id)
|
96
|
-
end
|
97
|
-
|
98
|
-
# Add a backreference to the object.
|
99
|
-
def add_backreference(obj)
|
100
|
-
return unless supports_backreference?(obj)
|
101
|
-
backreferences = obj.instance_variable_get(:@__weak_backreferences__) if obj.instance_variable_defined?(:@__weak_backreferences__)
|
102
|
-
unless backreferences
|
103
|
-
backreferences = []
|
104
|
-
obj.instance_variable_set(:@__weak_backreferences__, backreferences)
|
105
|
-
end
|
106
|
-
backreferences << object_id
|
107
|
-
end
|
108
|
-
|
109
|
-
# Remove backreferences from the object.
|
110
|
-
def remove_backreference(obj)
|
111
|
-
return unless supports_backreference?(obj)
|
112
|
-
backreferences = obj.instance_variable_get(:@__weak_backreferences__) if obj.instance_variable_defined?(:@__weak_backreferences__)
|
113
|
-
if backreferences
|
114
|
-
backreferences.dup.delete(object_id)
|
115
|
-
obj.send(:remove_instance_variable, :@__weak_backreferences__) if backreferences.empty?
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def supports_backreference?(obj)
|
120
|
-
obj.respond_to?(:instance_variable_get) && obj.respond_to?(:instance_variable_defined?)
|
121
|
-
rescue NoMethodError
|
122
|
-
false
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
private_constant :ReferencePointer
|
127
|
-
|
128
|
-
@@weak_references = {}
|
129
|
-
@@lock = Monitor.new
|
130
|
-
|
131
|
-
# Finalizer that cleans up weak references when references are destroyed.
|
132
|
-
@@reference_finalizer = lambda do |object_id|
|
133
|
-
@@lock.synchronize do
|
134
|
-
reference_pointer = @@weak_references.delete(object_id)
|
135
|
-
reference_pointer.cleanup if reference_pointer
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
# Create a new weak reference to an object. The existence of the weak reference
|
140
|
-
# will not prevent the garbage collector from reclaiming the referenced object.
|
141
|
-
def initialize(obj)
|
142
|
-
@referenced_object_id = obj.__id__
|
143
|
-
@@lock.synchronize do
|
144
|
-
@reference_pointer = ReferencePointer.new(obj)
|
145
|
-
@@weak_references[self.object_id] = @reference_pointer
|
146
|
-
end
|
147
|
-
ObjectSpace.define_finalizer(self, @@reference_finalizer)
|
148
|
-
end
|
149
|
-
|
150
|
-
# Get the reference object. If the object has already been garbage collected,
|
151
|
-
# then this method will return nil.
|
152
|
-
def object
|
153
|
-
if @reference_pointer
|
154
|
-
obj = @reference_pointer.object
|
155
|
-
unless obj
|
156
|
-
@@lock.synchronize do
|
157
|
-
@@weak_references.delete(object_id)
|
158
|
-
@reference_pointer.cleanup
|
159
|
-
@reference_pointer = nil
|
160
|
-
end
|
161
|
-
end
|
162
|
-
obj
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
private_constant :WeakReference
|
168
|
-
end
|
169
|
-
|
170
|
-
# The classes behave similar to Hashes, but the keys in the map are not strong references
|
171
|
-
# and can be reclaimed by the garbage collector at any time. When a key is reclaimed, the
|
172
|
-
# map entry will be removed.
|
173
|
-
#
|
174
|
-
# @!visibility private
|
175
|
-
class WeakKeyMap
|
176
|
-
|
177
|
-
# Create a new map. Values added to the hash will be cleaned up by the garbage
|
178
|
-
# collector if there are no other reference except in the map.
|
179
|
-
def initialize
|
180
|
-
@values = {}
|
181
|
-
@references_to_keys_map = {}
|
182
|
-
@lock = Monitor.new
|
183
|
-
@reference_cleanup = lambda{|object_id| remove_reference_to(object_id)}
|
184
|
-
end
|
185
|
-
|
186
|
-
# Get a value from the map by key. If the value has been reclaimed by the garbage
|
187
|
-
# collector, this will return nil.
|
188
|
-
def [](key)
|
189
|
-
@lock.synchronize do
|
190
|
-
rkey = ref_key(key)
|
191
|
-
@values[rkey] if rkey
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
# Add a key/value to the map.
|
196
|
-
def []=(key, value)
|
197
|
-
ObjectSpace.define_finalizer(key, @reference_cleanup)
|
198
|
-
@lock.synchronize do
|
199
|
-
@references_to_keys_map[key.__id__] = WeakReference.new(key)
|
200
|
-
@values[key.__id__] = value
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
# Remove the value associated with the key from the map.
|
205
|
-
def delete(key)
|
206
|
-
@lock.synchronize do
|
207
|
-
rkey = ref_key(key)
|
208
|
-
if rkey
|
209
|
-
@references_to_keys_map.delete(rkey)
|
210
|
-
@values.delete(rkey)
|
211
|
-
else
|
212
|
-
nil
|
213
|
-
end
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
# Get an array of keys that have not yet been garbage collected.
|
218
|
-
def keys
|
219
|
-
@values.keys.collect{|rkey| @references_to_keys_map[rkey].object}.compact
|
220
|
-
end
|
221
|
-
|
222
|
-
private
|
223
|
-
|
224
|
-
def ref_key (key)
|
225
|
-
ref = @references_to_keys_map[key.__id__]
|
226
|
-
if ref && ref.object
|
227
|
-
ref.referenced_object_id
|
228
|
-
else
|
229
|
-
nil
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
private_constant :WeakKeyMap
|
235
|
-
end
|
236
|
-
end
|