SharedLock: Implement downgrade from exclusive to shared mode
authorMichael Hanselmann <hansmi@google.com>
Fri, 6 May 2011 14:43:38 +0000 (16:43 +0200)
committerMichael Hanselmann <hansmi@google.com>
Fri, 13 May 2011 12:57:46 +0000 (14:57 +0200)
commit3dbe3ddf257082ec3e5b9e3506aabe599ae6a8e3
tree6341ecb8681de9a5e050da1242907714a5b13fff
parent9133387ed791b1906d6d35aa680c637ca92f7158
SharedLock: Implement downgrade from exclusive to shared mode

If a job needs to modify a resource and then wait for a result, it must
acquire the resource lock in exclusive mode. In some cases it would be
possible to only have a shared lock for waiting. Until now it was not
possible to change a lock's mode once it'd been acquired. Releasing and
re-acquiring might have been possible, but would require many more
checks and can introduce new issues.

With this patch a new method, named “downgrade”, is added to Ganeti's
own SharedLock class. It can only be called when the lock is held in
exclusive mode and changes it to shared. If there are any pending shared
acquires on the same priority, they're moved to the front of the queue
and notified (jumping ahead of exclusive acquires).

In a lockset the internal lock will be downgraded if, and only if, all
individual locks owned by the current thread are either released or
acquired in shared mode.

Unittests are provided.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
lib/locking.py
test/ganeti.locking_unittest.py