Subject: Re: libpthread and static constructor order
To: None <tech-userlevel@netbsd.org>
From: Christos Zoulas <christos@astron.com>
List: tech-userlevel
Date: 11/02/2007 19:48:51
In article <877il1481a.fsf@orac.acorntoolworks.com>,
J.T. Conklin <jtc@acorntoolworks.com> wrote:
>The pthread library's initialization function, pthread_init(), must be
>called before any other pthread functions are called. This is done by
>using gcc's __constructor__ function attribute. While this ensures it
>will be called before main(), it's not enough if other static ctors
>call pthread functions.
>
>I'm running into this problem now with ACE / TAO, which has static
>ctors which end up using pthread's thread specific storage. If the
>ACE / TAO ctors are executed before the libpthread ctor, bad things
>happen. If the libpthread ctor is executed before the ACE / TAO
>ctors, everything works fine. It's not clear to me what is needed to
>make the libpthread ctors run first. I've been able to tweak the link
>order by explicitly linking with -lpthread, but this is rather fragile.
>
>Is there any way to ensure libpthread's ctors will be run before all
>others? ld.elf_so doesn't appear to grok the DF_1_INITFIRST flag
>added by ld's -z initfirst option, and g++'s __init__priority__
>function attribute doesn't work across shared libraries.
We don't grok any of the DF_1_ flags I think.
http://docsun.cites.uiuc.edu/sun_docs/C/solaris_9/SUNWdev/LLM/p55.html
>Without a reliable way to ensure libpthread's initialization is run
>before all other static constructors, I think it needs to be changed
>so that pthread_init() returns if it's already been initialized, and
>something like:
>
> if (!__isthreaded) {
> pthread_init();
> }
>
>be added to the beginning of some (all?, most?) of the pthread calls.
>Are there any other options?
Well, you can add something to the dynamic linker and the csu like:
extern void pthread_init(void) __attribute__((__weak__));
...
if (pthread_init)
pthread_init();
...
but this is disgusting...
christos