Class: Riser::ResourceSet::Manager

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

Defined Under Namespace

Classes: Reference

Instance Method Summary collapse

Constructor Details

#initialize(create, destroy) ⇒ Manager

Returns a new instance of Manager.



211
212
213
214
215
216
217
# File 'lib/riser/resource.rb', line 211

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

Instance Method Details

#key_countObject



219
220
221
# File 'lib/riser/resource.rb', line 219

def key_count
  @mutex.synchronize{ @ref_table.size }
end

#proxy_countObject



233
234
235
# File 'lib/riser/resource.rb', line 233

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

#ref_count(access_key) ⇒ Object



223
224
225
226
227
228
229
230
231
# File 'lib/riser/resource.rb', line 223

def ref_count(access_key)
  @mutex.synchronize{
    if (ref = @ref_table[access_key]) then
      ref.count
    else
      0
    end
  }
end

#ref_object(access_key) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/riser/resource.rb', line 247

def ref_object(access_key)
  @mutex.synchronize{
    if (ref = @ref_table[access_key]) then
      unless (ref.count > 0) then
        raise "internal error: unreferenced resource object <#{ref.count}>"
      end
      ref.count += 1
      ref.object
    else
      # if an exception occurs at `@create.call', the object should not be referenced.
      tmp_object = @create.call(access_key)
      @ref_table[access_key] = Reference.new(1, tmp_object)
      tmp_object
    end
  }
end

#ref_object?(access_key) ⇒ Boolean

Returns:

  • (Boolean)


237
238
239
240
241
242
243
244
245
# File 'lib/riser/resource.rb', line 237

def ref_object?(access_key)
  @mutex.synchronize{
    if (ref = @ref_table[access_key]) then
      ! ref.object.nil?
    else
      false
    end
  }
end

#ref_proxy(proxy) ⇒ Object



282
283
284
285
286
287
288
289
# File 'lib/riser/resource.rb', line 282

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_object(access_key) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/riser/resource.rb', line 264

def unref_object(access_key)
  @mutex.synchronize{
    ref = @ref_table[access_key] or raise "internal error: not defined resource object <#{access_key}>"
    unless (ref.count > 0) then
      raise "internal error: unreferenced resource object <#{ref.count}>"
    end

    ref.count -= 1
    if (ref.count == 0) then
      @ref_table.delete(access_key)
      # even if an exception occurs at `@destroy.call', the object should be unreferenced.
      @destroy.call(ref.object)
    end
  }

  nil
end

#unref_proxy(proxy) ⇒ Object



291
292
293
294
295
# File 'lib/riser/resource.rb', line 291

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