If you always aquire the locks in the same order you will not deadlock, skipping lock acquisition is ok.
So, instead of a set of locks you should have an ordered collection of locks. Acquisition of multiple locks must proceed from low index to high index.
Terry
=========================================================== Terry Raymond Smalltalk Professional Debug Package Crafted Smalltalk 80 Lazywood Ln. Tiverton, RI 02878 (401) 624-4517 traymond@craftedsmalltalk.com http://www.craftedsmalltalk.com ===========================================================
-----Original Message----- From: squeak-dev-bounces@lists.squeakfoundation.org [mailto:squeak-dev- bounces@lists.squeakfoundation.org] On Behalf Of Andreas Raab Sent: Monday, June 26, 2006 6:12 PM To: The general-purpose Squeak developers list Subject: Acquiring multiple locks
Hi Folks -
I have an interesting little problem that can be trivially solved with help from the VM but that I'm curious if there is a way of doing it from Squeak directly. Here is the problem:
Given a set of N locks (semaphores), how can multiple processes acquire subsets of those locks without deadlocking? E.g., it's fairly obvious that if one process attempts to acquire lock A and B and another one lock B and A that this leads to deadlock in a hurry. What's in particular problematic for my use case is that I don't know which semaphores are part of said set of locks and that I can't establish a total order to make sure that lock A always gets acquired before lock B.
What I've been contemplating is to add a primitive which does the following: It attempts to acquire all the locks given as argument and if one of them cannot be acquired it immediately releases all the locks acquired so far and leaves the process suspended on the "failing" lock. The return value will indicate whether all the locks were acquired and therefore one can write a loop like here to acquire all of the locks:
[self primitiveAcquireAll: locks] whileFalse.
In other words, if all the locks can be acquired the code just marches through. If it fails to acquire a lock the process will stay suspended until that particular lock is released and then retry to acquire the locks.
Is it possible to do that without a VM change?
Cheers,
- Andreas