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.



207
208
209
210
211
212
213
# File 'lib/riser/resource.rb', line 207

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



215
216
217
# File 'lib/riser/resource.rb', line 215

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

#proxy_countObject



229
230
231
# File 'lib/riser/resource.rb', line 229

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

#ref_count(access_key) ⇒ Object



219
220
221
222
223
224
225
226
227
# File 'lib/riser/resource.rb', line 219

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/riser/resource.rb', line 243

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)


233
234
235
236
237
238
239
240
241
# File 'lib/riser/resource.rb', line 233

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



278
279
280
281
282
283
284
285
# File 'lib/riser/resource.rb', line 278

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



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/riser/resource.rb', line 260

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



287
288
289
290
291
# File 'lib/riser/resource.rb', line 287

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