Clasp
Named reentrant locks with deadlock detection for Ruby
Installation & usage
Install using gem install clasp
or add it to your Gemfile
.
Basic usage
manager = Clasp::LockManager.new
manager.lock 'id1'
manager.unlock 'id1'
Deadlock detection
## Thread 1 ## Thread 2
manager.lock 'AAA' manager.lock 'BBB'
begin begin
manager.lock 'BBB' manager.lock 'AAA'
# do work... # do work...
manager.unlock 'BBB' manager.unlock 'AAA'
ensure ensure
manager.unlock 'AAA' manager.unlock 'BBB'
end end
Using standard locks, this program could run forever because of a deadlock. Using LockManager
prevents this by tracking all locks and checking if a deadlock is about to occur.
The first thread that detects the deadlock can release any locks it holds. After this happens, it can rollback and try again later. The competing thread will then be able to lock any resources it needs to continue.
Todo
- More tests