Class: OsCtld::Devices::Lock

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/osctld/devices/lock.rb

Overview

Protect manipulation of device access trees

When managing group/container devices, it is often necessary to read or modify both parent and child groups. For the lack of a better mechanism, all operations on device access trees should be done only while holding this per-pool lock.

Instance Method Summary collapse

Constructor Details

#initializeLock

Returns a new instance of Lock



22
23
24
25
# File 'lib/osctld/devices/lock.rb', line 22

def initialize
  @main = Mutex.new
  @pools = {}
end

Instance Method Details

#acquire(pool) ⇒ Object

Parameters:



28
29
30
# File 'lib/osctld/devices/lock.rb', line 28

def acquire(pool)
  mutex(pool).lock
end

#locked?(pool) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


38
39
40
# File 'lib/osctld/devices/lock.rb', line 38

def locked?(pool)
  mutex(pool).owned?
end

#mutex(pool) ⇒ Object (protected)



54
55
56
57
58
59
60
61
62
# File 'lib/osctld/devices/lock.rb', line 54

def mutex(pool)
  @main.synchronize do
    if @pools.has_key?(pool.name)
      @pools[pool.name]
    else
      @pools[pool.name] = Mutex.new
    end
  end
end

#release(pool) ⇒ Object

Parameters:



33
34
35
# File 'lib/osctld/devices/lock.rb', line 33

def release(pool)
  mutex(pool).release
end

#sync(pool) ⇒ Object

Parameters:



43
44
45
46
47
48
49
50
51
# File 'lib/osctld/devices/lock.rb', line 43

def sync(pool)
  m = mutex(pool)

  if m.owned?
    yield
  else
    m.synchronize { yield }
  end
end