[Python-Dev] Thread locked sections

M.-A. Lemburg mal@lemburg.com
Tue, 27 Jul 1999 11:23:05 +0200


[The previous mail got truncated due to insufficient disk space;
 here is a summary...]

Mark Hammond wrote:
> 
> > Actually, I think all that's needed is another global like
> > the interpreter_lock in ceval.c. Since this lock is only
> > accessed via abstract functions, I presume the unlock flag could
> > easily be added.
> 
> Well, my personal opinion is that this is really quite wrong.  The most
> obvious thing to me is that we are exposing an implementation detail we all
> would dearly like to see removed one day - the global interpreter lock.
> 
> But even if we ignore that, it seems to me that you are describing an
> application abstraction, not a language abstraction.  This thread started
> with Jim wanting a thread-safe, atomic list operation.  This is not an
> unusual requirement (ie, a thread-safe, atomic operation), so languages
> give you access to primitives that let you build this.
> 
> To my mind, you are asking for the equivilent of a C function that says
> "suspend all threads except me, cos Im doing something _really_ important".
> C does not provide that, and I have never thought it should.   As Gordon
> said, Win32 has critical sections, but these are really just lightweight
> locks.  I really dont see how Python is different - it gives you all the
> tools you need to build these abstractions.
> 
> I really dont see what you are after that can not be done with a lock.  If
> the performance is a problem, then to paraphrase the Timbot, it may be
> questionable if you are using Python appropriately in this case.

The locked section may not be leading in the right direction,
but it surely helps in situations where you cannot otherwise
enforce useage of an object specific lock, e.g. for builtin
file objects (some APIs insist on getting the real thing, not
a thread safe wrapper).

Here is a hack that let's you do much the same with an
unpatched Python interpreter:

sys.setcheckinterval(sys.maxint) # *)
# >=10 Python OPs to flush the ticker counter and have the new
# check interavl setting take effect:
0==0; 0==0; 0==0; 0==0
try:

	...lock section...

finally:
	sys.setcheckinterval(10)

*) sys.setcheckinterval should really return the previous value
   so that we can reset the value to the original one afterwards.

Note that the lock section may not call code which uses the
Py_*_ALLOW_THREADS macros.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                                   157 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/