[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Coroutines & C boundaries
- From: David Given <dg@...>
- Date: Mon, 13 Feb 2006 22:51:49 +0000
I'm trying to write some code in Lua using coroutines, and am having trouble.
My Lua code is being used as a plugin for an external program. The program is
calling into Lua to tell it something's happening. As my algorithm is very
stateful, doing everything from call-ins is a nightmare, which makes it a
classic example for control inversion using coroutines.
Example: when input arrives on a socket, I'm called to tell me that it's
happening.
The core of my Lua code is a scheduler. Whenever input arrives, my C code kick
the scheduler and it runs the first task on the list for one iteration. It
does this by calling the following function with lua_pcall():
function schedule_now()
if (table.getn(taskqueue) == 0) then
idle()
return
end
local task = taskqueue[1]
if (type(task) == "function") then
task = coroutine.create(task)
taskqueue[1] = task
end
coroutine.resume(task)
if (coroutine.status(task) == "dead") then
table.remove(taskqueue, 1)
end
end
This ought to be straightforward enough. I schedule tasks by adding functions
to the taskqueue table. They get run in stages until termination and then
removed. Here's a sample task:
function a_task()
print("Hello,")
coroutine.yield()
print("world!")
end
However, whenever I try to actually use this, the call to coroutine.yield()
fails with a 'attempt to yield across metamethod/C-call boundary' error.
Why?
--
+- David Given --McQ-+ "I must have spent at least ten minutes out of my
| dg@cowlark.com | life talking to this joker like he was a sane
| (dg@tao-group.com) | person. I want a refund." --- Louann Miller, on
+- www.cowlark.com --+ rasfw
Attachment:
pgpLc7mNm2uBm.pgp
Description: PGP signature