Class: VinaryTree::Libdictenstein::ConcurrentHandle
- Inherits:
-
Object
- Object
- VinaryTree::Libdictenstein::ConcurrentHandle
- Defined in:
- lib/vinary_tree/libdictenstein.rb
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(pointer) ⇒ ConcurrentHandle
constructor
A new instance of ConcurrentHandle.
- #with_pointer ⇒ Object
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
#close ⇒ Object
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_pointer ⇒ Object
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 |