Do LUA coroutines work on the camera? - Script Writing - CHDK Forum  

Do LUA coroutines work on the camera?

  • 5 Replies
  • 2441 Views
Do LUA coroutines work on the camera?
« on: 02 / December / 2019, 00:56:06 »
Advertisements
If I replace
Code: [Select]
play_sound(4) in my script with 
Code: [Select]
coroutine.resume(coroutine.create(play_sound),4) the camera doesn't beep, and there isn't any error. I'm new to lua, is there anything wrong with this coroutine usage?

*

Offline reyalp

  • ******
  • 13307
Re: Do LUA coroutines work on the camera?
« Reply #1 on: 02 / December / 2019, 02:27:27 »
If I replace
Code: [Select]
play_sound(4) in my script with 
Code: [Select]
coroutine.resume(coroutine.create(play_sound),4) the camera doesn't beep, and there isn't any error. I'm new to lua, is there anything wrong with this coroutine usage?
I think they may be a bit broken*, but even if they weren't, they wouldn't do what you want here. Lua coroutines are a purely Lua construction, which isn't connected the camera (or other Lua host program) task scheduling. They can be used to implement things like producer/consumer patterns or cooperative multi-tasking within a script, but they won't let you play a sound asynchronously.

coroutine.create only accepts Lua functions, not C functions like play_sound, so your example should generate an error like
Code: [Select]
bad argument #1 to 'create' (Lua function expected)

* I'm pretty sure Lua coroutines would have issues with the way our C code yields back to the camera OS when your call things like sleep, shoot() or key press functions, and possibly also with the debug hook that yields periodically to avoid hogging the CPU. Yes, I said coroutines weren't connected to host task scheduling, but we abuse Lua yield in C code.
Don't forget what the H stands for.

*

Offline Caefix

  • *****
  • 528
  • Sorry, busy deleting test shots...
Re: Do LUA coroutines work on the camera?
« Reply #2 on: 12 / November / 2020, 15:07:32 »
If there is no known use for it, maybe some bytes could be saved if You "ifdef" them out?  :-[
All lifetime is a loan from eternity.

*

Offline Caefix

  • *****
  • 528
  • Sorry, busy deleting test shots...
Re: Do LUA coroutines work on the camera?
« Reply #3 on: 02 / January / 2021, 09:41:37 »
If there is no known use for it, maybe some bytes could be saved if You "ifdef" them out?  :-[
:xmas  // sizeof("lua.flt")-=~1.7k with...
Code: [Select]
lib/lua/lbaselib.c  line 515 ++
#ifdef HOST_LUA
line 650 ++
#endif
...
...
LUALIB_API int luaopen_base (lua_State *L) {
  base_open(L);
#ifdef HOST_LUA
  luaL_register(L, LUA_COLIBNAME, co_funcs);
#endif
  return 2;
}
++ 1800 bytes free memory, compiled with same conditions, probably a lot for some good old cams.
« Last Edit: 02 / January / 2021, 14:01:26 by Caefix »
All lifetime is a loan from eternity.


*

Offline reyalp

  • ******
  • 13307
Re: Do LUA coroutines work on the camera?
« Reply #4 on: 03 / January / 2021, 00:48:02 »
Thanks. I disabled it in trunk r5699.

I verified that is broken in the the general case. The following code should return 10000, 20000, ... 50000, but on the camera (running from chdkptp on elph130) returns nothing.
Code: [Select]
co=coroutine.wrap(
function()
local xx = {1,2,3,4,5}
while #xx > 0 do
local x = table.remove(xx,1)
local j
for i=1,10000 do
j = x*i
end
coroutine.yield(j)
end
end
)
t={}
repeat
x = co()
table.insert(t,x)
until not x
return t

This is (edit: probably, I thought :-[) because the lua_script yield code (called from the debug hook in the case above, but also used for explicit sleep, wait_click etc) doesn't keep track of which coroutine has it has yielded.  I think this can be fixed pretty simply, and will look into.

On the one hand, if no one has wanted coroutines for the last 12 years, maybe saving the memory is better. OTOH maybe people have tried to use them, been frustrated and given up.

Long term (lol) I'd like to make Lua C modules work (Lua loading a .flt that implements a Lua library) which would allow some of the lesser used stuff to only be loaded by scripts that need it. That requires the loaded modules be able to import a bunch of C functions from the Lua core at runtime. I may actually try to do this for 1.6

I also debated whether the coroutine lib should be under #if 0 or #ifdef HOST_LUA. hostlua is nominally supposed to reflect the camera configuration, which would argue for #if 0, but in practice we can just exclude it form emu.lua. Coroutines would actually be a natural pattern to use for emulating camera functions.

edit for completeness:
In the previous code, short coroutines that don't use yielding functions would *appear* to work most of the time, but could break unpredictably. set_yield could be used to avoid automatic yielding in coroutines.
« Last Edit: 03 / January / 2021, 01:46:29 by reyalp »
Don't forget what the H stands for.

*

Offline reyalp

  • ******
  • 13307
Re: Do LUA coroutines work on the camera?
« Reply #5 on: 18 / January / 2021, 21:49:46 »
I merged this to the 1.5 branch in r5708. While it arguably breaks my "keep stable releases feature stable" rule, I decided it was better to have it clearly break for anyone trying to use coroutines rather than randomly misbehave.

I spent some time looking into fixing it. It should be possible, but is non-trivial. If a Lua coroutine yields via the hooks or blocking functions like sleep, the C code would need to know to resume that coroutine instead of Lt, but then subsequent yields from that coroutine in Lua end up back in our C code rather than yielding back to what ever Lua code started it.
Don't forget what the H stands for.

 

Related Topics