Class: RefCounting

Inherits:
Object show all
Defined in:
lib/roby/distributed/base.rb

Overview

A thread-safe reference-counting class

Instance Method Summary collapse

Constructor Details

#initializeRefCounting

Returns a new instance of RefCounting.



5
6
7
8
# File 'lib/roby/distributed/base.rb', line 5

def initialize
  @values = Hash.new(0)
  @mutex  = Mutex.new
end

Instance Method Details

#delete(object) ⇒ Object

Remove object from the set of referenced objects, regardless of its reference count



36
37
38
39
40
# File 'lib/roby/distributed/base.rb', line 36

def delete(object)
  @mutex.synchronize do
 @values.delete(object)
  end
end

#deref(obj) ⇒ Object

Dereference obj by one



13
14
15
16
17
18
19
20
21
# File 'lib/roby/distributed/base.rb', line 13

def deref(obj)
  @mutex.synchronize do
 if (@values[obj] -= 1) == 0
    @values.delete(obj)
    return true
 end
  end
  false
end

#ref(obj) ⇒ Object

Add +1 to the reference count of obj



23
24
25
26
27
# File 'lib/roby/distributed/base.rb', line 23

def ref(obj)
  @mutex.synchronize do
 @values[obj] += 1
  end
end

#ref?(obj) ⇒ Boolean

True if obj is referenced

Returns:

  • (Boolean)


11
# File 'lib/roby/distributed/base.rb', line 11

def ref?(obj); @mutex.synchronize { @values[obj] > 0 } end

#referenced_objectsObject

Returns the set of referenced objects



29
30
31
32
33
# File 'lib/roby/distributed/base.rb', line 29

def referenced_objects
  @mutex.synchronize do
 @values.keys
  end
end