Tcl Source Code

View Ticket
Login
Ticket UUID: 849901
Title: Cooperative event threading
Type: RFE Created on: 2003-11-26 22:16:37
Submitter: s_a_white Assigned to: andreas_kupries
Subsystem: 49. Threading Severity:
Priority: 5 Medium Last modified: 2009-06-18 15:31:26
Status: Closed Closed by: dkf
Resolution: Accepted Closed on: 2009-06-18 08:31:26
Version: None
Description:
I was wondering if anything could be supported towards
cooperative event threading in standard tcl.

What I mean is that each event + main gets it own
stack.  Currently when we call update or use tkwait
(and friends) the current execution blocks and a new
event starts.  The old execution appears not to start
until completion of all events.

However what about making all stacks independent such
that an execution can be marked as sleeping.  For an
update an old execution will not be marked as runnable
until all events are processed providng the same
behaviour as before.

Things performing tkwait will however become runnable
earlier and perhaps at the end of the current event
things that have become runnable should be processed
before any further new events.

The result is basically cooperative multi-tasking for
events allowing us to provide mutex style event
blocking through tkwait, etc.  This sort of support I
feel is lacking where you are forced to do an update to
complete  an operation and a new overlapped operation
occurs that you wish to process after the current one
has finished, but to do that you must terminate (or
somehow regenerate later) the new one.
User Comments:
dkf added on 2005-01-12 16:45:50:
Logged In: YES 
user_id=79902

I am extremely inclined to say "No Way!" to this. Here's why.

Threads are something that always causes trouble, what with
either too many locks or bizarre race conditions. 
Cooperative threading is a bit better in some ways, but
still causes headaches. And you can get the effects through
careful design of event handlers and callbacks (I've done
this in the past). On a practical implementation level, the
main problem with cooperative threading is that it requires
a total redesign of the way Tcl uses the C stack.

If you really need threads, use the Thread extension and get
*real* threads (i.e. fewer problems with syscalls) together
with a threading/ITC model that is much easier to use. It
might not be everything that you're after, but it is *so*
much less effort on our part! :^)

s_a_white added on 2005-01-12 18:00:01:
Logged In: YES 
user_id=59929

I did try out the threading extensions first in the hope
they would do what I required.  However unless this is
nolonger true, you were forced to have things like seperate
interpreters and such which didn't work.  I need objects,
global variables, displays to be accessable by both.  It
would be great if threading was like c where everything had
access to everything and you just protect as required, this
would work great.

As for the event cooperative order switching: an example
situation (and there are many different others and alot more
complicated) is that the user presses a button to start a
sequence.  This calls some code, which calls so more code,
etc, etc so we are now down a number of levels on the stack
with specific parameters.  Finally we get to a central point
which which decides to act on it but says hang on I can't
just yet because that section of code is already running as
an update had to be executed.

Then as explained we are forced to drop the event or get it
somehow at the back of the event queue.  Ideally i would
just tkwait on variable and then when the other code
finishes it would just resume, however it never does because
the older event is permenantly blocked by this one doing a
tkwait.

I'm afraid I am not aware in the changes required, but
assumed the code supplied its own stack that wouldn't be
tied to the C stack itself (since tcl operations have
different lifetimes to the C parsing of the tcl).  The tcl
pro debugger also shows the stack restarting so there
appears to be some concept of seperate tcl stacks per event,
so hoped it maybe just a matter of order switching (in some
sort of controlled way).

dkf added on 2009-06-18 15:31:26:

allow_comments - 1

Should be possible to use 8.6's coroutine support to do something like this.