Re: errno global vx. errno pthread...
Re: errno global vx. errno pthread...
- Subject: Re: errno global vx. errno pthread...
- From: "Michael Andronov" <email@hidden>
- Date: Fri, 3 Oct 2008 21:22:33 -0400
Patrick-
Below is the message from the list archive, I was referring to.
============
Re: pthreads and errno
- Subject: Re: pthreads and errno
- From: "Scott D.W. Rankin" <email@hidden>
- Date: Mon, 21 Oct 2002 07:59:35 -0700
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?
Scott
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...
matt.
_______________________________________________
unix-porting mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/unix-porting
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