Followup-To: comp.programming.threads
> The above sounds like calling a scheduler after each 'basic block', ie
> polling to see if something has changed, (presumably because of some
> operation running on another processor). Doesn't sound all that
> efficient to me, especially if the 'basic block' is part of a tight
> loop <g>
For code which performs lots of context switches, it is faster.
I've measured.
Moreover the same check can fulfill several roles at once: besides
context switching, checking for stack overflow, and delivering Unix
signals such that it is allowed to use arbitrary operations in a
signal handler.

Signature
__("< Marcin Kowalczyk
\__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/
>> Preemptive user-mode threading does not necessarily require use of
>> signals or interrupts. It can be implemented by adding an explicit preemption
>> check to (roughly speaking) each basic block of the generated code. Erlang/OTP
>> and Mozart/Oz use this technique, for example.
More precisely, Mozart/Oz uses a virtual machine and counts VM instructions.
Erlang/OTP with the HiPE compiler does approximately what I said, when executing
compiled code.
>> I don't know whether any Java implementations do.
>
> This is 'preemption'?
Yes, of course. As long as a thread cannot starve other threads for an unbounded
time, without any explicit yield operations in the *source* language, that is
preemptive threading.
> IMHO, if code cannot be interrupted, it cannot be preempted.
There is no difference in semantics between an interrupt-based implementation
and this technique, so it does not make sense to say that one does preemption
and the other not.
> The above sounds like calling a scheduler after each 'basic block', ie
> polling to see if something has changed, (presumably because of some
> operation running on another processor). Doesn't sound all that
> efficient to me, especially if the 'basic block' is part of a tight loop
> <g>
Mozart/Oz and Erlang/OTP have among the most efficient and scalable user-level
thread implementations of any language. Of course there is overhead, but intuition
is not a reliable guide to how much overhead. As Marcin says in another follow-up,
you need to measure.

Signature
David Hopwood <david.nospam.hopwood@blueyonder.co.uk>
Michel Talon - 07 Oct 2006 09:12 GMT
> >> Preemptive user-mode threading does not necessarily require use of
> >> signals or interrupts. It can be implemented by adding an explicit preemption
> >> check to (roughly speaking) each basic block of the generated code. Erlang/OTP
> >> and Mozart/Oz use this technique, for example.
>
> More precisely, Mozart/Oz uses a virtual machine and counts VM instructions.
By the way, python does exactly the same thing. If there are several threads,
it switches threads each 100 bytecode instructions by default.

Signature
Michel TALON
David Hopwood - 07 Oct 2006 16:21 GMT
>>>>Preemptive user-mode threading does not necessarily require use of
>>>>signals or interrupts. It can be implemented by adding an explicit preemption
[quoted text clipped - 4 lines]
>
> By the way, python does exactly the same thing.
CPython, yes.
> If there are several threads, it switches threads each 100 bytecode instructions
> by default.
100 sounds rather small, from the point of view of maintaining cache locality.
I suspect this has not been tuned. In general, performance does not seem to be
a particularly important goal for CPython (that is, it is much slower than a
Python implementation could be without any language changes).

Signature
David Hopwood <david.nospam.hopwood@blueyonder.co.uk>