Class: CrystalRuby::ArcMutex

Inherits:
Object
  • Object
show all
Defined in:
lib/crystalruby/arc_mutex.rb

Instance Method Summary collapse

Instance Method Details

#init_mutex!Object



29
30
31
32
33
34
35
# File 'lib/crystalruby/arc_mutex.rb', line 29

def init_mutex!
  mutex = LibC::PThreadMutexT.new
  res = LibC.pthread_mutex_init(mutex, nil)
  raise "Failed to initialize mutex" unless res.zero?

  mutex
end

#lockObject



37
38
39
40
# File 'lib/crystalruby/arc_mutex.rb', line 37

def lock
  res = LibC.pthread_mutex_lock(phtread_mutex)
  raise "Failed to lock mutex" unless res.zero?
end

#phtread_mutexObject



15
16
17
# File 'lib/crystalruby/arc_mutex.rb', line 15

def phtread_mutex
  @phtread_mutex ||= init_mutex!
end

#synchronizeObject



19
20
21
22
23
# File 'lib/crystalruby/arc_mutex.rb', line 19

def synchronize
  lock
  yield
  unlock
end

#to_ptrObject



25
26
27
# File 'lib/crystalruby/arc_mutex.rb', line 25

def to_ptr
  phtread_mutex.pointer
end

#unlockObject



42
43
44
45
# File 'lib/crystalruby/arc_mutex.rb', line 42

def unlock
  res = LibC.pthread_mutex_unlock(phtread_mutex)
  raise "Failed to unlock mutex" unless res.zero?
end