Class: Riser::Resource::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/riser/resource.rb

Instance Method Summary collapse

Constructor Details

#initialize(create, destroy) ⇒ Manager

Returns a new instance of Manager.



13
14
15
16
17
18
19
20
# File 'lib/riser/resource.rb', line 13

def initialize(create, destroy)
  @mutex = Thread::Mutex.new
  @create = create
  @destroy = destroy
  @ref_count = 0
  @ref_object = nil
  @ref_proxy = {}         # to keep proxy objects living in dRuby process
end

Instance Method Details

#proxy_countObject



26
27
28
# File 'lib/riser/resource.rb', line 26

def proxy_count
  @mutex.synchronize{ @ref_proxy.size }
end

#ref_countObject



22
23
24
# File 'lib/riser/resource.rb', line 22

def ref_count
  @mutex.synchronize{ @ref_count }
end

#ref_objectObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/riser/resource.rb', line 34

def ref_object
  @mutex.synchronize{
    if (@ref_count < 0) then
      raise "internal error: negative reference count <#{@ref_count}>"
    end

    if (@ref_count == 0) then
      # if an exception occurs at `@create.call', the object should not be referenced.
      @ref_object = @create.call
    end
    @ref_count += 1
    @ref_object
  }
end

#ref_object?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/riser/resource.rb', line 30

def ref_object?
  @mutex.synchronize{ ! @ref_object.nil? }
end

#ref_proxy(proxy) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/riser/resource.rb', line 67

def ref_proxy(proxy)
  @mutex.synchronize{
    if (@ref_proxy.key? proxy.__id__) then
      raise "internal error: duplicated proxy object <#{proxy.__id__}>"
    end
    @ref_proxy[proxy.__id__] = proxy
  }
end

#unref_objectObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/riser/resource.rb', line 49

def unref_object
  @mutex.synchronize{
    unless (@ref_count > 0) then
      raise "internal error: unreferenced resource object <#{@ref_count}>"
    end

    @ref_count -= 1
    if (@ref_count == 0) then
      tmp_object = @ref_object
      @ref_object = nil
      # even if an exception occurs at `@destroy.call', the object should be unreferenced.
      @destroy.call(tmp_object)
    end
  }

  nil
end

#unref_proxy(proxy) ⇒ Object



76
77
78
79
80
# File 'lib/riser/resource.rb', line 76

def unref_proxy(proxy)
  @mutex.synchronize{
    @ref_proxy.delete(proxy.__id__) or raise "internal error: unreferenced proxy object <#{proxy.__id__}>"
  }
end