Class: N::SafeHash

Inherits:
Hash show all
Defined in:
lib/glue/hash.rb

Overview

SafeHash

A thread-safe hash. 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 uses the delegator pattern to allow for multiple implementations!

Direct Known Subclasses

SiteMap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegator = nil) ⇒ SafeHash

gmosx: delegator is not used.



25
26
27
# File 'lib/glue/hash.rb', line 25

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

Instance Attribute Details

#syncObject (readonly)

Returns the value of attribute sync.



21
22
23
# File 'lib/glue/hash.rb', line 21

def sync
  @sync
end

Instance Method Details

#[](key) ⇒ Object



29
30
31
32
33
# File 'lib/glue/hash.rb', line 29

def [](key)
	return @sync.synchronize(::Sync::SH) {
		super
	}
end

#[]=(key, value) ⇒ Object



35
36
37
38
39
# File 'lib/glue/hash.rb', line 35

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

#clearObject



47
48
49
50
51
# File 'lib/glue/hash.rb', line 47

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

#delete(key) ⇒ Object



41
42
43
44
45
# File 'lib/glue/hash.rb', line 41

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

#keysObject



65
66
67
68
69
# File 'lib/glue/hash.rb', line 65

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

#sizeObject



53
54
55
56
57
# File 'lib/glue/hash.rb', line 53

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

#valuesObject



59
60
61
62
63
# File 'lib/glue/hash.rb', line 59

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