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.



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

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

Instance Method Details

#acquire(pool) ⇒ Object

Parameters:



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

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

#locked?(pool) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


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

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

#mutex(pool) ⇒ Object (protected)



59
60
61
62
63
64
65
66
67
# File 'lib/osctld/devices/lock.rb', line 59

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:



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

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

#sync(pool) ⇒ Object

Parameters:



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

def sync(pool)
  m = mutex(pool)

  if m.owned?
    yield
  else
    m.synchronize do
      Devices::ChangeSet.open(pool)
      ret = yield
      Devices::ChangeSet.close(pool)
      ret
    end
  end
end