lua-users home
lua-l archive

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


Hello all.

As the subject says, I'm currently attempting to implement simple
pre-emptive multi-tasking of co-routines, using the LUA_MASKCOUNT hook,
registered to a C function that presently looks like :

void countHook(Lua::lua_State *L, lua_Debug *ar)
{
   if (L->nCcalls > 0)
	   return;

   lua_yield(L,0);
}

I've run through the list archives, but couldn't find anything that explains
the error that I'm seeing, so I thought I'd best ask.

My main Lua script looks like :

	A = 0.0;
	B = 0.0;

	while 1 do
		coroutine.resume(ChildState.Coroutine1);
		coroutine.resume(ChildState.Coroutine2);
	end	

While, coroutines 1 and 2 are simply :

	print("----Entering CR1----")
	while 1 do
		A=A+1;
		print("Variable A is ", A);
	end
	print("----Leaving CR1 ----")

and

	print("----Entering CR2----")
	while 1 do
		B=B-1;
		print("Variable B is ", B);
	end
	print("----Leaving CR2 ----")

Using the above hook method, with a count set to 50, co-routine 1 runs, and
yields, then co-routine 2 does it's thing.  However, once it yields and
co-routine 1 is resumed, the following assert in lvm.c fires :

	lua_assert(L->top == L->ci->top ||
         GET_OPCODE(i) == OP_CALL ||   GET_OPCODE(i) == OP_TAILCALL ||
         GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO); 

as L->top & L->ci->top are now different.

Has anyone encountered this before?  Is there something blindingly obvious
that I'm failing to notice or do inside my hook function?

All suggestions, gratefully recieved.

Dan