Sha256: 14a2f2f1e11152e407931105418e65d9ef8abe9d6d49010acb3eff1d1ab8a2be

Contents?: true

Size: 869 Bytes

Versions: 39

Compression:

Stored size: 869 Bytes

Contents

# -*- coding: binary -*-
require 'thread'

module Rex

###
#
# This module provides a uniform reference counted interface for classes to
# use.
#
###
module Ref

  #
  # Initializes the reference count to one.
  #
  def refinit
    @_references       = 1
    @_references_mutex = Mutex.new

    self
  end

  #
  # Increments the total number of references.
  #
  def ref
    @_references_mutex.synchronize {
      @_references += 1
    }

    self
  end

  #
  # Decrements the total number of references.  If the reference count
  # reaches zero, true is returned.  Otherwise, false is returned.
  #
  def deref
    @_references_mutex.synchronize {
      if ((@_references -= 1) == 0)
        cleanup

        true
      else
        false
      end
    }
  end

  #
  # Called to clean up resources once the ref count drops to zero.
  #
  def cleanup
  end

end
end

Version data entries

39 entries across 39 versions & 4 rubygems

Version Path
rex-core-0.1.25 lib/rex/sync/ref.rb
rex-core-0.1.24 lib/rex/sync/ref.rb
rex-core-0.1.23 lib/rex/sync/ref.rb
rex-core-0.1.22 lib/rex/sync/ref.rb
rex-core-0.1.21 lib/rex/sync/ref.rb
rex-core-0.1.20 lib/rex/sync/ref.rb
rex-core-0.1.19 lib/rex/sync/ref.rb
rex-core-0.1.18 lib/rex/sync/ref.rb
rex-core-0.1.17 lib/rex/sync/ref.rb
rex-core-0.1.16 lib/rex/sync/ref.rb
rex-core-0.1.15 lib/rex/sync/ref.rb
rex-core-0.1.14 lib/rex/sync/ref.rb
rex-2.0.13 lib/rex/sync/ref.rb
rex-core-0.1.13 lib/rex/sync/ref.rb
rex-2.0.12 lib/rex/sync/ref.rb
rex-core-0.1.12 lib/rex/sync/ref.rb
rex-core-0.1.11 lib/rex/sync/ref.rb
rex-2.0.11 lib/rex/sync/ref.rb
rex-core-0.1.10 lib/rex/sync/ref.rb
rex-core-0.1.9 lib/rex/sync/ref.rb