[Python-Dev] Threading idea -- exposing a global thread lock

Josiah Carlson jcarlson at uci.edu
Tue Mar 14 17:58:10 CET 2006


Samuele Pedroni <pedronis at strakt.com> wrote:
> 
> Raymond Hettinger wrote:
> 
> > [Samuele Pedroni]
> >
> >> there's no sys.checkinterval in Jython. Implementing this would need the
> >> introduction of some kind of GIL implementation in Jython, the JVM 
> >> has no primitive for global critical sections.
> >
> >
> > Wouldn't Java implement this directly by suspending and resuming the 
> > other threads (being careful to avoid access to monitored resources 
> > and to pair the suspend/resume operations in a try/finally or 
> > with-statement to prevent deadlocks)?
> 
> suspending a thread is a deprecated operation because it can cause 
> deadlocks.

There are two assumptions that one can make about code using the "gil",
or the equivalent of suspending all threads but the current one, or in
Python, just disabling thread switching; I'll call it a (global)
'critical section'.

Either the user is going to rely on just the critical section for
locking, or the user is going to mix locks too.  If the user doesn't mix
(even implicitly with Queue, etc.), then there can be no deadlocks
caused by the critical section.  If the user _is_ mixing standard locks
with critical sections, the only _new_ potential cause of deadlocks is
if the user attempts to acquire locks within the critical section which
have already been acquired by another thread.  Deadlocks of this
particular type, however, can be generally prevented by making locks
aware of critical sections and raising an exception whenever a lock
acquisition is taking place within a critical section.  You wouldn't
want the exception to only be raised if the acquisition would block, as
this would result in intermittant errors; just make it an error.


It would be nice if Jython or IronPython could (and would) implement
these 'critical sections'.  Whether they can or not, I think that it
would be a useful feature in the CPython runtime.  It could be
considered a platform-specific feature, similar to how you can use
select on any file handle on *nix, but you need to jump through hoops to
get a similar thing on Windows.

I'm +1, but only because I've spent more than my share of time digging
around with threads, locks, Rlocks, conditions, events, etc.

 - Josiah



More information about the Python-Dev mailing list