[Vm-dev] Re: [Pharo-dev] Question about errno and NB FFI

David T. Lewis lewis at mail.msen.com
Tue Dec 15 23:18:03 UTC 2015


On Tue, Dec 15, 2015 at 01:52:15PM -0800, Eliot Miranda wrote:
>  
> On Tue, Dec 15, 2015 at 1:34 PM, Mariano Martinez Peck <
> marianopeck at gmail.com> wrote:
> 
> > Hi guys,
> >
> > I am using NB FFI and I am calling a function that could answer a NULL* in
> > case of error and that it should have set the number on *errno.*
> >
> > So now I have some questions:
> >
> > Say the code is this:
> >
> > run
> > | whatever |
> > whatever := self *primitiveWhatever*.
> > ^ popenFile ifNil: [
> > self *primitivePerror*.
> >
> > Imagine that the C lib called from #primitiveWhatever was the one setting
> > *errno.*
> > My question is... the following #primitivePerror is a DIFFERENT system
> > call. Couldn't that happened that another error happened before the first
> > system call and time I am executing the second one and so the "last error"
> > (errno) is actually the wrong one I am thinking of?
> >
> 
> Yes, which is why it's really important that the FFI be able to be told to
> answer errno on error. See e.g. the VW FFI where IIRC one can specify that
> if a call answers -1, the FFI call can fail with a primitive error code
> that includes errno.
> 
> 
> >
> > Even worst, I may think that the scope of the  "global variable errno" may
> > existed at the scope of the invoked lib only? In this case I don't have
> > above problem, but then....how can I get such number once the NB primitive
> > finished (so that I can call, for example,  strerror() ?
> >
> 
> Well, there are two issues here.  One is that one should use waterer the
> underlying platform defines for getting "errno", for example some platforms
> will say something like #define errno get_errno(), to get the errno
> specific to the current thread.  Second, all one's libraries need to use
> the same approach so that errno is shared between the VM and the libraries,
> otherwise your library may not set the errno seen by the VM and the FFI.

There are some tips at http://www.unix.org/whitepapers/reentrant.html

Googling "thread safe errno" will turn up some more.

Also see "man 3 errno" on your gnu/linux system.

It is system dependent, but in general if the platform support Posix threads,
then there will be a way to get access to an errno value on a per thread basis.
It is important to #include <errno.h> as oppposed to manually declaring
extern int errno.

In addition there are usually compile time defines or gcc parameters that
tell the compiler to pay attention to thread safe error handling. I found
some tips at http://fixunix.com/unix/84275-errno-thread-safe.html

Dave



More information about the Vm-dev mailing list