[plt-scheme] FFI + errno

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Dec 20 20:32:42 EST 2009

At Mon, 21 Dec 2009 01:30:14 +0100, Thomas Chust wrote:
> Matthew Flatt schrieb:
> > At Sun, 20 Dec 2009 08:56:49 +0100, Thomas Chust wrote:
> >> 2009/12/15 Matthew Flatt <mflatt at cs.utah.edu>:
> >>> [...]
> >>> With the latest in SVN (v4.2.3.5), the `_fun' form of `scheme/foreign'
> >>> supports an optional `#:save-errno' specification that makes the FFI
> >>> squirrel away the value of `errno' immediately after a foreign call.
> >>> The saved value is available through a new `saved-errno' function.
> >>> [...]
> >> [...]
> >> wouldn't it be nicer to have a special kind of output specification
> >> for errno values in a _fun form? Since errno is effectively a
> >> container for an additional return value from a function, not some
> >> kind of permanent state variable, this would seem more natural to me.
> >> [...]
> > This sounds complicated to me, because cleaning up `errno' handling is
> > specific to a particular foreign function.
> > [...]
> 
> Hello,
> 
> actually I don't propose to add fancy automatic handling of errno
> values. I was rather thinking of something very simple like, for
> example, replacing your
> 
> > [...]
> >  (_fun #:save-errno 'posix _bytes
> >                     -> (res : _int)
> >                     -> (if (equal? res -1)
> >                            (error 'errno "~a" (saved-errno)) 
> >                            res))
> > [...]
> 
> with something like
> 
>    (_fun _bytes
>          -> (res : _int) (errno : (_errno 'posix))
>          -> (if (equal? res -1)
>                 (error 'errno "~a" errno)
>                 res))
> 
> where _errno would be a special output type expression.
> 
> Of course it would be functionally equivalent, but I think this variant
> looks cleaner and is more in the Scheme spirit since it treats the
> additional error code output of the function as an output instead of
> replicating the strange behaviour of C by storing a function output in a
> thread local parameter for no added value.

Oh, yes, that's sensible. I should have understood your suggestion,
because I considered a variant of that style. From my explorations,
though, I decided that propagating `errno' that way would be much more
complicated and not a big improvement, while exposing a C-style
interface was reasonable for a FFI.

If you or anyone feels strongly about this, I'd be happy to accept and
help apply a patch to change the way `errno' works.



Posted on the users mailing list.