summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog20
-rw-r--r--bits/libc-lock.h4
-rw-r--r--elf/dl-close.c6
-rw-r--r--elf/dl-iteratephdr.c4
-rw-r--r--elf/dl-lookup.c4
-rw-r--r--elf/dl-misc.c4
-rw-r--r--elf/dl-open.c4
-rw-r--r--elf/dl-support.c2
-rw-r--r--elf/rtld.c2
-rw-r--r--linuxthreads/ChangeLog4
-rw-r--r--linuxthreads/sysdeps/pthread/bits/libc-lock.h19
-rw-r--r--nptl/ChangeLog19
-rw-r--r--nptl/Makefile5
-rw-r--r--nptl/flockfile.c3
-rw-r--r--nptl/funlockfile.c3
-rw-r--r--nptl/sysdeps/pthread/bits/libc-lock.h137
-rw-r--r--nptl/sysdeps/unix/sysv/linux/Makefile4
-rw-r--r--nptl/sysdeps/unix/sysv/linux/fork.c4
-rw-r--r--sysdeps/generic/bits/libc-lock.h4
-rw-r--r--sysdeps/generic/ldsodefs.h2
-rw-r--r--sysdeps/mach/bits/libc-lock.h9
-rw-r--r--sysdeps/mach/hurd/bits/libc-lock.h20
22 files changed, 236 insertions, 47 deletions
diff --git a/ChangeLog b/ChangeLog
index 0bf1edabf9..4bc3f95fb5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2002-12-06  Ulrich Drepper  <drepper@redhat.com>
+
+	* misc/syslog.c (log_cleanup): New function.
+	(openlog): Use log_cleanup instead of __libc_mutex_unlock.
+	(closelog): Likewise.
+
+	* elf/dl-close.c: Use __rtld_lock_* macros instead of __libc_lock_*.
+	* elf/dl-iteratephdr.c: Likewise.
+	* elf/dl-lookup.c: Likewise.
+	* elf/dl-misc.c: Likewise.
+	* elf/dl-open.c: Likewise.
+	* elf/dl-support.c: Likewise.
+	* elf/rtld.c: Likewise.
+	* sysdeps/generic/ldsodefs.h: Likewise.
+	* sysdeps/generic/bits/libc-lock.h: Define __rtld_lock_* macros.
+	* sysdeps/mach/bits/libc-lock.h: Likewise.
+	* sysdeps/mach/hurd/bits/libc-lock.h: Likewise.
+
 2002-12-06  Roland McGrath  <roland@redhat.com>
 
 	* sysdeps/generic/libc-tls.c (__libc_setup_tls): Cope with zero ALIGN.
@@ -56,7 +74,7 @@
 
 2002-12-05  Ulrich Drepper  <drepper@redhat.com>
 
-	* dirent/bug-readdir1.c (main): Don't call closedir, just close on
+	* dirent/bug-readdir1.c (main): Don't call closedir, just close
 	the file descriptor.  This is testing what the bug report was about.
 
 2002-12-04  Ulrich Drepper  <drepper@redhat.com>
diff --git a/bits/libc-lock.h b/bits/libc-lock.h
index ff808904a8..c2bb494940 100644
--- a/bits/libc-lock.h
+++ b/bits/libc-lock.h
@@ -30,6 +30,7 @@
    of libc.  */
 #define __libc_lock_define(CLASS,NAME)
 #define __libc_lock_define_recursive(CLASS,NAME)
+#define __rtld_lock_define_recursive(CLASS,NAME)
 #define __libc_rwlock_define(CLASS,NAME)
 
 /* Define an initialized lock variable NAME with storage class CLASS.  */
@@ -47,6 +48,7 @@
 
 /* Same as last but this time we initialize a recursive mutex.  */
 #define __libc_lock_init_recursive(NAME)
+#define __rtld_lock_init_recursive(NAME)
 
 /* Finalize the named lock variable, which must be locked.  It cannot be
    used again until __libc_lock_init is called again on it.  This must be
@@ -64,6 +66,7 @@
 
 /* Lock the recursive named lock variable.  */
 #define __libc_lock_lock_recursive(NAME)
+#define __rtld_lock_lock_recursive(NAME)
 
 /* Try to lock the named lock variable.  */
 #define __libc_lock_trylock(NAME) 0
@@ -79,6 +82,7 @@
 
 /* Unlock the recursive named lock variable.  */
 #define __libc_lock_unlock_recursive(NAME)
+#define __rtld_lock_unlock_recursive(NAME)
 
 
 /* Define once control variable.  */
diff --git a/elf/dl-close.c b/elf/dl-close.c
index 0a6265f142..84c10cbf3a 100644
--- a/elf/dl-close.c
+++ b/elf/dl-close.c
@@ -126,7 +126,7 @@ _dl_close (void *_map)
     _dl_signal_error (0, map->l_name, NULL, N_("shared object not open"));
 
   /* Acquire the lock.  */
-  __libc_lock_lock_recursive (GL(dl_load_lock));
+  __rtld_lock_lock_recursive (GL(dl_load_lock));
 
   /* Decrement the reference count.  */
   if (map->l_opencount > 1 || map->l_type != lt_loaded)
@@ -139,7 +139,7 @@ _dl_close (void *_map)
       /* One decrement the object itself, not the dependencies.  */
       --map->l_opencount;
 
-      __libc_lock_unlock_recursive (GL(dl_load_lock));
+      __rtld_lock_unlock_recursive (GL(dl_load_lock));
       return;
     }
 
@@ -422,7 +422,7 @@ _dl_close (void *_map)
   free (list);
 
   /* Release the lock.  */
-  __libc_lock_unlock_recursive (GL(dl_load_lock));
+  __rtld_lock_unlock_recursive (GL(dl_load_lock));
 }
 libc_hidden_def (_dl_close)
 
diff --git a/elf/dl-iteratephdr.c b/elf/dl-iteratephdr.c
index b80936870a..c374baaa98 100644
--- a/elf/dl-iteratephdr.c
+++ b/elf/dl-iteratephdr.c
@@ -32,7 +32,7 @@ __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
   int ret = 0;
 
   /* Make sure we are alone.  */
-  __libc_lock_lock_recursive (GL(dl_load_lock));
+  __rtld_lock_lock_recursive (GL(dl_load_lock));
 
   for (l = GL(dl_loaded); l != NULL; l = l->l_next)
     {
@@ -49,7 +49,7 @@ __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
     }
 
   /* Release the lock.  */
-  __libc_lock_unlock_recursive (GL(dl_load_lock));
+  __rtld_lock_unlock_recursive (GL(dl_load_lock));
 
   return ret;
 }
diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
index 1c3d9b5ad5..39b3a3d013 100644
--- a/elf/dl-lookup.c
+++ b/elf/dl-lookup.c
@@ -100,7 +100,7 @@ add_dependency (struct link_map *undef_map, struct link_map *map)
     return 0;
 
   /* Make sure nobody can unload the object while we are at it.  */
-  __libc_lock_lock_recursive (GL(dl_load_lock));
+  __rtld_lock_lock_recursive (GL(dl_load_lock));
 
   /* Determine whether UNDEF_MAP already has a reference to MAP.  First
      look in the normal dependencies.  */
@@ -186,7 +186,7 @@ add_dependency (struct link_map *undef_map, struct link_map *map)
 
  out:
   /* Release the lock.  */
-  __libc_lock_unlock_recursive (GL(dl_load_lock));
+  __rtld_lock_unlock_recursive (GL(dl_load_lock));
 
   return result;
 }
diff --git a/elf/dl-misc.c b/elf/dl-misc.c
index 0fbfcde004..979de9b7ac 100644
--- a/elf/dl-misc.c
+++ b/elf/dl-misc.c
@@ -258,9 +258,9 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
     __writev (fd, iov, niov);
   else
     {
-      __libc_lock_lock_recursive (GL(dl_load_lock));
+      __rtld_lock_lock_recursive (GL(dl_load_lock));
       __writev (fd, iov, niov);
-      __libc_lock_unlock_recursive (GL(dl_load_lock));
+      __rtld_lock_unlock_recursive (GL(dl_load_lock));
     }
 #else
   __writev (fd, iov, niov);
diff --git a/elf/dl-open.c b/elf/dl-open.c
index 7dd18b7d97..65102fabce 100644
--- a/elf/dl-open.c
+++ b/elf/dl-open.c
@@ -486,7 +486,7 @@ _dl_open (const char *file, int mode, const void *caller)
     _dl_signal_error (EINVAL, file, NULL, N_("invalid mode for dlopen()"));
 
   /* Make sure we are alone.  */
-  __libc_lock_lock_recursive (GL(dl_load_lock));
+  __rtld_lock_lock_recursive (GL(dl_load_lock));
 
   args.file = file;
   args.mode = mode;
@@ -500,7 +500,7 @@ _dl_open (const char *file, int mode, const void *caller)
 #endif
 
   /* Release the lock.  */
-  __libc_lock_unlock_recursive (GL(dl_load_lock));
+  __rtld_lock_unlock_recursive (GL(dl_load_lock));
 
   if (__builtin_expect (errstring != NULL, 0))
     {
diff --git a/elf/dl-support.c b/elf/dl-support.c
index b4b185fd97..7d56237e1d 100644
--- a/elf/dl-support.c
+++ b/elf/dl-support.c
@@ -138,7 +138,7 @@ size_t _dl_phnum;
    This must be a recursive lock since the initializer function of
    the loaded object might as well require a call to this function.
    At this time it is not anymore a problem to modify the tables.  */
-__libc_lock_define_initialized_recursive (, _dl_load_lock)
+__rtld_lock_define_initialized_recursive (, _dl_load_lock)
 
 
 #ifdef HAVE_AUX_VECTOR
diff --git a/elf/rtld.c b/elf/rtld.c
index e43a0e998d..a93be8447e 100644
--- a/elf/rtld.c
+++ b/elf/rtld.c
@@ -102,7 +102,7 @@ struct rtld_global _rtld_global =
     ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
     ._dl_hwcap_mask = HWCAP_IMPORTANT,
 #ifdef _LIBC_REENTRANT
-    ._dl_load_lock = _LIBC_LOCK_RECURSIVE_INITIALIZER
+    ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER
 #endif
   };
 strong_alias (_rtld_global, _rtld_local);
diff --git a/linuxthreads/ChangeLog b/linuxthreads/ChangeLog
index 1fa6aea5c7..10e075adfe 100644
--- a/linuxthreads/ChangeLog
+++ b/linuxthreads/ChangeLog
@@ -1,3 +1,7 @@
+2002-12-06  Ulrich Drepper  <drepper@redhat.com>
+
+	* sysdeps/pthread/bits/libc-lock.h: Define __rtld_lock_* macros.
+
 2002-12-05  Roland McGrath  <roland@redhat.com>
 
 	* pthread.c (__pthread_initialize_minimal)
diff --git a/linuxthreads/sysdeps/pthread/bits/libc-lock.h b/linuxthreads/sysdeps/pthread/bits/libc-lock.h
index c25c0385e9..ceb08b6238 100644
--- a/linuxthreads/sysdeps/pthread/bits/libc-lock.h
+++ b/linuxthreads/sysdeps/pthread/bits/libc-lock.h
@@ -32,6 +32,7 @@ typedef pthread_rwlock_t __libc_rwlock_t;
 # else
 typedef struct __libc_rwlock_opaque__ __libc_rwlock_t;
 # endif
+typedef __libc_lock_recursive_t __rtld_lock_recursive_t;
 #else
 typedef struct __libc_lock_opaque__ __libc_lock_t;
 typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
@@ -54,6 +55,8 @@ typedef pthread_key_t __libc_key_t;
   CLASS __libc_rwlock_t NAME;
 #define __libc_lock_define_recursive(CLASS,NAME) \
   CLASS __libc_lock_recursive_t NAME;
+#define __rtld_lock_define_recursive(CLASS,NAME) \
+  CLASS __rtld_lock_recursive_t NAME;
 
 /* Define an initialized lock variable NAME with storage class CLASS.
 
@@ -82,12 +85,17 @@ typedef pthread_key_t __libc_key_t;
 #define _LIBC_LOCK_RECURSIVE_INITIALIZER \
   {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
 
+#define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
+  CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
+#define _RTLD_LOCK_RECURSIVE_INITIALIZER \
+  {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
+
 #ifdef __PIC__
-#define __libc_maybe_call(FUNC, ARGS, ELSE) \
+# define __libc_maybe_call(FUNC, ARGS, ELSE) \
   (__extension__ ({ __typeof (FUNC) *_fn = (FUNC); \
                     _fn != NULL ? (*_fn) ARGS : ELSE; }))
 #else
-#define __libc_maybe_call(FUNC, ARGS, ELSE) \
+# define __libc_maybe_call(FUNC, ARGS, ELSE) \
   (FUNC != NULL ? FUNC ARGS : ELSE)
 #endif
 
@@ -110,6 +118,8 @@ typedef pthread_key_t __libc_key_t;
 	__pthread_mutexattr_destroy (&__attr);				      \
       }									      \
   } while (0);
+#define __rtld_lock_init_recursive(NAME) \
+  __libc_lock_init_recursive (NAME)
 
 /* Finalize the named lock variable, which must be locked.  It cannot be
    used again until __libc_lock_init is called again on it.  This must be
@@ -121,6 +131,7 @@ typedef pthread_key_t __libc_key_t;
 
 /* Finalize recursive named lock.  */
 #define __libc_lock_fini_recursive(NAME) __libc_lock_fini ((NAME).mutex)
+#define __rtld_lock_fini_recursive(NAME) __libc_lock_fini_recursive (NAME)
 
 /* Lock the named lock variable.  */
 #define __libc_lock_lock(NAME) \
@@ -132,6 +143,7 @@ typedef pthread_key_t __libc_key_t;
 
 /* Lock the recursive named lock variable.  */
 #define __libc_lock_lock_recursive(NAME) __libc_lock_lock ((NAME).mutex)
+#define __rtld_lock_lock_recursive(NAME) __libc_lock_lock_recursive (NAME)
 
 /* Try to lock the named lock variable.  */
 #define __libc_lock_trylock(NAME) \
@@ -143,6 +155,8 @@ typedef pthread_key_t __libc_key_t;
 
 /* Try to lock the recursive named lock variable.  */
 #define __libc_lock_trylock_recursive(NAME) __libc_lock_trylock ((NAME).mutex)
+#define __rtld_lock_trylock_recursive(NAME) \
+  __libc_lock_trylock_recursive (NAME)
 
 /* Unlock the named lock variable.  */
 #define __libc_lock_unlock(NAME) \
@@ -152,6 +166,7 @@ typedef pthread_key_t __libc_key_t;
 
 /* Unlock the recursive named lock variable.  */
 #define __libc_lock_unlock_recursive(NAME) __libc_lock_unlock ((NAME).mutex)
+#define __rtld_lock_unlock_recursive(NAME) __libc_lock_unlock_recursive (NAME)
 
 
 /* Define once control variable.  */
diff --git a/nptl/ChangeLog b/nptl/ChangeLog
index 9d40cca747..b8a193ea09 100644
--- a/nptl/ChangeLog
+++ b/nptl/ChangeLog
@@ -1,3 +1,22 @@
+2002-12-06  Ulrich Drepper  <drepper@redhat.com>
+
+	* Makefile (tests): Comment out tst-locale2 for now.
+	(CFLAGS-flockfile.c, CFLAGS-funlockfile.c): Define to -D_IO_MTSAFE_IO.
+
+	* sysdeps/unix/sysv/linux/Makefile: Define CFLAGS-fork.c to
+	-D_IO_MTSAFE_IO.
+	* sysdeps/unix/sysv/linux/fork.c: Include <bits/stdio-lock.h>.
+	Use _IO_lock_init instead of explicit assignment.
+
+	* sysdeps/pthread/bits/libc-lock.h: Define __rtld_lock_* macros.
+	Define __libc_lock_* and __libc_lock_recursive macros with
+	lowlevellock macros, not pthread mutexes.
+
+	* flockfile.c: Include <bits/stdio-lock.h>.  Use _IO_lock_lock instead
+	of pthread_mutex_lock.
+	* funlockfile.c: Include <bits/stdio-lock.h>.  Use _IO_lock_unlock
+	instead of pthread_mutex_unlock.
+
 2002-12-06  Roland McGrath  <roland@redhat.com>
 
 	* allocatestack.c (__stack_user): Use uninitialized defn.
diff --git a/nptl/Makefile b/nptl/Makefile
index a856b2df9d..7683ab87db 100644
--- a/nptl/Makefile
+++ b/nptl/Makefile
@@ -134,7 +134,7 @@ tests = tst-mutex1 tst-mutex2 tst-mutex3 tst-mutex4 tst-mutex5 tst-mutex6 \
 	tst-stack1 \
 	tst-unload \
 	tst-sysconf \
-	tst-locale1 tst-locale2
+	tst-locale1 #tst-locale2  <--- commented out until compiler is fixed
 
 LDFLAGS-pthread.so = -Wl,--enable-new-dtags,-z,nodelete,-z,initfirst
 
@@ -155,6 +155,9 @@ omit-deps += crti
 CFLAGS-pt-initfini.s = -g0 -fPIC -fno-inline-functions
 endif
 
+CFLAGS-flockfile.c = -D_IO_MTSAFE_IO
+CFLAGS-funlockfile.c = -D_IO_MTSAFE_IO
+
 ifeq ($(build-static),yes)
 tests-static += tst-locale1 tst-locale2
 endif
diff --git a/nptl/flockfile.c b/nptl/flockfile.c
index a82059f2f1..db6776813c 100644
--- a/nptl/flockfile.c
+++ b/nptl/flockfile.c
@@ -20,12 +20,13 @@
 #include <pthread.h>
 #include <stdio.h>
 #include <libio.h>
+#include <bits/stdio-lock.h>
 
 
 void
 flockfile (stream)
      FILE *stream;
 {
-  pthread_mutex_lock (stream->_lock);
+  _IO_lock_lock (*stream->_lock);
 }
 strong_alias (flockfile, _IO_flockfile)
diff --git a/nptl/funlockfile.c b/nptl/funlockfile.c
index cbb3ec690c..f0314b2fc6 100644
--- a/nptl/funlockfile.c
+++ b/nptl/funlockfile.c
@@ -20,12 +20,13 @@
 #include <pthread.h>
 #include <stdio.h>
 #include <libio.h>
+#include <bits/stdio-lock.h>
 
 
 void
 funlockfile (stream)
      FILE *stream;
 {
-  pthread_mutex_unlock (stream->_lock);
+  _IO_lock_unlock (*stream->_lock);
 }
 strong_alias (funlockfile, _IO_funlockfile)
diff --git a/nptl/sysdeps/pthread/bits/libc-lock.h b/nptl/sysdeps/pthread/bits/libc-lock.h
index 9536d0c909..30e3410cbc 100644
--- a/nptl/sysdeps/pthread/bits/libc-lock.h
+++ b/nptl/sysdeps/pthread/bits/libc-lock.h
@@ -33,16 +33,19 @@
    ld.so might be used on old kernels with a different libc.so.  */
 #ifdef _LIBC
 # include <lowlevellock.h>
+# include <tls.h>
 #endif
 
 /* Mutex type.  */
 #if defined _LIBC || defined _IO_MTSAFE_IO
-# if defined NOT_IN_libc || !defined _LIBC
+# if (defined NOT_IN_libc && !defined IS_IN_libpthread) || !defined _LIBC
 typedef pthread_mutex_t __libc_lock_t;
+typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t;
 # else
 typedef int __libc_lock_t;
+typedef struct { int lock; int cnt; void *owner; } __libc_lock_recursive_t;
 # endif
-typedef struct { pthread_mutex_t mutex; } __libc_lock_recursive_t;
+typedef struct { pthread_mutex_t mutex; } __rtld_lock_recursive_t;
 # ifdef __USE_UNIX98
 typedef pthread_rwlock_t __libc_rwlock_t;
 # else
@@ -70,6 +73,8 @@ typedef pthread_key_t __libc_key_t;
   CLASS __libc_rwlock_t NAME;
 #define __libc_lock_define_recursive(CLASS,NAME) \
   CLASS __libc_lock_recursive_t NAME;
+#define __rtld_lock_define_recursive(CLASS,NAME) \
+  CLASS __rtld_lock_recursive_t NAME;
 
 /* Define an initialized lock variable NAME with storage class CLASS.
 
@@ -80,7 +85,7 @@ typedef pthread_key_t __libc_key_t;
    initialized locks must be set to one due to the lack of normal
    atomic operations.) */
 
-#if defined _LIBC && !defined NOT_IN_libc
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
 # if LLL_LOCK_INITIALIZER == 0
 #  define __libc_lock_define_initialized(CLASS,NAME) \
   CLASS __libc_lock_t NAME;
@@ -103,11 +108,27 @@ typedef pthread_key_t __libc_key_t;
 
 /* Define an initialized recursive lock variable NAME with storage
    class CLASS.  */
-#define __libc_lock_define_initialized_recursive(CLASS,NAME) \
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
+# if LLL_LOCK_INITIALIZER == 0
+#  define __libc_lock_define_initialized_recursive(CLASS,NAME) \
+  CLASS __libc_lock_recursive_t NAME;
+# else
+#  define __libc_lock_define_initialized_recursive(CLASS,NAME) \
+  CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
+# endif
+# define _LIBC_LOCK_RECURSIVE_INITIALIZER \
+  { LLL_LOCK_INITIALIZER, 0, NULL }
+#else
+# define __libc_lock_define_initialized_recursive(CLASS,NAME) \
   CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
-#define _LIBC_LOCK_RECURSIVE_INITIALIZER \
+# define _LIBC_LOCK_RECURSIVE_INITIALIZER \
   {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
+#endif
 
+#define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
+  CLASS __rtld_lock_recursive_t NAME = _RTLD_LOCK_RECURSIVE_INITIALIZER;
+#define _RTLD_LOCK_RECURSIVE_INITIALIZER \
+  {PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}
 
 /* If we check for a weakly referenced symbol and then perform a
    normal jump to it te code generated for some platforms in case of
@@ -126,7 +147,7 @@ typedef pthread_key_t __libc_key_t;
 
 /* Initialize the named lock variable, leaving it in a consistent, unlocked
    state.  */
-#if defined _LIBC && !defined NOT_IN_libc
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
 # define __libc_lock_init(NAME) (NAME) = LLL_LOCK_INITIALIZER;
 #else
 # define __libc_lock_init(NAME) \
@@ -136,23 +157,40 @@ typedef pthread_key_t __libc_key_t;
   __libc_maybe_call (__pthread_rwlock_init, (&(NAME), NULL), 0)
 
 /* Same as last but this time we initialize a recursive mutex.  */
-#define __libc_lock_init_recursive(NAME) \
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
+# define __libc_lock_init_recursive(NAME) \
+    (NAME) = (__libc_lock_recursive_t) _LIBC_LOCK_RECURSIVE_INITIALIZER
+#else
+# define __libc_lock_init_recursive(NAME) \
+  do {									      \
+    if (__pthread_mutex_init != NULL)					      \
+      {									      \
+	pthread_mutexattr_t __attr;					      \
+	__pthread_mutexattr_init (&__attr);				      \
+	__pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP);    \
+	__pthread_mutex_init (&(NAME).mutex, &__attr);			      \
+	__pthread_mutexattr_destroy (&__attr);				      \
+      }									      \
+  } while (0)
+#endif
+
+#define __rtld_lock_init_recursive(NAME) \
   do {									      \
     if (__pthread_mutex_init != NULL)					      \
       {									      \
 	pthread_mutexattr_t __attr;					      \
 	__pthread_mutexattr_init (&__attr);				      \
-	__pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP); \
+	__pthread_mutexattr_settype (&__attr, PTHREAD_MUTEX_RECURSIVE_NP);    \
 	__pthread_mutex_init (&(NAME).mutex, &__attr);			      \
 	__pthread_mutexattr_destroy (&__attr);				      \
       }									      \
-  } while (0);
+  } while (0)
 
 /* Finalize the named lock variable, which must be locked.  It cannot be
    used again until __libc_lock_init is called again on it.  This must be
    called on a lock variable before the containing storage is reused.  */
-#if defined _LIBC && !defined NOT_IN_libc
-# define __libc_lock_fini(NAME) (0)
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
+# define __libc_lock_fini(NAME) ((void) 0)
 #else
 # define __libc_lock_fini(NAME) \
   __libc_maybe_call (__pthread_mutex_destroy, (&(NAME)), 0)
@@ -161,11 +199,15 @@ typedef pthread_key_t __libc_key_t;
   __libc_maybe_call (__pthread_rwlock_destroy, (&(NAME)), 0)
 
 /* Finalize recursive named lock.  */
-#define __libc_lock_fini_recursive(NAME) \
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
+# define __libc_lock_fini_recursive(NAME) ((void) 0)
+#else
+# define __libc_lock_fini_recursive(NAME) \
   __libc_maybe_call (__pthread_mutex_destroy, (&(NAME)), 0)
+#endif
 
 /* Lock the named lock variable.  */
-#if defined _LIBC && !defined NOT_IN_libc
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
 # define __libc_lock_lock(NAME) \
   lll_lock (NAME);
 #else
@@ -178,11 +220,27 @@ typedef pthread_key_t __libc_key_t;
   __libc_maybe_call (__pthread_rwlock_wrlock, (&(NAME)), 0)
 
 /* Lock the recursive named lock variable.  */
-#define __libc_lock_lock_recursive(NAME) \
-  __libc_maybe_call (__pthread_mutex_lock, (&(NAME)), 0)
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
+# define __libc_lock_lock_recursive(NAME) \
+  do {									      \
+    void *self = THREAD_SELF;						      \
+    if ((NAME).owner != self)						      \
+      {									      \
+	lll_lock ((NAME).lock);						      \
+	(NAME).owner = self;						      \
+      }									      \
+    ++(NAME).cnt;							      \
+  } while (0)
+#else
+# define __libc_lock_lock_recursive(NAME) \
+  __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0)
+#endif
+
+#define __rtld_lock_lock_recursive(NAME) \
+  __libc_maybe_call (__pthread_mutex_lock, (&(NAME).mutex), 0)
 
 /* Try to lock the named lock variable.  */
-#if defined _LIBC && !defined NOT_IN_libc
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
 # define __libc_lock_trylock(NAME) \
   lll_trylock (NAME)
 #else
@@ -195,11 +253,34 @@ typedef pthread_key_t __libc_key_t;
   __libc_maybe_call (__pthread_rwlock_trywrlock, (&(NAME)), 0)
 
 /* Try to lock the recursive named lock variable.  */
-#define __libc_lock_trylock_recursive(NAME) \
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
+# define __libc_lock_trylock_recursive(NAME)				      \
+  do {									      \
+    int result = 0;							      \
+    void *self = THREAD_SELF;						      \
+    if ((NAME).owner != self)						      \
+      {									      \
+	if (lll_trylock ((NAME).lock) == 0)				      \
+	  {								      \
+	    (NAME).owner = self;					      \
+	    (NAME).cnt = 1;						      \
+	  }								      \
+	else								      \
+	  result = EBUSY;						      \
+      }									      \
+    else								      \
+      ++(NAME).cnt;							      \
+  } while (0)
+#else
+# define __libc_lock_trylock_recursive(NAME) \
   __libc_maybe_call (__pthread_mutex_trylock, (&(NAME)), 0)
+#endif
+
+#define __rtld_lock_trylock_recursive(NAME) \
+  __libc_maybe_call (__pthread_mutex_trylock, (&(NAME).mutex), 0)
 
 /* Unlock the named lock variable.  */
-#if defined _LIBC && !defined NOT_IN_libc
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
 # define __libc_lock_unlock(NAME) \
   lll_unlock (NAME);
 #else
@@ -210,9 +291,23 @@ typedef pthread_key_t __libc_key_t;
   __libc_maybe_call (__pthread_rwlock_unlock, (&(NAME)), 0)
 
 /* Unlock the recursive named lock variable.  */
-#define __libc_lock_unlock_recursive(NAME) \
+#if defined _LIBC && (!defined NOT_IN_libc || defined IS_IN_libpthread)
+/* We do no error checking here.  */
+# define __libc_lock_unlock_recursive(NAME) \
+  do {									      \
+    if (--(NAME).cnt == 0)						      \
+      {									      \
+	(NAME).owner = NULL;						      \
+	lll_unlock ((NAME).lock);					      \
+      }									      \
+  } while (0)
+#else
+# define __libc_lock_unlock_recursive(NAME) \
   __libc_maybe_call (__pthread_mutex_unlock, (&(NAME)), 0)
+#endif
 
+#define __rtld_lock_unlock_recursive(NAME) \
+  __libc_maybe_call (__pthread_mutex_unlock, (&(NAME).mutex), 0)
 
 /* Define once control variable.  */
 #if PTHREAD_ONCE_INIT == 0
@@ -394,8 +489,4 @@ weak_extern (BP_SYM (_pthread_cleanup_pop_restore))
 # endif
 #endif
 
-/* We need portable names for some functions.  E.g., when they are
-   used as argument to __libc_cleanup_region_start.  */
-#define __libc_mutex_unlock __pthread_mutex_unlock
-
 #endif	/* bits/libc-lock.h */
diff --git a/nptl/sysdeps/unix/sysv/linux/Makefile b/nptl/sysdeps/unix/sysv/linux/Makefile
index 832ce2326b..9a84534d5b 100644
--- a/nptl/sysdeps/unix/sysv/linux/Makefile
+++ b/nptl/sysdeps/unix/sysv/linux/Makefile
@@ -22,3 +22,7 @@ sysdep_routines += register-atfork unregister-atfork fork-gen
 
 libpthread-sysdep_routines += pt-fork
 endif
+
+ifeq ($(subdir),posix)
+CFLAGS-fork.c = -D_IO_MTSAFE_IO
+endif
diff --git a/nptl/sysdeps/unix/sysv/linux/fork.c b/nptl/sysdeps/unix/sysv/linux/fork.c
index c1aa23c5c5..b643f89dfc 100644
--- a/nptl/sysdeps/unix/sysv/linux/fork.c
+++ b/nptl/sysdeps/unix/sysv/linux/fork.c
@@ -24,6 +24,7 @@
 #include <libio/libioP.h>
 #include <tls.h>
 #include "fork.h"
+#include <bits/stdio-lock.h>
 
 
 unsigned long int *__fork_generation_pointer;
@@ -41,8 +42,7 @@ fresetlockfiles (void)
   _IO_ITER i;
 
   for (i = _IO_iter_begin(); i != _IO_iter_end(); i = _IO_iter_next(i))
-    *((pthread_mutex_t *) _IO_iter_file(i)->_lock)
-      = (pthread_mutex_t) PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+    _IO_lock_init (*((_IO_lock_t *) _IO_iter_file(i)->_lock));
 }
 
 
diff --git a/sysdeps/generic/bits/libc-lock.h b/sysdeps/generic/bits/libc-lock.h
index ff808904a8..c2bb494940 100644
--- a/sysdeps/generic/bits/libc-lock.h
+++ b/sysdeps/generic/bits/libc-lock.h
@@ -30,6 +30,7 @@
    of libc.  */
 #define __libc_lock_define(CLASS,NAME)
 #define __libc_lock_define_recursive(CLASS,NAME)
+#define __rtld_lock_define_recursive(CLASS,NAME)
 #define __libc_rwlock_define(CLASS,NAME)
 
 /* Define an initialized lock variable NAME with storage class CLASS.  */
@@ -47,6 +48,7 @@
 
 /* Same as last but this time we initialize a recursive mutex.  */
 #define __libc_lock_init_recursive(NAME)
+#define __rtld_lock_init_recursive(NAME)
 
 /* Finalize the named lock variable, which must be locked.  It cannot be
    used again until __libc_lock_init is called again on it.  This must be
@@ -64,6 +66,7 @@
 
 /* Lock the recursive named lock variable.  */
 #define __libc_lock_lock_recursive(NAME)
+#define __rtld_lock_lock_recursive(NAME)
 
 /* Try to lock the named lock variable.  */
 #define __libc_lock_trylock(NAME) 0
@@ -79,6 +82,7 @@
 
 /* Unlock the recursive named lock variable.  */
 #define __libc_lock_unlock_recursive(NAME)
+#define __rtld_lock_unlock_recursive(NAME)
 
 
 /* Define once control variable.  */
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index 5e08844ce3..a704644d6e 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -250,7 +250,7 @@ struct rtld_global
      This must be a recursive lock since the initializer function of
      the loaded object might as well require a call to this function.
      At this time it is not anymore a problem to modify the tables.  */
-  __libc_lock_define_recursive (EXTERN, _dl_load_lock)
+  __rtld_lock_define_recursive (EXTERN, _dl_load_lock)
 
   /* OS version.  */
   EXTERN unsigned int _dl_osversion;
diff --git a/sysdeps/mach/bits/libc-lock.h b/sysdeps/mach/bits/libc-lock.h
index 2484297c8a..76bbd02d0d 100644
--- a/sysdeps/mach/bits/libc-lock.h
+++ b/sysdeps/mach/bits/libc-lock.h
@@ -1,5 +1,5 @@
 /* libc-internal interface for mutex locks.  Mach cthreads version.
-   Copyright (C) 1996,97,98,2000,01 Free Software Foundation, Inc.
+   Copyright (C) 1996,97,98,2000,01, 2002 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -134,4 +134,11 @@ void *__libc_getspecific (__libc_key_t key);
 #define __libc_lock_unlock_recursive __libc_lock_unlock
 #define __libc_lock_lock_recursive __libc_lock_lock
 
+#define __rtld_lock_define_initialized_recursive __libc_lock_define_initialized
+#define __rtld_lock_init_recursive __libc_lock_init
+#define __rtld_lock_fini_recursive __libc_lock_fini
+#define __rtld_lock_trylock_recursive __libc_lock_trylock
+#define __rtld_lock_unlock_recursive __libc_lock_unlock
+#define __rtld_lock_lock_recursive __libc_lock_lock
+
 #endif	/* bits/libc-lock.h */
diff --git a/sysdeps/mach/hurd/bits/libc-lock.h b/sysdeps/mach/hurd/bits/libc-lock.h
index 6e3835371c..bc83eafc49 100644
--- a/sysdeps/mach/hurd/bits/libc-lock.h
+++ b/sysdeps/mach/hurd/bits/libc-lock.h
@@ -1,5 +1,5 @@
 /* libc-internal interface for mutex locks.  Hurd version using Mach cthreads.
-   Copyright (C) 1996,97,98,2000,01 Free Software Foundation, Inc.
+   Copyright (C) 1996,97,98,2000,01, 2002 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -62,6 +62,7 @@ typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
    called on a lock variable before the containing storage is reused.  */
 #define __libc_lock_fini(NAME) __mutex_unlock (&(NAME))
 #define __libc_lock_fini_recursive(NAME) __mutex_unlock (&(NAME).mutex)
+#define __rtld_lock_fini_recursive(NAME) __mutex_unlock (&(NAME).mutex)
 
 
 /* Lock the named lock variable.  */
@@ -80,6 +81,13 @@ typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
 #define __libc_lock_define_initialized_recursive(CLASS,NAME) \
   CLASS __libc_lock_recursive_t NAME = _LIBC_LOCK_RECURSIVE_INITIALIZER;
 
+#define __rtld_lock_define_recursive(CLASS,NAME) \
+  __libc_lock_define_recursive (CLASS, NAME)
+#define _RTLD_LOCK_RECURSIVE_INITIALIZER \
+  _LIBC_LOCK_RECURSIVE_INITIALIZER
+#define __rtld_lock_define_initialized_recursive(CLASS,NAME) \
+  __libc_lock_define_initialized_recursive (CLASS, NAME)
+
 #define __libc_lock_init_recursive(NAME) \
   ({ __libc_lock_recursive_t *const __lock = &(NAME); \
      __lock->owner = 0; mutex_init (&__lock->mutex); })
@@ -111,6 +119,16 @@ typedef struct __libc_lock_recursive_opaque__ __libc_lock_recursive_t;
   })
 
 
+#define __rtld_lock_init_recursive(NAME) \
+  __libc_lock_init_recursive (NAME)
+#define __rtld_lock_trylock_recursive(NAME) \
+  __libc_lock_trylock_recursive (NAME)
+#define __rtld_lock_lock_recursive(NAME) \
+  __libc_lock_lock_recursive(NAME)
+#define __rtld_lock_unlock_recursive(NAME) \
+  __libc_lock_unlock_recursive (NAME)
+
+
 /* XXX for now */
 #define __libc_rwlock_define		__libc_lock_define
 #define __libc_rwlock_define_initialized __libc_lock_define_initialized