[concurrency-interest] Roach motels

Steve Kautz skautz at aonix.com
Wed Nov 16 15:49:27 EST 2005

Reading Brian Goetz's article on synchronization optimizations in Mustang
got me thinking again about Bill Pugh's roach motel analogy (statements can
move into a sync block, but they can't move out...).  Does anyone have any
information about conditions under which statements might be moved into a
preceding sync block (or similarly, whether adjacent synchronized blocks may
be coalesced when there is intervening code)?

E.g. suppose we have something like:

boolean foo()
if (inProgress) return false;
inProgress = true;

// make this call without lock
Result result = someOtherObject.bar();

inProgress = false;
return true;

When is the compiler allowed to move the call to bar() into the upper sync
block or to coalesce the two blocks?

I'm just wondering about several situations that have come up recently in
which we need to guarantee that the call to bar() is made without holding
the sync lock. This is generally to avoid a possible deadlock, but it may be
for other liveness-related reasons (call to bar() may block).

For example, in the OSGi framework, calls on the bundle registry must not be
made from synchronized code. (The problem is that any call on the registry
may trigger callbacks on other bundles (listeners) and these callbacks are
always made synchronously, so if two threads call the registry at about the
same time, and they are holding sync locks on bundles that are listening for
registry events, it is easy to get deadlock.)

Thanks in advance for any information or pointers to information.
-- Steve K

More information about the Concurrency-interest mailing list