[Sbcl-help] Callback from C

88 views
Skip to first unread message

Robert Bebop

unread,
May 3, 2012, 6:35:29 AM5/3/12
to sbcl mailing list
Hi all,

Is it actually possible to callback from C with SBCL. According to the
manual it's tricky but the CFFI manual claims that it's possible on some
platforms with SBCL.

I am a musician and enjoy messing around with audio DSP. I currently use
SBCL on a GNU/Linux platform and have written a C wrapper for the JACK
audio server. I am getting pretty good real-time performance with the
current blocking method I am using (about 10 -15 ms latency) but it
should improve if I can get the server to callback into LISP.

I have written the FFI interface using SBCL's interface but if CFFI
works I will recode to use it.

Another question I have is whether there could be some unpleasant side
effects of having a real-time thread callback into LISP.

Thanks,

Robert.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Sbcl-help mailing list
Sbcl...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sbcl-help

Kalyanov Dmitry

unread,
May 3, 2012, 6:47:56 AM5/3/12
to sbcl...@lists.sourceforge.net
0On Thu, 2012-05-03 at 12:35 +0200, Robert Bebop wrote:
> Hi all,
>
> Is it actually possible to callback from C with SBCL. According to the
> manual it's tricky but the CFFI manual claims that it's possible on some
> platforms with SBCL.
>
> I am a musician and enjoy messing around with audio DSP. I currently use
> SBCL on a GNU/Linux platform and have written a C wrapper for the JACK
> audio server. I am getting pretty good real-time performance with the
> current blocking method I am using (about 10 -15 ms latency) but it
> should improve if I can get the server to callback into LISP.
>
> I have written the FFI interface using SBCL's interface but if CFFI
> works I will recode to use it.
>
> Another question I have is whether there could be some unpleasant side
> effects of having a real-time thread callback into LISP.
>
> Thanks,
>
> Robert.

It is possible to call back from C to SBCL (with either SBCL's FFI or
with CFFI). The big caveat is that callback invocation has to happen on
a Lisp thread (an initial Lisp thread or thread created with
sb-thread:create-thread).

As far as I know, many audio APIs invoke callbacks in their own threads
which conflicts with the requirement of invoking callback in Lisp
thread. If JACK does that, one option is to have callback written in C
that notifies waiting Lisp thread (for example, via a condition
variable) and marshalls the data between Lisp and JACK.

Robert Bebop

unread,
May 3, 2012, 7:57:19 AM5/3/12
to sbcl...@lists.sourceforge.net
El 03/05/12 12:47, Kalyanov Dmitry escribió:
Hi Kalyanov,

That's exactly what happens, JACK calls back in it's own thread which is
running in hard real-time.

Signaling a condition sounds like the best way to go although I'm not
sure as to how to signal a Lisp condition from C and can't see anything
in the manual about it?

Leslie P. Polzer

unread,
May 3, 2012, 8:18:13 AM5/3/12
to Robert Bebop, sbcl...@lists.sourceforge.net
On Thu, May 03, 2012 at 01:57:19PM +0200, Robert Bebop wrote:
> Signaling a condition sounds like the best way to go although I'm not
> sure as to how to signal a Lisp condition from C and can't see anything
> in the manual about it?

He was talking about condition *variables*. They help with thread
coordination and don't really have anything to do with Common Lisp
conditions.

Leslie

Robert Bebop

unread,
May 3, 2012, 9:06:15 AM5/3/12
to Leslie P. Polzer, sbcl...@lists.sourceforge.net
El 03/05/12 14:18, Leslie P. Polzer escribió:
> On Thu, May 03, 2012 at 01:57:19PM +0200, Robert Bebop wrote:
>> Signaling a condition sounds like the best way to go although I'm not
>> sure as to how to signal a Lisp condition from C and can't see anything
>> in the manual about it?
> He was talking about condition *variables*. They help with thread
> coordination and don't really have anything to do with Common Lisp
> conditions.
>
> Leslie
Ok, thanks Leslie, I misunderstood.

That is actually what I'm doing now with pthread conditions, I thought
Kalyanov was suggesting that a LISP condition would be a good way to go.
It seems that the limitations on performance depend on the GC and lack
of priority scheduling on the sb-thread. I guess I'll have to wait until
SBCL develops some kind of scheduling mechanism for different
sb-threads. I am too nervous about running SBCL top-level in hard real-time.

Cheers.
Reply all
Reply to author
Forward
0 new messages