Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit 0fdbc1b

Browse files
committed
Merge pull request #21 from ruby-concurrency/replaces-safe-monitor-with-monitor
Replaces SafeMonitor with Ruby's Monitor
2 parents c641b0d + 96a6d94 commit 0fdbc1b

File tree

7 files changed

+35
-86
lines changed

7 files changed

+35
-86
lines changed

‎lib/ref.rb‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module Ref
33
require'ref/abstract_reference_key_map'
44
require'ref/reference'
55
require'ref/reference_queue'
6-
require'ref/safe_monitor'
76

87
# Set the best implementation for weak references based on the runtime.
98
ifdefined?(RUBY_PLATFORM) && RUBY_PLATFORM == 'java'

‎lib/ref/abstract_reference_key_map.rb‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ class << self
99
defreference_class=(klass)#:nodoc:
1010
@reference_class=klass
1111
end
12-
12+
1313
defreference_class#:nodoc:
1414
raiseNotImplementedError.new("#{name} is an abstract class and cannot be instantiated")unless@reference_class
1515
@reference_class
1616
end
1717
end
18-
18+
1919
# Create a new map. Values added to the hash will be cleaned up by the garbage
2020
# collector if there are no other reference except in the map.
2121
definitialize
2222
@values={}
2323
@references_to_keys_map={}
24-
@lock=SafeMonitor.new
24+
@lock=Monitor.new
2525
@reference_cleanup=lambda{|object_id| remove_reference_to(object_id)}
2626
end
2727

@@ -60,14 +60,14 @@ def delete(key)
6060
defkeys
6161
@values.keys.collect{|rkey| @references_to_keys_map[rkey].object}.compact
6262
end
63-
63+
6464
# Turn the map into an arry of [key, value] entries.
6565
defto_a
6666
array=[]
6767
each{|k,v| array << [k,v]}
6868
array
6969
end
70-
70+
7171
# Iterate through all the key/value pairs in the map that have not been reclaimed
7272
# by the garbage collector.
7373
defeach
@@ -128,7 +128,7 @@ def ref_key (key)
128128
nil
129129
end
130130
end
131-
131+
132132
defremove_reference_to(object_id)
133133
@lock.synchronizedo
134134
@references_to_keys_map.delete(object_id)

‎lib/ref/abstract_reference_value_map.rb‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ class << self
99
defreference_class=(klass)#:nodoc:
1010
@reference_class=klass
1111
end
12-
12+
1313
defreference_class#:nodoc:
1414
raiseNotImplementedError.new("#{name} is an abstract class and cannot be instantiated")unless@reference_class
1515
@reference_class
1616
end
1717
end
18-
18+
1919
# Create a new map. Values added to the map will be cleaned up by the garbage
2020
# collector if there are no other reference except in the map.
2121
definitialize
2222
@references={}
2323
@references_to_keys_map={}
24-
@lock=SafeMonitor.new
24+
@lock=Monitor.new
2525
@reference_cleanup=lambda{|object_id| remove_reference_to(object_id)}
2626
end
2727

@@ -74,14 +74,14 @@ def values
7474
each{|k,v| vals << v}
7575
vals
7676
end
77-
77+
7878
# Turn the map into an arry of [key, value] entries
7979
defto_a
8080
array=[]
8181
each{|k,v| array << [k,v]}
8282
array
8383
end
84-
84+
8585
# Iterate through all the key/value pairs in the map that have not been reclaimed
8686
# by the garbage collector.
8787
defeach

‎lib/ref/reference_queue.rb‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Ref
1818
# # Do something...
1919
# end
2020
# end
21-
#
21+
#
2222
# queue = Ref::ReferenceQueue.new
2323
# ref = MyRef.new(Object.new)
2424
# queue.monitor(ref)
@@ -30,15 +30,15 @@ class ReferenceQueue
3030
definitialize
3131
@queue=[]
3232
@references={}
33-
@lock=SafeMonitor.new
33+
@lock=Monitor.new
3434
@finalizer=lambdado |object_id|
3535
@lock.synchronizedo
3636
ref=@references.delete(object_id)
3737
@queue.push(ref)ifref
3838
end
3939
end
4040
end
41-
41+
4242
# Monitor a reference. When the object the reference points to is garbage collected,
4343
# the reference will be added to the queue.
4444
defmonitor(reference)
@@ -52,7 +52,7 @@ def monitor(reference)
5252
push(reference)
5353
end
5454
end
55-
55+
5656
# Add a reference to the queue.
5757
defpush(reference)
5858
ifreference
@@ -61,26 +61,26 @@ def push(reference)
6161
end
6262
end
6363
end
64-
64+
6565
# Pull the last reference off the queue. Returns +nil+ if their are no references.
6666
defpop
6767
@lock.synchronizedo
6868
@queue.pop
6969
end
7070
end
71-
71+
7272
# Pull the next reference off the queue. Returns +nil+ if there are no references.
7373
defshift
7474
@lock.synchronizedo
7575
@queue.shift
7676
end
7777
end
78-
78+
7979
# Return +true+ if the queue is empty.
8080
defempty?
8181
@queue.empty?
8282
end
83-
83+
8484
# Get the current size of the queue.
8585
defsize
8686
@queue.size

‎lib/ref/safe_monitor.rb‎

Lines changed: 0 additions & 50 deletions
This file was deleted.

‎lib/ref/soft_reference.rb‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ module Ref
1919
classSoftReference < Reference
2020
@@strong_references=[{}]
2121
@@gc_flag_set=false
22-
22+
2323
# Number of garbage collection cycles after an object is used before a reference to it can be reclaimed.
2424
MIN_GC_CYCLES=10
25-
26-
@@lock=SafeMonitor.new
27-
25+
26+
@@lock=Monitor.new
27+
2828
@@finalizer=lambdado |object_id|
2929
@@lock.synchronizedo
3030
while@@strong_references.size >= MIN_GC_CYCLESdo
@@ -34,14 +34,14 @@ class SoftReference < Reference
3434
@@gc_flag_set=false
3535
end
3636
end
37-
37+
3838
# Create a new soft reference to an object.
3939
definitialize(obj)
4040
@referenced_object_id=obj.__id__
4141
@weak_reference=WeakReference.new(obj)
4242
add_strong_reference(obj)
4343
end
44-
44+
4545
# Get the referenced object. If the object has been reclaimed by the
4646
# garbage collector, then this will return nil.
4747
defobject
@@ -50,7 +50,7 @@ def object
5050
add_strong_reference(obj)ifobj
5151
obj
5252
end
53-
53+
5454
private
5555
# Create a strong reference to the object. This reference will live
5656
# for three passes of the garbage collector.

‎lib/ref/weak_reference/pure_ruby.rb‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ module Ref
44
# subclass Delegator which is very heavy to instantiate and utilizes a
55
# because it does not fair amount of memory under Ruby 1.8.
66
classWeakReference < Reference
7-
7+
88
classReferencePointer
99
definitialize(object)
1010
@referenced_object_id=object.__id__
1111
add_backreference(object)
1212
end
13-
13+
1414
defcleanup
1515
obj=ObjectSpace._id2ref(@referenced_object_id)rescuenil
1616
remove_backreference(obj)ifobj
1717
end
18-
18+
1919
defobject
2020
obj=ObjectSpace._id2ref(@referenced_object_id)
2121
objifverify_backreferences(obj)
2222
rescueRangeError
2323
nil
2424
end
25-
25+
2626
private
2727
# Verify that the object is the same one originally set for the weak reference.
2828
defverify_backreferences(obj)#:nodoc:
2929
returnnilunlesssupports_backreference?(obj)
3030
backreferences=obj.instance_variable_get(:@__weak_backreferences__)ifobj.instance_variable_defined?(:@__weak_backreferences__)
3131
backreferences && backreferences.include?(object_id)
3232
end
33-
33+
3434
# Add a backreference to the object.
3535
defadd_backreference(obj)#:nodoc:
3636
returnunlesssupports_backreference?(obj)
@@ -41,7 +41,7 @@ def add_backreference(obj) #:nodoc:
4141
end
4242
backreferences << object_id
4343
end
44-
44+
4545
# Remove backreferences from the object.
4646
defremove_backreference(obj)#:nodoc:
4747
returnunlesssupports_backreference?(obj)
@@ -51,16 +51,16 @@ def remove_backreference(obj) #:nodoc:
5151
obj.send(:remove_instance_variable,:@__weak_backreferences__)ifbackreferences.empty?
5252
end
5353
end
54-
54+
5555
defsupports_backreference?(obj)
5656
obj.respond_to?(:instance_variable_get) && obj.respond_to?(:instance_variable_defined?)
5757
rescueNoMethodError
5858
false
5959
end
6060
end
61-
61+
6262
@@weak_references={}
63-
@@lock=SafeMonitor.new
63+
@@lock=Monitor.new
6464

6565
# Finalizer that cleans up weak references when references are destroyed.
6666
@@reference_finalizer=lambdado |object_id|

0 commit comments

Comments
(0)