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.



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

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



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

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

#ref_countObject



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

def ref_count
  @mutex.synchronize{ @ref_count }
end

#ref_objectObject



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

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)


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

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

#ref_proxy(proxy) ⇒ Object



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

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



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

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



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

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