Acquiring multiple locks

Andreas Raab andreas.raab at gmx.de
Mon Jun 26 22:12:00 UTC 2006


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




More information about the Squeak-dev mailing list