Coroutines Again :)

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Coroutines Again :)

Daniel Krenn
Hi All,

Just wondering if any progress has been made with regards to adding
coroutine support to LUA?? - This is the main stumbling block we've
come across while trying set up cooperative character scripts...

** Also, would anyone have any solid examples of coroutine implementation?
i.e. how to handle multiple (i.e. hundreds, maybe thousands) of
independant characters in a persistant world which need to fully
interact with one another? We're very new to scripting, and have had
little luck trying to find help on this particular topic :(

As a note, the project we are working is a real-time dedicated server based
RPG with which we hope to add scripted AI.

Thankyou,
Daniel.



Reply | Threaded
Open this post in threaded view
|

Re: Coroutines Again :)

Luiz Henrique de Figueiredo
>Just wondering if any progress has been made with regards to adding
>coroutine support to LUA??

Support for coroutines is currently our main goal for Lua 4.1.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Coroutines Again :)

Max Ischenko
	Hi there, Luiz Henrique de Figueiredo!
	On Tuesday, 26 of December you wrote:

> >Just wondering if any progress has been made with regards to adding
> >coroutine support to LUA??
> 
> Support for coroutines is currently our main goal for Lua 4.1.

Would you give me a hint what is a `coroutine' and what are good at?

-- 
Best regards, Maxim F. Ischenko.

Reply | Threaded
Open this post in threaded view
|

Re: Coroutines Again :)

Luiz Henrique de Figueiredo
In reply to this post by Daniel Krenn
>Would you give me a hint what is a `coroutine' and what are good at?

Coroutines, as the names suggests, are pieces of code that run "in parallel".
As such, they are similar to multiple threads, except that control is passed
from one coroutine to another in an explicit way, by calling a special transfer
function, whereas with threads the system interrupts them at unknown points.
In this sense, coroutines are much simpler than multiple threads.
So, that's the way we're going with Lua: the simpler thing first :-)

It's not too difficult to add them to Lua, because it runs a virtual machine,
but of course there are lots of details. In particular, we want to make sure
that external mechanisms for coroutines in C can blend well with coroutines
in Lua.

For one implementation of coroutines in C, written by Edgar Toernig, a member
of lua-l, see http://themen01.exit.de/user/member/froese/ .

--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Coroutines Again :)

Joshua Ritter
Coroutines!

Very good sirs... very good indeed!

My last interest in python(stackless) for now has been squashed...

Rock on rebel warriors,
-Joshua

PS:  My email address has changed from [hidden email] to
[hidden email]





Reply | Threaded
Open this post in threaded view
|

Re: Coroutines Again :)

Sebby
In reply to this post by Daniel Krenn
--- In [hidden email], Daniel Krenn <daniel@k...> wrote:
> Hi All,
> 
> Just wondering if any progress has been made with regards to adding
> coroutine support to LUA?? - This is the main stumbling block we've
> come across while trying set up cooperative character scripts...
> 


Hiya.

I did post some code about 1-2 months ago about my lua co-routine 
implementation (unfortunateley i could not post the whole code 
because of a non disclosure agreement i'm under). You could use that 
as a basis for lua co-routines. In general, minimal modifications are 
needed to make coroutines for lua. Unfortunateley, the "quick" 
solution tends to be less portable since it implies assembler code to 
manipulate the system stack and can be unstable on some platforms 
that make some assumptions about the stack (e.x: PS2). Another 
solution that would be nice if implemented in Lua would be to make 
lua stackless. This way coroutines could easily be implemented in a 
cross-platform way.

Originaly, we wanted to use Lua as our scripting system. So i have 
implemented a simple co-routine system on top of Lua. After some 
tests, we are hoewver considering making our own scripter because of 
some performance issues on the Playstation 2 platform.

Sebastien St-Laurent
Software engineer, Z-Axis Ltd.
[hidden email]



Reply | Threaded
Open this post in threaded view
|

Re: Coroutines Again :)

Luiz Henrique de Figueiredo
In reply to this post by Daniel Krenn
>Another 
>solution that would be nice if implemented in Lua would be to make 
>lua stackless. This way coroutines could easily be implemented in a 
>cross-platform way.

That's the way we plan to do it in 4.1.

>Originaly, we wanted to use Lua as our scripting system. So i have 
>implemented a simple co-routine system on top of Lua. After some 
>tests, we are hoewver considering making our own scripter because of 
>some performance issues on the Playstation 2 platform.

That's sad :-(
Was it the performance of your co-routine layer or was it Lua's performance?
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Coroutines Again :)

Edgar Toernig
In reply to this post by Daniel Krenn
Hi,

Daniel Krenn wrote:
> ** Also, would anyone have any solid examples of coroutine
> implementation?

I'll make a first snapshot of my derivate of Lua that includes
coroutines in about 2 weeks.

> i.e. how to handle multiple (i.e. hundreds, maybe thousands) of
> independant characters in a persistant world which need to fully
> interact with one another? We're very new to scripting, and have
> had little luck trying to find help on this particular topic :(

I have to make some notes here.

My coroutine implementation is not system independent because it
uses C coroutines and these cannot be made with ANSI functions.

And about thousands of coroutines: that's where coroutines would
really shine.  Unfortunately, my implementation creates a fixed
stack for Lua and one for C for _each_ coroutine.  You can set
these sizes during coroutine creation but I would say, a total
of less than 4kByte/coroutine becomes ... hmm... dangerous *g*.
So, with thousands of them you get pretty heavy memory usage.

Ciao, ET.

Reply | Threaded
Open this post in threaded view
|

Re: Coroutines Again :)

Sebby
In reply to this post by Luiz Henrique de Figueiredo
--- In [hidden email], Luiz Henrique de Figueiredo <lhf@t...> 
wrote:
> >Another 
> >solution that would be nice if implemented in Lua would be to make 
> >lua stackless. This way coroutines could easily be implemented in 
a 
> >cross-platform way.
> 
> That's the way we plan to do it in 4.1.

That's good, i'm happy to hear that. I'm sure it will make lots of 
people happy :)

> 
> >Originaly, we wanted to use Lua as our scripting system. So i have 
> >implemented a simple co-routine system on top of Lua. After some 
> >tests, we are hoewver considering making our own scripter because 
of 
> >some performance issues on the Playstation 2 platform.
> 
> That's sad :-(
> Was it the performance of your co-routine layer or was it Lua's 
performance?

Yeah, i wish i could have the 2-3 weeks it will take to write my own 
compiler+VM. Considering that in my test case, i only had 2 co-
routines functioning, i think most of the performance impact came 
from Lua. I had 2 routines running (one at 1 execution per second 
which displayed a message to the console, and the second running 10 
times/sec basibly calling a bunch of dummy stub functions that were 
registered to lua) and considering we wanted to keep a constant 60 
frames per second (we don't have a choice on the Playstation 2 or 
else we get visual artifacts), we had a performance hit of ~7-8% on 
the CPU. This is a lot for us considering we were not doing anything 
else in out code at that point (AI, animations, game logic, ...)

Well we'll give a try with our own staticaly typed language in hopes 
we can get better performance out of it. 

Sebastien St-Laurent
Software Engineer, Z-Axis ltd.
[hidden email]


> --lhf


Reply | Threaded
Open this post in threaded view
|

Re: Coroutines Again :)

Maria Julia Dias de Lima
Hi,
I've been making some experimental implementation of
continuations in Lua as part of my PhD thesis. 

I have just made some preliminary tests and there are
some restrictions that are not my focus (at least, for the
time being). One of them is performance.
Another one is that our continuation involves only  the Lua stack,
so it doesn't capture stacks that cross Lua -> C -> Lua calls.
Another assumption is that the local variables remain in the stack, 
so when a continuation is called, it uses the same values which 
were on the stack when the continuation was created.

A continuation is created as a userdata and we used a syntax similar
to setjmp and longjmp functions:  getcont and callcont. 
The following example illustrates coroutines using continuations in Lua.

===
function a()
   write ("Starting A\n")
   co_a.transfer(co_b)
   write ("Back to A\n")
   co_a.transfer(co_b)
   write ("Finishing A\n")
   co_a.transfer(co_b)
end
 
function b()
   write ("   Starting B\n")
   co_b.transfer(co_c)
   write ("   Back to B\n")
   co_b.transfer(co_c)
   write ("   Finishing B\n")
   co_b.transfer(co_c)
end

function c()
   write ("      Starting C\n")
   co_c.transfer(co_a)
   write ("      Back to C\n")
   co_c.transfer(co_a)
   write ("      Finishing C\n")
   co_c.transfer(co_a)
end

function coroutine(f)
  self={}
  self.cont=nil
  self.func=f
  transfer = function(prala)
                local x = getcont() -- creates a continuation
                if x then
                   %self.cont=x
                   prala.resume()
                end
             end

  resume  =  function ()
                if (%self.cont==nil) then  -- if no continuation
                   %self.func()
                else
                   callcont(%self.cont)  -- call the target continuation
                end
             end
  return {transfer=transfer, resume=resume}
end  
  
co_a = coroutine(a)
co_b = coroutine(b)
co_c = coroutine(c)

co_a.resume() 

===

This current implementation of "Lua with continuations"
can be downloaded from: www.inf.puc-rio.br/~mjulia/luacc.tgz. 
Comments and contributions will be very much appreciated.

Regards,
--Julia