Module: Rex::Ref
- Defined in:
- lib/rex/sync/ref.rb
Overview
This module provides a uniform reference counted interface for classes to use.
Class Method Summary collapse
-
.extended(instance) ⇒ Object
Ensures that the Ref is correctly initialized when extended on an object: “‘ arbitrary_resource = Resource.new arbitrary_resource.extend(::Rex::Ref) “`.
Instance Method Summary collapse
-
#cleanup ⇒ Object
Called to clean up resources once the ref count drops to zero.
-
#clone ⇒ Object
Raises a TypeError to prevent cloning.
-
#deref ⇒ Object
Decrements the total number of references.
-
#dup ⇒ Object
Raises a TypeError to prevent duping.
-
#ref ⇒ Object
Increments the total number of references.
-
#refinit ⇒ Object
Initializes the reference count to one.
Class Method Details
.extended(instance) ⇒ Object
Ensures that the Ref is correctly initialized when extended on an object: “‘ arbitrary_resource = Resource.new arbitrary_resource.extend(::Rex::Ref) “`
35 36 37 |
# File 'lib/rex/sync/ref.rb', line 35 def self.extended(instance) instance.refinit end |
Instance Method Details
#cleanup ⇒ Object
Called to clean up resources once the ref count drops to zero.
81 82 |
# File 'lib/rex/sync/ref.rb', line 81 def cleanup end |
#clone ⇒ Object
Raises a TypeError to prevent cloning.
16 17 18 |
# File 'lib/rex/sync/ref.rb', line 16 def clone raise TypeError, "can't clone instance of Ref #{self.class}" end |
#deref ⇒ Object
Decrements the total number of references. If the reference count reaches zero, true is returned. Otherwise, false is returned.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rex/sync/ref.rb', line 66 def deref @_references_mutex.synchronize { if ((@_references -= 1) == 0) cleanup true else false end } end |
#dup ⇒ Object
Raises a TypeError to prevent duping.
23 24 25 |
# File 'lib/rex/sync/ref.rb', line 23 def dup raise TypeError, "can't dup instance of Ref #{self.class}" end |
#ref ⇒ Object
Increments the total number of references.
54 55 56 57 58 59 60 |
# File 'lib/rex/sync/ref.rb', line 54 def ref @_references_mutex.synchronize { @_references += 1 } self end |
#refinit ⇒ Object
Initializes the reference count to one.
42 43 44 45 46 47 48 49 |
# File 'lib/rex/sync/ref.rb', line 42 def refinit return if defined?(@_references) @_references = 1 @_references_mutex = Mutex.new self end |