Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: errno global vx. errno pthread...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: errno global vx. errno pthread...


Below is the message from the list archive, I was referring to. 


Re: pthreads and errno

Matt, everyone,

Thanks very much for the help - and Matt, for filing the bug report.

In the meantime, would a workaround be to redefine __error() (or a custom function) to just return &self->err_no, rather than checking to see if it's the main thread that's requesting it?


On Sunday, October 20, 2002, at 10:33  PM, Matt Watson wrote:

On Sunday, October 20, 2002, at 09:13 PM, Ian Lister wrote:

On Sun, 20 Oct 2002, Scott D.W. Rankin wrote:
#include <pthread.h>
#include <stdio.h>
#include <fcntl.h>

It needs to #include <errno.h> to get the macro definition of errno in <sys/errno.h>. I'm surprised this test isn't broken on more platforms.

Actually, that's not causing the problem the OP is seeing. <errno.h> gets included by <pthread.h>, which causes errno to be defined as:

#define errno (*__error())

The problem is in the implementation of __error():

#undef errno
int *__error(void) {
pthread_t self = pthread_self();
/* If we're not a detached pthread, just return the global errno */
if ((self == (pthread_t)0) || (self->sig != _PTHREAD_SIG) || (self->detached & _PTHREAD_CREATE_PARENT)) {
return &errno;
return &self->err_no;

Libc has both global and per-thread errno values, which both get set on return from a failed library call. The self->detached check above is true in the case of the main thread, so the main thread always uses the global errno value, and the global errno value is always set at the same time the per-thread value is set.

While the window for this race *should* be small (programs are supposed to only check errno immediately after a failed library call), I've filed a bug against the "optimization" in the __error() implementation...

unix-porting mailing list | email@hidden
Do not post admin requests to the list. They will be ignored.

Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

 >errno global vx. errno pthread... (From: "Michael Andronov" <email@hidden>)
 >Re: errno global vx. errno pthread... (From: Patrick Rutkowski <email@hidden>)

Visit the Apple Store online or at retail locations.

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.