Benjamin Tovar | 10 May 22:37 2009
Picon

Threads and function pointers misunderstanding

Hi,

I am writing an application that creates a callback in lisp
(cffi:defcallback callback-lisp ...), and then passes the pointer to a C
library with something like (set-callback-in-c (cffi:callback
callback-lisp). In C, I assign the function pointer as follows:

/* global variable */
void (*callback)(void);

void set_callback_in_c(void (*callback_from_lisp)(void))
{
  callback = callback_from_lisp;
}

And call (*callback)()); as needed.

Now, everything works nicely, unless I use threads in C (pthreads). With
threads, the first time I call (*callback)()); it segfaults, as if the
pointer to the function was not valid (it does get assigned). I have no
clue of what is going on, since it seems that the threads are not
sharing the information to the function pointer. What I am missing here?
I have tried with both Allegro and SBCL, on a Debian "testing".

Thanks!
Luís Oliveira | 10 May 23:54 2009
Picon

Re: Threads and function pointers misunderstanding

On Sun, May 10, 2009 at 9:37 PM, Benjamin Tovar <mugrido <at> gmail.com> wrote:
> Now, everything works nicely, unless I use threads in C (pthreads). With
> threads, the first time I call (*callback)()); it segfaults, as if the
> pointer to the function was not valid (it does get assigned). I have no
> clue of what is going on, since it seems that the threads are not
> sharing the information to the function pointer. What I am missing here?
> I have tried with both Allegro and SBCL, on a Debian "testing".

IIUC, you may only call back into Lisp within a thread that has a Lisp
stack. So, you can't invoke those callbacks from threads created in C.

--

-- 
Luís Oliveira
http://student.dei.uc.pt/~lmoliv/

Gmane