Class: VinaryTree::Libdictenstein::ConcurrentHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/vinary_tree/libdictenstein.rb

Instance Method Summary collapse

Constructor Details

#initialize(pointer) ⇒ ConcurrentHandle

Returns a new instance of ConcurrentHandle.



31
32
33
34
35
36
37
# File 'lib/vinary_tree/libdictenstein.rb', line 31

def initialize(pointer)
  @pointer = pointer
  @active = 0
  @closing = false
  @mutex = Mutex.new
  @condition = ConditionVariable.new
end

Instance Method Details

#closeObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vinary_tree/libdictenstein.rb', line 53

def close
  pointer = @mutex.synchronize do
    return 0 if @pointer.zero?
    @closing = true
    @condition.wait(@mutex) until @active.zero?
    result = @pointer
    @pointer = 0
    result
  end
  Native.ldict_dictionary_free(pointer) unless pointer.zero?
  pointer
end

#with_pointerObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vinary_tree/libdictenstein.rb', line 39

def with_pointer
  pointer = @mutex.synchronize do
    raise IOError, "dictionary is closed" if @closing || @pointer.zero?
    @active += 1
    @pointer
  end
  yield pointer
ensure
  @mutex.synchronize do
    @active -= 1
    @condition.broadcast if @active.zero?
  end if pointer
end