Hi Tim, I spent some time playing around with interfacing to multi-threaded C libraries for AudioIO, namely portaudio. In Portaudio you register a callback which gets called periodically whenever the audio card needs a new frame of data. It gets called from a separate thread, so I was getting segfaults if I executed julia code directly from the callback.
In the end I wrote a small C module that implemented a callback which I kept synchronized with a Julia Task. For the C callback to wake up the Task I used a file descriptor (that way in Julia land I could wait on it and only block that task instead of the whole julia process). For the Julia Task to wake up the C callback I used a semaphore. You can see my C code (as well as the BinDeps stuff to compile it) in the AudioIO repo at https://github.com/ssfrr/AudioIO.jl I'm not sure if this approach would work in your situation, but if your C library is spawning a separate thread I think this is one way to handle it. -s On Fri, Jan 31, 2014 at 7:58 AM, Tim Holy <[email protected]> wrote: > I'm interfacing with a C library that provides the ability to set a > callback > function so you can be notified when some job is done. I'm hoping to > exploit > this with the Task interface so I can feed multiple jobs with a single > Julia > process. I'm declaring my notify function like this: > > function callback_notify(hnd, status, data) > println("In notify") > s = unsafe_pointer_to_objref(data)::Stream > @show s > s.status = status > notify(s.c) > println("Done notifying") > nothing > end > > Stream is a type that has a Condition member, > type Stream > handle::StreamHandle > status::Cuint > c::Condition > end > > I've successfully set the callback function, issued wait(s.c), and from the > println statements I can see that the notify callback executes fully. > However, > my task never wakes up. My suspicion is that the C library is spawning a > new > thread; does that pose problems for the Task interface? Or is something > else > happening? Any good workarounds? > > --Tim > >
