Sha256: f390f98a6b4d50e4b63b46100c86545ae6eccc8c7fbe8ce53d654fb5e1103873

Contents?: true

Size: 782 Bytes

Versions: 43

Compression:

Stored size: 782 Bytes

Contents

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

43 entries across 43 versions & 1 rubygems

Version Path
librex-0.0.65 lib/rex/sync/ref.rb
librex-0.0.63 lib/rex/sync/ref.rb
librex-0.0.54 lib/rex/sync/ref.rb
librex-0.0.53 lib/rex/sync/ref.rb
librex-0.0.52 lib/rex/sync/ref.rb
librex-0.0.51 lib/rex/sync/ref.rb
librex-0.0.50 lib/rex/sync/ref.rb
librex-0.0.49 lib/rex/sync/ref.rb
librex-0.0.48 lib/rex/sync/ref.rb
librex-0.0.47 lib/rex/sync/ref.rb
librex-0.0.46 lib/rex/sync/ref.rb
librex-0.0.44 lib/rex/sync/ref.rb
librex-0.0.43 lib/rex/sync/ref.rb
librex-0.0.42 lib/rex/sync/ref.rb
librex-0.0.41 lib/rex/sync/ref.rb
librex-0.0.40 lib/rex/sync/ref.rb
librex-0.0.39 lib/rex/sync/ref.rb
librex-0.0.38 lib/rex/sync/ref.rb
librex-0.0.37 lib/rex/sync/ref.rb
librex-0.0.36 lib/rex/sync/ref.rb