On Wed, 2005-07-13 at 09:59 +0100, Richard Jones wrote: > > Actually a version for Ocaml is possible although I > > haven't tried to create one yet -- Felix targets C++ > > but it should work for any language which supports > > classes with virtual functions and switches. > > It should definitely be possible in OCaml. For anyone who wants to > see how this trick is done (in C using setcontext), also look at: > http://www.annexia.org/freeware/pthrlib Ah yes, but there is an important difference: this library supports C code by swapping machine stacks. This works, but it isn't capable of supporting large numbers of threads due to a lack of address space: each stack must reserve the maximum possible amount of memory it could use. If you use malloc, it is even worse, since malloc is required to allocate memory (not merely address space). This kind of library is actually a good argument for a 64 bit processor :) Felix uses a linked list of heap allocated stack frames to avoid this problem. Stackless Python works this way too I think. The cost is slow allocation, however that wouldn't apply to an Ocaml version. Also the whole program optimiser eliminates many calls by inlining, or by observing that there is no blocking operation in the control path, which allows the machine stack to be used instead. The C++ version also switches much faster than setjump/longjmp since only 'the usual' function call overhead is incurred, there's no need to save the whole CPU state. MLton uses linear stacks, but they're not machine stacks, and MLton's copying collector can grow and shrink them. There's another problem with the C stack swapping trick -- it only works with C. It may or may not work with C++, and it is very unlikely to work properly in a multi-language environment (eg: C and Ocaml together). BTW: I'm curious if g++ supports get/setcontext? -- John Skaller Download Felix: http://felix.sf.net