Sha256: d709709fe6a5544bcb6411e2a1451b021bfa63647a9c6027a2a61d04eb7a0bd1

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

# = General arrays utilities collection
#
# === Design:
#
# Implement as a module to avoid class polution. You can still Ruby's
# advanced features to include the module in your class.
# Passing the object to act upon allows to check for nil, which isn't
# possible if you use self.
#
# code:: gmosx, ekarak
#
# (c) 2002-2003 Navel, all rights reserved.
# $Id: array.rb 71 2004-10-18 10:50:22Z gmosx $

require "sync"

module N

# == SafeArray
#
# A thread-safe array. We use a sync object instead of a mutex,
# because it is re-entrant.
# An exclusive lock is needed when writing, a shared lock IS NEEDED
# when reading

class SafeArray < Array
	
	attr :sync

	# gmosx: delegator is not used.

	def initialize(delegator = nil)
		@sync = ::Sync.new()
	end

	def << (value)
		return @sync.synchronize(::Sync::SH) {
			super
		}		
	end
	
	def delete_if(&block) 
		return @sync.synchronize(::Sync::SH) {
			super
		}				
	end
	
	def [](key)
		return @sync.synchronize(::Sync::SH) {
			super
		}
	end

	def []=(key, value)
		return @sync.synchronize(::Sync::EX) {
			super
		}
	end

	def delete(key)
		return @sync.synchronize(::Sync::EX) {
			super
		}
	end

	def clear
		@sync.synchronize(::Sync::EX) {
			super
		}
	end

	def size
		return @sync.synchronize(::Sync::SH) {
			super
		}
	end

	def shift
		return @sync.synchronize(::Sync::EX) {
			super
		}
	end

	def unshift(el)
		return @sync.synchronize(::Sync::EX) {
			super
		}
	end
		
end # SafeArray

end # module

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.1.2 lib/n/utils/array.rb