lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Pedriana, Paul:
> I would like to have a coroutine that automatically yields at every N
> instructions or at some other regular interval. However, this appears to
> be impossible in practice with Lua5. Various techniques I (and others
> who've reported on this) have tried just result in the "attempt to yield
> across metamethod/C-call boundary" error.

The basic problem as I see it (I wrote a mutli-tasking OS in C for a
dedicated machine + a DOS debug version) is that, to have coroutines, you
must have multiple stacks (ie, process state).

Since Lua has its own internal stack mechanism it can implement *internal*
coroutines without problem. However, if one wants to run multiple C code one
needs multiple C stacks. This requires access to the machine stack pointer
etc and, unfortunately, the standard C libraries do not provide that
functionality. Which would make implementing C-based coroutines an extremely
machine dependent endeavour and hence not very suitable for a general Lua
feature.

It might be nice, however, if someone implemented a C-coroutine (or even
multitasking!) library for a common platform. This would provide a framework
for other implementations to be modelled on.

Actually, I REALLY think that Lua would be an excellent core for an
operating system. User programs run as C-functions, OS API calls are called
via a Lua call structure, etc. And I believe that all operating systems
should come with some general language built in (eg, like the original
Smalltalk machines, or sort-of like Unix) so Lua is a good candidate.

*cheers*
Peter Hill.