stability issue (how to deal with asynchronous callbacks?)

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

stability issue (how to deal with asynchronous callbacks?)

Asko Kauppi-3

The Lua/SDL_mixer integration is having the following problem:

  Lua runs happily, starts to play a tune  ..dam-dam..
  Attaches a callback to get to the rhytm

  Lua continues running..
	--> sdl_mixer gives a C callback -> sdl_mixer module -> Lua call

Now, the callback probably happens in another thread, or otherwise asynchronously to the main loop, running the Lua stack. How do we deal with this?

Should the Lua core be patched to include a "incoming callbacks" queue, or something, which other threads could write and the main Lua execution thread would read (thus, needing no other synchronization). Or do coroutines provide a solution (I think not).

If the "incoming callbacks queue" solution is suitable, has anyone done such already?

-ak


Reply | Threaded
Open this post in threaded view
|

Re: stability issue (how to deal with asynchronous callbacks?)

D Burgess-4
I have encountered this issue in a completely different application. The 
solution for me was rather simple. Sync. your callbacks.
 If this does not fit  your sound app requirements, I know Adrian has
solved this problem with Lua sound on WIn32 with a rather cool ring
buffer.

DB

On Apr 5, 2005 9:28 PM, Asko Kauppi <[hidden email]> wrote:
> 
> The Lua/SDL_mixer integration is having the following problem:
> 
>    Lua runs happily, starts to play a tune  ..dam-dam..
>    Attaches a callback to get to the rhytm
> 
>    Lua continues running..
>         --> sdl_mixer gives a C callback -> sdl_mixer module -> Lua call
> 
> Now, the callback probably happens in another thread, or otherwise
> asynchronously to the main loop, running the Lua stack.  How do we deal
> with this?
> 
> Should the Lua core be patched to include a "incoming callbacks" queue,
> or something, which other threads could write and the main Lua
> execution thread would read (thus, needing no other synchronization).
> Or do coroutines provide a solution (I think not).
> 
> If the "incoming callbacks queue" solution is suitable, has anyone done
> such already?
> 
> -ak
> 
>

Reply | Threaded
Open this post in threaded view
|

Re: stability issue (how to deal with asynchronous callbacks?)

Ashwin Hirschi-2
In reply to this post by Asko Kauppi-3

The Lua/SDL_mixer integration is having the following problem:

   Lua runs happily, starts to play a tune  ..dam-dam..
   Attaches a callback to get to the rhytm

   Lua continues running..
	--> sdl_mixer gives a C callback -> sdl_mixer module -> Lua call

Now, the callback probably happens in another thread, or otherwise
asynchronously to the main loop, running the Lua stack.  How do we deal
with this?

A start to a solution could be organising the way "Lua continues running".

If you're able to switch your current approach to one that runs in brief steps, you're (probably) pretty much nearly there. It certainly makes the (important!) synchronising bit that David mentions fairly trivial.

Then again, I haven't looked at your sdl_mixer code. So I'm not sure how feasible switching is in your situation...

Ashwin.
--
no signature is a signature.

Reply | Threaded
Open this post in threaded view
|

Re: stability issue (how to deal with asynchronous callbacks?)

Asko Kauppi-3

My current plans go approximately like this:

Since any Lua script utilizing SDL_Mixer also needs to have an SDL event loop, I'll make SDL_PollEvent responsible for the "lua side" fetching of the callbacks.

	Lua runs..
-> sdl_mixer C side callback -> glua_callback_post -> continue.. (no access of Lua here)
	SDL_PollEvent
		-> glua_callback_fetch -> Lua callback
		(looping until no callbacks left in queue)
	...

That's fairly simple, and does not need Lua core mods (good so). There's a lot more details, sure, but this should work, be simple, and modular. Also, the dispatcher and fetcher don't need to intimitely know each other.

Thanks for the ideas!
-ak


5.4.2005 kello 16:16, Ashwin Hirschi kirjoitti:


The Lua/SDL_mixer integration is having the following problem:

   Lua runs happily, starts to play a tune  ..dam-dam..
   Attaches a callback to get to the rhytm

   Lua continues running..
	--> sdl_mixer gives a C callback -> sdl_mixer module -> Lua call

Now, the callback probably happens in another thread, or otherwise
asynchronously to the main loop, running the Lua stack. How do we deal
with this?

A start to a solution could be organising the way "Lua continues running".

If you're able to switch your current approach to one that runs in brief steps, you're (probably) pretty much nearly there. It certainly makes the (important!) synchronising bit that David mentions fairly trivial.

Then again, I haven't looked at your sdl_mixer code. So I'm not sure how feasible switching is in your situation...

Ashwin.
--
no signature is a signature.



Reply | Threaded
Open this post in threaded view
|

Re: stability issue (how to deal with asynchronous callbacks?)

Adrian Sietsma
Asko Kauppi wrote:

My current plans go approximately like this:

Since any Lua script utilizing SDL_Mixer also needs to have an SDL event loop, I'll make SDL_PollEvent responsible for the "lua side" fetching of the callbacks.

    Lua runs..
-> sdl_mixer C side callback -> glua_callback_post -> continue.. (no access of Lua here)
    SDL_PollEvent
        -> glua_callback_fetch -> Lua callback
        (looping until no callbacks left in queue)
    ...

That's fairly simple, and does not need Lua core mods (good so). There's a lot more details, sure, but this should work, be simple, and modular. Also, the dispatcher and fetcher don't need to intimitely know each other.

Thanks for the ideas!
-ak

my PortAudio port uses (brutal i know) a line debug hook (in one implementation). this is set to check the audio buffer status every 1e6 instructions or so, and top-up if required.

audio data is transferred via a userdata with accessors for table-of-floats.

i looked at threads / events etc. for scheduling, but didn't want to mod the core.

it's (fairly) easy to do "hard-sound" apps in lua :

while (true) do
  check status (maybe sleep / wait event)
  update buffer
  update display
end
(better yet do with coroutines, or as a filter chain)

it's that background refresh / save audio data while the app is away with the pixies somewhere that i have problems with.

by the way - i looked at SDL, but it didn't record, as i remember. is this still the case ?

congrats.

ps my new phone supports brew apps ... the nights are just too short :)

Adrian

Reply | Threaded
Open this post in threaded view
|

Re: stability issue (how to deal with asynchronous callbacks?)

Asko Kauppi-3

Probably is. SDL is made for games, and there's not much recording there.


5.4.2005 kello 21:29, Adrian Sietsma kirjoitti:

by the way - i looked at SDL, but it didn't record, as i remember. is this still the case ?



Reply | Threaded
Open this post in threaded view
|

Re: stability issue (how to deal with asynchronous callbacks?)

David Olofson
On Tuesday 05 April 2005 21.46, Asko Kauppi wrote:
> 
> Probably is.

Right; still no audio input in the official versions, but there are 
patches and add-on libs that do it on a few platforms at least. My 
guess is it will get into SDL 1.3 and/or 2.0, since quite a few have 
asked for it.


> SDL is made for games, and there's not much recording  
> there.

Not quite. SDL is made for multimedia, and games are just one form of 
that. Either way, voice chat is pretty popular in some gaming 
communities AFAIK, and there are other uses for audio input in both 
games and other multimedia applications.


//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
|  Free/Open Source audio engine for games and multimedia.  |
| MIDI, modular synthesis, real time effects, scripting,... |
`-----------------------------------> http://audiality.org -'
   --- http://olofson.net --- http://www.reologica.se ---

Reply | Threaded
Open this post in threaded view
|

Re: stability issue (how to deal with asynchronous callbacks?)

Asko Kauppi-3

Once SDL gets recording capabilities, LuaX will support them.. within 24 hrs :)


6.4.2005 kello 02:57, David Olofson kirjoitti:

 On Tuesday 05 April 2005 21.46, Asko Kauppi wrote:

Probably is.

Right; still no audio input in the official versions, but there are
patches and add-on libs that do it on a few platforms at least. My
guess is it will get into SDL 1.3 and/or 2.0, since quite a few have
asked for it.


SDL is made for games, and there's not much recording
there.

Not quite. SDL is made for multimedia, and games are just one form of
that. Either way, voice chat is pretty popular in some gaming
communities AFAIK, and there are other uses for audio input in both
games and other multimedia applications.


//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
|  Free/Open Source audio engine for games and multimedia.  |
| MIDI, modular synthesis, real time effects, scripting,... |
`-----------------------------------> http://audiality.org -'
   --- http://olofson.net --- http://www.reologica.se ---