Python threading (was: Re: global interpreter lock not working as it should)

anton wilson anton_shim at hotmail.com
Mon Aug 5 21:24:42 EDT 2002


I remembered something that changes my "coincidence" theory:
the Linux OS checks if it needs to reschedule after system calls and 
(depending on if you have pre-emptive patch or 2.5+) on return from an 
interrupt in entry.S

This makes the coincidences that allow another thread to grab the GIL as 
such:

1) the python thread currently holding the GIL depletes its time-slice
2) the current python thread calls pthread_cond_signal in this depleted 
state BEFORE the OS notices it needs to be removed from the CPU by other 
means. This means there can be no system calls between the time when the 
process runs out of time and when it calls pthread_cond_signal. (In certain 
cases this means no interrupts either) If the OS notices AND removes it 
before it releases the lock and signals the waiting thread or threads back 
onto the run-queue, it will get to go for another full timeslice.

I have a feeling that this coincidence is very likely.
But it still holds that a python thread will run a full time-slice which is 
usually in the range of 1/10 of a second (110 + ms), but will try to release 
the lock every 10 byte codes which on avg will is in the 10s of microseconds 
range on my 1 Ghz P3.

SCHED_RR threads should also switch at the end of their timeslice. Only 
SCHED_FIFO threads which have no concept of time-slices will never ever 
release the GIL. I have not tested this with the semaphore version of 
Python.


Another side-effect is that if the process it taken of the run-queue before 
calling pthread_cond_signal, all other threads will be blocked for the 
duration of its sleep and the duration of its next run.

If you're wondering what I changed, I had said before that the timer-tick 
and the release have to coincide. This is wrong. The timer-tick only NOTICES 
the depletion of the time-slice but will not take action on it *unless* you 
have a patch or 2.5+ version which makes it check for need to reschedule on 
an interrupt return. Even if this were true, it only happens 100 times a 
second.

Anton


_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com





More information about the Python-list mailing list