From: Tamas Papp
Subject: returning cffi pointers with sb-threads
Date: 
Message-ID: <878xapa8sx.fsf@pu100877.student.princeton.edu>
Hi,

I have a C function that allocates/fills two C structures then enters
into a loop (catching events for an X11 window).  I need these two
structures while it is running, so I thought I would pass pointers to
the C function, where it can place pointers to these new stuctures.
After the thread has started, I save the thread and the two pointers
in a class.  The Lisp code looks like this (I am using SBCL):

(cffi:defcfun ("create_xlib_context" create_xlib_context) :int
  (display_name :string)
  (width :int)
  (height :int)
  (xc-pointer :pointer)
  (context-pointer :pointer))

(defun create-xlib-context (width height &optional (display-name ":0"))
  (let ((xlc (make-instance 'xlib-context))
	(xc-pointer (foreign-alloc :pointer))
	(context-pointer (foreign-alloc :pointer)))
    (setf (slot-value xlc 'thread)
	  (sb-thread:make-thread 
	   (lambda () 
	     (let ((err (create_xlib_context display-name width height
					     xc-pointer context-pointer)))
	       (unless (zerop err)
		 (error "Error ~a when creating xlib-context." err))))))
    ;; extract slots
    (setf (slot-value xlc 'xc) (mem-ref xc-pointer :pointer))
    (setf (slot-value xlc 'pointer) (mem-ref context-pointer :pointer))
    (foreign-free xc-pointer)
    (foreign-free context-pointer)
    xlc))

The skeleton of the C functions is

create_xlib_context(char *display_name,
		    unsigned int width,
		    unsigned int height,
		    xlib_context **xc_pointer,
		    cairo_t **context_pointer) {
...
    *xc_pointer = xc;
    *context_pointer = context;
...
    for (;;) {
...
    }
}

Something weird is going on, because the two pointers I get back are
not the right ones (I print them from the C function for debugging).
Any suggestions?

Thanks

Tamas

From: David Lichteblau
Subject: Re: returning cffi pointers with sb-threads
Date: 
Message-ID: <slrnf6t4np.67g.usenet-2006@babayaga.math.fu-berlin.de>
On 2007-06-12, Tamas Papp <······@gmail.com> wrote:
> Something weird is going on, because the two pointers I get back are
> not the right ones (I print them from the C function for debugging).
> Any suggestions?

The parent thread can continue execution before the child had a chance
to complete the call to the foreign function.  Use condition variables
to have the parent thread wait for the child to notify it.

(Or, of course, compute those values in the parent before starting the
thread, if that is possible.)
From: Tamas Papp
Subject: Re: returning cffi pointers with sb-threads
Date: 
Message-ID: <874pld9qpv.fsf@pu100877.student.princeton.edu>
David Lichteblau <···········@lichteblau.com> writes:

> On 2007-06-12, Tamas Papp <······@gmail.com> wrote:
>> Something weird is going on, because the two pointers I get back are
>> not the right ones (I print them from the C function for debugging).
>> Any suggestions?
>
> The parent thread can continue execution before the child had a chance
> to complete the call to the foreign function.  Use condition variables
> to have the parent thread wait for the child to notify it.
>
> (Or, of course, compute those values in the parent before starting the
> thread, if that is possible.)

Thank you very much, this is what happened.

Tamas