[plt-scheme] FFI + errno
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.
> > [...]
>
> Hello,
>
> 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.
> It would also make error handling for functions with special error
> return values and functions which just set errno more similar, and it
> would probably cause less runtime overhead.
This sounds complicated to me, because cleaning up `errno' handling is
specific to a particular foreign function. For example, maybe `errno'
should be consulted when the result from a foreign function is -1, or
maybe it's when the result is a NULL pointer. Maybe the right thing
to do with `errno' is just raise an exception, or maybe the operation
should be retried, or maybe the errno value should be returned.
It might make sense to build shortcut for common patterns into `_fun'
somehow. For example, maybe something like
(_fun #:check-posix-errno -1 _bytes -> _int)
could turn into
(_fun #:save-errno 'posix _bytes
-> (res : _int)
-> (if (equal? res -1)
(error 'errno "~a" (saved-errno))
res))
In any case, you could write macros over `_fun' for specific patterns.
The goal of `#:save-errno' and `saved-errno' is the make the patterns
possible in the simplest, most general, and most efficient way I could
find.