<div dir="ltr">Franz support has said that setting :errno-value is the only guaranteed way to get errno. Macros such as without-interrupt, without-scheduling, and with-delayed-interrupts, are not sufficient.</div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Thu, Aug 8, 2013 at 5:53 PM, Felix Filozov <span dir="ltr"><<a href="mailto:ffilozov@gmail.com" target="_blank">ffilozov@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">I'm not sure what's overwriting errno. What sort of interrupts do you expect could be overwriting errno, and wouldn't they have to go through the same mechanism of writing to errno like any other thread?<div>
<br></div><div>Looking into Allegro CL SMP's manual, there's a macro called <a href="http://www.franz.com/support/documentation/9.0/doc/operators/excl/with-delayed-interrupts.htm" target="_blank">with-delayed-interrupts</a>, which should prevent interrupts and GC from running. However, the wrong errno was still returned when I used this macro. I will ask Franz if such a macro exists.</div>
<div><br></div><div>I started exploring your second suggestion. I created an errno defstruct (this is what the user passes), in the CFFI package, and I tried passing that structure to the CFFI-SYS layer to be modified, but then I realized that CFFI-SYS doesn't know anything about the CFFI package so it can't modify the structure. So I'm passing a function to CFFI-SYS, which gets called with the returned errno value as an argument. I don't know if I like this callback mechanism.</div>
<div><br></div><div>Also, while reading through the code I noticed that there's some edge cases that would need to be handled. For example %defcfun may make a call to foreign-funcall if the argument types and the returns types are considered call-by-value. What does that mean?</div>
<div><br></div><div>I also don't quite understand what entry-vec is, if that's something that can be ignored for now.</div><div><br></div><div>By the way, I closed my pull request since this clearly would need more work.</div>
<div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Aug 7, 2013 at 7:44 PM, Luís Oliveira <span dir="ltr"><<a href="mailto:luismbo@gmail.com" target="_blank">luismbo@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Felix Filozov <<a href="mailto:ffilozov@gmail.com" target="_blank">ffilozov@gmail.com</a>> writes:<br>
<br>
> In Allegro CL, the only safe way to get errno after a foreign function<br>
> call is to configure the foreign function definition to return it as a<br>
> second value.<br>
<br>
</div>Why does that happen. Is it because interrupt handlers may call<br>
functions that overwrite errno?<br>
<div><br>
<br>
> CFFI doesn't deal with errno. There has been some previous discussion<br>
> (<a href="http://lists.common-lisp.net/pipermail/cffi-devel/2009-January/003017" target="_blank">http://lists.common-lisp.net/pipermail/cffi-devel/2009-January/003017</a>.<br>
> html) to return errno safely, but doesn't seem like it went anywhere.<br>
<br>
</div>Re-reading that was useful. Thanks for the pointer.<br>
<br>
To sum that discussion up: it'd nice to have cffi-sys:get-errno plus a<br>
cffi-sys:without-3rd-party-messing-of-errno so we can implement errno<br>
handling in the portable side of CFFI.<br>
<br>
Do you think that cffi-sys:without-3rd-party-messing-of-errno macro is<br>
feasible? If not, we'll have to place the abstraction at the<br>
%FOREIGN-FUNCALL level as you suggest.<br>
<div><br>
<br>
> So any library that tries to get errno is potentially broken in<br>
> Allegro CL. I'm seeing this in practice with lisp-zmq, for example.<br>
<br>
</div>Out of curiosity, what's messing with errno in your case?<br>
<div><br>
<br>
> I'd like to introduce a new option to defcfun and foreign-funcall<br>
> called :errno.<br>
<br>
> It would look like this: (foreign-funcall ("strlen" :errno t) :string<br>
> "foo" :int), or (defcfun (strlen "strlen" :errno t) :int (s :string)).<br>
><br>
> Calling (strlen) would return two values, the return value of the<br>
> foreign call, and errno.<br>
><br>
> In some Lisps, the only way to get errno is to make an additional<br>
> foreign call. Then perhaps that call could be made by CFFI and returns<br>
> as the second value.<br>
<br>
</div>That makes sense. Except for the second value bit because CFFI types can<br>
yield more than one value. Alternatives that pop up:<br>
<br>
1. Return it as the first value. (Probably requires some<br>
multiple-value-list mangling which might not be acceptable<br>
performance-wise?)<br>
<br>
2. Have the foreign function accept an extra errno argument that takes<br>
some structure than can be modified with the errno value.<br>
<br>
Not quite happy with either. Any other ideas?<br>
<div><br>
<br>
> Since CFFI delegates the handling of errno to the implementation,<br>
> perhaps we could preserve the status quo and not get bugged down in<br>
> grandiose plans of portability.<br>
<br>
</div>CFFI /is/ a grandiose excursion into portability. :-)<br>
<span><font color="#888888"><br>
Luís<br>
<br>
<br>
</font></span></blockquote></div><br></div>
</div></div></blockquote></div><br></div>