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

Instance Method Summary collapse

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) “`

Parameters:

  • instance (object)

    the instance that has just extended the Ref module



35
36
37
# File 'lib/rex/sync/ref.rb', line 35

def self.extended(instance)
  instance.refinit
end

Instance Method Details

#cleanupObject

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

#cloneObject

Raises a TypeError to prevent cloning.

Raises:

  • (TypeError)


16
17
18
# File 'lib/rex/sync/ref.rb', line 16

def clone
  raise TypeError, "can't clone instance of Ref #{self.class}"
end

#derefObject

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

#dupObject

Raises a TypeError to prevent duping.

Raises:

  • (TypeError)


23
24
25
# File 'lib/rex/sync/ref.rb', line 23

def dup
  raise TypeError, "can't dup instance of Ref #{self.class}"
end

#refObject

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

#refinitObject

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