Class: Foobara::WeakObjectSet::GarbageCleaner
- Inherits:
-
Object
- Object
- Foobara::WeakObjectSet::GarbageCleaner
- Defined in:
- foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb
Instance Attribute Summary collapse
-
#cleanup_thread ⇒ Object
Returns the value of attribute cleanup_thread.
-
#deactivated ⇒ Object
Returns the value of attribute deactivated.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#weak_object_set ⇒ Object
Returns the value of attribute weak_object_set.
Instance Method Summary collapse
- #cleanup_proc ⇒ Object
- #deactivate ⇒ Object
- #deactivated? ⇒ Boolean
-
#initialize(weak_object_set, queue) ⇒ GarbageCleaner
constructor
A new instance of GarbageCleaner.
- #start_cleanup_thread ⇒ Object
- #track(object) ⇒ Object
Constructor Details
#initialize(weak_object_set, queue) ⇒ GarbageCleaner
Returns a new instance of GarbageCleaner.
10 11 12 13 14 15 |
# File 'foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb', line 10 def initialize(weak_object_set, queue) self.queue = queue self.weak_object_set = weak_object_set start_cleanup_thread end |
Instance Attribute Details
#cleanup_thread ⇒ Object
Returns the value of attribute cleanup_thread.
8 9 10 |
# File 'foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb', line 8 def cleanup_thread @cleanup_thread end |
#deactivated ⇒ Object
Returns the value of attribute deactivated.
8 9 10 |
# File 'foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb', line 8 def deactivated @deactivated end |
#queue ⇒ Object
Returns the value of attribute queue.
8 9 10 |
# File 'foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb', line 8 def queue @queue end |
#weak_object_set ⇒ Object
Returns the value of attribute weak_object_set.
8 9 10 |
# File 'foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb', line 8 def weak_object_set @weak_object_set end |
Instance Method Details
#cleanup_proc ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb', line 17 def cleanup_proc if deactivated? # :nocov: raise "GarbageCleaner has been deactivated" # :nocov: end @cleanup_proc ||= begin queue = self.queue ->(object_id) do unless deactivated? begin queue.push(object_id) rescue ClosedQueueError # :nocov: deactivate # :nocov: end end end end end |
#deactivate ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb', line 65 def deactivate raise if deactivated? self.deactivated = true queue.close # TODO: don't bother to join here outside of test suite cleanup_thread.join # just doing this for test suite/simplecov @cleanup_proc = nil @queue = nil @weak_object_set = nil @cleanup_thread = nil end |
#deactivated? ⇒ Boolean
78 79 80 |
# File 'foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb', line 78 def deactivated? deactivated end |
#start_cleanup_thread ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb', line 41 def start_cleanup_thread queue = self.queue self.cleanup_thread = Thread.new do loop do object_id = queue.pop if object_id weak_object_set.delete(object_id) elsif queue.closed? self.queue = nil break else # :nocov: raise "Unexpected nil value in the queue" # :nocov: end end end end |
#track(object) ⇒ Object
61 62 63 |
# File 'foobara-0.0.141/projects/weak_object_set/src/weak_object_set.rb', line 61 def track(object) ObjectSpace.define_finalizer(object, cleanup_proc) end |