Re: nss_ldap broken

From: Daniel Eischen <eischen_at_vigrid.com>
Date: Mon, 29 Mar 2004 22:31:56 -0500 (EST)
On Mon, 29 Mar 2004, Jacques A. Vidrine wrote:

> On Fri, Mar 26, 2004 at 05:51:02PM -0500, Daniel Eischen wrote:
> > I think I made a comment about how you should always
> > prefix _pthread_foo() calls with 'if (__isthreaded)'.
> 
> Yes, I'm sure you did.  My recollection was that it was an
> optimization only, but it seems either I misunderstood or my
> recollection is poor (or both) :-)

I had no idea that libpthread would be loaded then unloaded.

> > When the thread libraries are initialized, then overrwrite
> > the function pointers in libc's thread jumptable.  If you
> > unload the library, libc still retains those pointers.
> 
> OK, so we guard calls to threading routines with __isthreaded.  (Patch
> below.)  Uglifies things a bit, but I can deal.  Maybe some day we'll
> rewrite reentrant.h so that it doesn't lose the return code (they
> should all be like mutex_trylock?).
> 
> 
> So, if I understand correctly:
> 
>  (1)  __isthreaded starts out 0
>  (2)  When a threading library is loaded (by any cause? DT_NEEDED?
>       dlopen RTLD_GLOBAL? dlopen RTLD_LOCAL?), __isthreaded is set
>       to 1

No, __isthreaded is only set to 1 (non-zero) when the first
thread (other than main) is created.  But the library is
auto-initialized and that's when libc's jump table is
filled.

>  (3)  When a threading library is unloaded, __isthreaded is reset to 0

No, once threaded always threaded.  There's really no going
back.

> Only, I don't immediately see where (3) happens...
> 
> Sean, could you report how this patch works for you?  Hmm, actually, it
> looks almost identical to what you posted :-)  Is there a reason that
> you stored the value of `__isthreaded' in a local variable?  Did that
> make a difference for your case?

I'm unsure how nss_ldap was built to depend on libpthread (or
any threads library).  I built it from ports and 'ldd' didn't
report any dependency on a threads library.

An example of how to build a library that is thread-safe
but doesn't bring in the threads library is libgcc.  It uses
weak references (not definitions, like you see in our libc
and libpthread) to the necessary locking functions and
pthread_create.  For instance:

	#pragma weak	pthread_create
	#pragma weak	pthread_mutex_lock
	#pragma weak	pthread_mutex_unlock
	...

	static void
	foo(void)
	{
		...
		if (pthread_create != NULL) {
			pthread_mutex_lock(&foo_lock);
			...
			pthread_mutex_unlock(&foo_lock);
		}
		...
	}

By making the pthread_* references weak, you don't need to be linked
to the threads library; they will be NULL if it is not present.
But if an application is linked to the threads library, then
those references won't be NULL.  There may be a little more
fu to it, but that's the general idea.

-- 
Dan Eischen
Received on Mon Mar 29 2004 - 17:31:58 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:49 UTC