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

Commit 96a6d94

Browse files
committed
Replaces SafeMonitor with Ruby's Monitor
1 parent c641b0d commit 96a6d94

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
if defined?(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
def reference_class=(klass) #:nodoc:
1010
@reference_class = klass
1111
end
12-
12+
1313
def reference_class #:nodoc:
1414
raise NotImplementedError.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
def initialize
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
def keys
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
def to_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
def each
@@ -128,7 +128,7 @@ def ref_key (key)
128128
nil
129129
end
130130
end
131-
131+
132132
def remove_reference_to(object_id)
133133
@lock.synchronize do
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
def reference_class=(klass) #:nodoc:
1010
@reference_class = klass
1111
end
12-
12+
1313
def reference_class #:nodoc:
1414
raise NotImplementedError.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
def initialize
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
def to_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
def each

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
def initialize
3131
@queue = []
3232
@references = {}
33-
@lock = SafeMonitor.new
33+
@lock = Monitor.new
3434
@finalizer = lambda do |object_id|
3535
@lock.synchronize do
3636
ref = @references.delete(object_id)
3737
@queue.push(ref) if ref
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
def monitor(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
def push(reference)
5858
if reference
@@ -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
def pop
6767
@lock.synchronize do
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
def shift
7474
@lock.synchronize do
7575
@queue.shift
7676
end
7777
end
78-
78+
7979
# Return +true+ if the queue is empty.
8080
def empty?
8181
@queue.empty?
8282
end
83-
83+
8484
# Get the current size of the queue.
8585
def size
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
class SoftReference < 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 = lambda do |object_id|
2929
@@lock.synchronize do
3030
while @@strong_references.size >= MIN_GC_CYCLES do
@@ -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
def initialize(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
def object
@@ -50,7 +50,7 @@ def object
5050
add_strong_reference(obj) if obj
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
class WeakReference < Reference
7-
7+
88
class ReferencePointer
99
def initialize(object)
1010
@referenced_object_id = object.__id__
1111
add_backreference(object)
1212
end
13-
13+
1414
def cleanup
1515
obj = ObjectSpace._id2ref(@referenced_object_id) rescue nil
1616
remove_backreference(obj) if obj
1717
end
18-
18+
1919
def object
2020
obj = ObjectSpace._id2ref(@referenced_object_id)
2121
obj if verify_backreferences(obj)
2222
rescue RangeError
2323
nil
2424
end
25-
25+
2626
private
2727
# Verify that the object is the same one originally set for the weak reference.
2828
def verify_backreferences(obj) #:nodoc:
2929
return nil unless supports_backreference?(obj)
3030
backreferences = obj.instance_variable_get(:@__weak_backreferences__) if obj.instance_variable_defined?(:@__weak_backreferences__)
3131
backreferences && backreferences.include?(object_id)
3232
end
33-
33+
3434
# Add a backreference to the object.
3535
def add_backreference(obj) #:nodoc:
3636
return unless supports_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
def remove_backreference(obj) #:nodoc:
4747
return unless supports_backreference?(obj)
@@ -51,16 +51,16 @@ def remove_backreference(obj) #:nodoc:
5151
obj.send(:remove_instance_variable, :@__weak_backreferences__) if backreferences.empty?
5252
end
5353
end
54-
54+
5555
def supports_backreference?(obj)
5656
obj.respond_to?(:instance_variable_get) && obj.respond_to?(:instance_variable_defined?)
5757
rescue NoMethodError
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 = lambda do |object_id|

0 commit comments

Comments
 (0)