[racket] How to invoke a blocking foreign function without blocking whole racket?

From: Tim Brown (tim at timb.net)
Date: Wed Jan 30 06:28:39 EST 2013

Resend... original (and previous resend!) sent from an address not
valid for the list.

---------- Forwarded message ----------
From: Tim Brown <tim.brown at timb.net>
Date: 30 January 2013 10:01
Subject: Re: [racket] How to invoke a blocking foreign function
without blocking whole racket?
To: Haiwei Zhou <highfly22 at gmail.com>
Cc: users at racket-lang.org


There are internal FFI calls: scheme_make_fd_input_port and
scheme_make_fd_output_port, documented in
http://docs.racket-lang.org/inside/Ports_and_the_Filesystem.html?q=to%20port#(idx._(gentag._480._(lib._scribblings/inside/inside..scrbl)))

If you would normally be able to poll (or select) from the port, then
a little bit of work will allow you to tie your IO closer to the
racket events system.

There are more involved variants, I've mostly only needed this level
of access at the racket level; servicing the known-to-be ready fd is
done by your FFI library.

Tim

On 30 Jan 2013 09:44, "Haiwei Zhou" <highfly22 at gmail.com> wrote:
>
> Finally, I figured out the callback from this letter: http://lists.racket-lang.org/users/archive/2010-July/040409.html . I wish I red this letter early.
>
> After realized that the foreign function blocks the caller thread, I used dynamic-place to create worker thread. The code is here : https://github.com/highfly22/alert/blob/master/inotify.rkt
>
> Racket is amazing simple after you know the bloody details.
>
> Haiwei
>
>
> On 30 January 2013 14:23, Haiwei Zhou <highfly22 at gmail.com> wrote:
>>
>> Hi,
>>
>>  I try to wrap inotify API. After adding a watcher, I try to read events from file descriptor. But the racket thread is blocking until the read operation is done. Here is the definition of read.
>>
>> (define _read (get-ffi-obj "read" libc (_fun #:async-apply (lambda (f) (f))
>>                                              #:save-errno 'posix
>>                                             _fd_t
>>                                             (output : (_bytes o size))
>>                                             (size : _uint32)
>>                                             -> (r : _int32)
>>                                             -> (values r saved-errno output))))
>>
>> I am confused by the term callback. Is the callback called in the racket thread or another OS thread?
>>
>> Thanks,
>> Haiwei
>
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

Posted on the users mailing list.