Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: to have labels or not
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Max Skaller <skaller@m...>
Subject: Control inversion
Given a 'functional/procedural' program which does blocking read
operations,
usually modelled by threading constructions, can we transform this
program into one which operates entirely synchronously using
callbacks from a message dispatcher (where the messages are the
same data that the blocking read would have fetched?

I call this process 'control inversion', because it turns master into
slave, and vice versa: the read 'subroutine' called by the master
algorithm, is turned inside out so that the code is executed
by returning control, and then continuing via a callback (making
the code a slave of the dispatcher).

The requirements for control inversion are to save the state of the
environment and point of control, and to restore the state, modified
by the presence of the 'read' message, at the next point of control.
With 'heavyweight' threads, function programs using the machine stack
are control inverted by operating system level stack/register saving.

With (ultra) lightweight threads, we cannot use the machine stack,
except temporarily, so we must maintain a 'stack' manually,
which is difficult to implement .. by hand. But it should not be so hard
to add 'control inversion' to a bytecode machine.
This has been done for Python (by Chritian Tismer, called Stackless
Python).
But it should also be possible for generated code.

I have a clue that for Haskell, the notion of 'arrows' was invented
to replace monads (these arrows correspond to arrows of categories)
for just this purpose (the application here was thousands of
concurrent interactive web sessions, where existing technology
requires 'recording' the state between interactions in an extremely
clubsy ad hoc (usually perlish) way. My application is different,
but the requirements seem the same.

I'm currently using ocaml to implement a simplistic language
which provides 'automatic' control inversion (allowing programmers
to write threaded programs instead of event driven ones).
Unfortunately, the technique is to generate C++ code.

I'd be very interested to hear more from those that know more about
the inner workings of ocaml, whether there is a possible 'native'
solution to this problem. I imagine this would require language
constructions to specify threads (rather than just a library
function call/calls).

-- 
John (Max) Skaller, mailto:skaller@maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
checkout Vyper http://Vyper.sourceforge.net
download Interscript http://Interscript.sourceforge.net