A libstdc++ patch for glibc 2

H.J. Lu hjl@lucon.org
Thu Jan 15 16:30:00 GMT 1998


There is a problem with libstdc++ and glibc 2. libstdc++
is compiled with -D_PTHREADS on glibc 2, which is needed
by STL to enable pthread support. But that means C++
codes using libstdc++ may have to be compiled with
-D_PTHREADS also. Otherwise, you will get a linker error.

For glibc 2, those pthread functions used by STL are
defined in libc as weak symbols so that pthread functions
can be enabled by adding -D_REENTRANT and -lpthread. At
the same time, single thread application will also link
and run against the same libc. Calling those pthread functions
in a single thread application will work correctly with
glibc 2. Checking _PTHREADS in STL is not necessary to
build single thread application with glibc 2.

This patch changes _PTHREADS to __STL_PTHREADS_ENABLED
which is set by _PTHREADS or __GLIBC__ >= 2. It seems
to work for libc 5 and glibc 2.

I will send another patch to remove _PTHREADS for
Linux/glibc 2.

Thanks.


-- 
H.J. Lu (hjl@gnu.org)
--
Wed Jan 14 16:15:05 1998  H.J. Lu  (hjl@gnu.org)

	* ropeimpl.h: Include <stl_config.h>.
	* stl_alloc.h: Ditto.
	* stl_rope.h: Ditto.

	* ropeimpl.h (__STL_PTHREADS_ENABLED): Check
	__STL_PTHREADS_ENABLED instead of _PTHREADS.
	* stl_alloc.h: Ditto.
	* stl_config.h: Ditto.
	* stl_rope.h: Ditto.

	* stl_config.h: include <_G_config.h> if __GNUG__ is defined.
	(__STL_PTHREADS_ENABLED): Defined if _PTHREADS is defined or
	__GLIBC__ >= 2.

Index: ropeimpl.h
===================================================================
RCS file: /home/work/cvs/gnu/egcs/libstdc++/stl/ropeimpl.h,v
retrieving revision 1.1.1.3
diff -u -r1.1.1.3 ropeimpl.h
--- ropeimpl.h	1997/11/06 16:57:29	1.1.1.3
+++ ropeimpl.h	1998/01/14 23:59:43
@@ -17,6 +17,7 @@
 
 # include <stdio.h>
 # include <iostream.h>
+# include <stl_config.h>
 
 __STL_BEGIN_NAMESPACE
 
@@ -1429,7 +1430,7 @@
 
 template<class charT, class Alloc> charT rope<charT,Alloc>::empty_c_str[1];
 
-# ifdef _PTHREADS
+# ifdef __STL_PTHREADS_ENABLED
     template<class charT, class Alloc>
     pthread_mutex_t rope<charT,Alloc>::swap_lock = PTHREAD_MUTEX_INITIALIZER;
 # endif
Index: stl_alloc.h
===================================================================
RCS file: /home/work/cvs/gnu/egcs/libstdc++/stl/stl_alloc.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 stl_alloc.h
--- stl_alloc.h	1997/11/06 16:57:30	1.1.1.1
+++ stl_alloc.h	1998/01/15 00:00:08
@@ -57,16 +57,17 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+#include <stl_config.h>
 #ifndef __RESTRICT
 #  define __RESTRICT
 #endif
 
-#if !defined(_PTHREADS) && !defined(_NOTHREADS) \
+#if !defined(__STL_PTHREADS_ENABLED) && !defined(_NOTHREADS) \
  && !defined(__STL_SGI_THREADS) && !defined(__STL_WIN32THREADS)
 #   define _NOTHREADS
 #endif
 
-# ifdef _PTHREADS
+# ifdef __STL_PTHREADS_ENABLED
     // POSIX Threads
     // This is dubious, since this is likely to be a high contention
     // lock.   Performance may not be adequate.
@@ -357,7 +358,7 @@
     static inline void __unlock(volatile unsigned long *);
 # endif
 
-# ifdef _PTHREADS
+# ifdef __STL_PTHREADS_ENABLED
     static pthread_mutex_t __node_allocator_lock;
 # endif
 
@@ -558,7 +559,7 @@
     return(result);
 }
 
-#ifdef _PTHREADS
+#ifdef __STL_PTHREADS_ENABLED
     template <bool threads, int inst>
     pthread_mutex_t
     __default_alloc_template<threads, inst>::__node_allocator_lock
Index: stl_config.h
===================================================================
RCS file: /home/work/cvs/gnu/egcs/libstdc++/stl/stl_config.h,v
retrieving revision 1.1.1.3
diff -u -r1.1.1.3 stl_config.h
--- stl_config.h	1997/11/06 16:57:28	1.1.1.3
+++ stl_config.h	1998/01/15 00:00:38
@@ -71,6 +71,15 @@
 //  (19) Defines __stl_assert either as a test or as a null macro,
 //       depending on whether or not __STL_ASSERTIONS is defined.
 
+#ifdef __GNUG__
+#include <_G_config.h>
+#endif
+
+#if !defined(__STL_PTHREADS_ENABLED) && (defined(_PTHREADS) \
+ || (defined(__GLIBC__) && __GLIBC__ >= 2))
+#   define __STL_PTHREADS_ENABLED
+#endif
+
 # if defined(__sgi) && !defined(__GNUC__)
 #   if !defined(_BOOL)
 #     define __STL_NEED_BOOL
@@ -93,7 +102,7 @@
 #   if (_COMPILER_VERSION >= 721) && defined(_NAMESPACES)
 #     define __STL_USE_NAMESPACES
 #   endif 
-#   if !defined(_NOTHREADS) && !defined(_PTHREADS)
+#   if !defined(_NOTHREADS) && !defined(__STL_PTHREADS_ENABLED)
 #     define __STL_SGI_THREADS
 #   endif
 # endif
Index: stl_rope.h
===================================================================
RCS file: /home/work/cvs/gnu/egcs/libstdc++/stl/stl_rope.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 stl_rope.h
--- stl_rope.h	1997/11/06 16:57:31	1.1.1.1
+++ stl_rope.h	1998/01/15 00:01:48
@@ -18,6 +18,8 @@
 #ifndef __SGI_STL_INTERNAL_ROPE_H
 # define __SGI_STL_INTERNAL_ROPE_H
 
+# include <stl_config.h>
+
 # ifdef __GC
 #   define __GC_CONST const
 # else
@@ -306,7 +308,7 @@
             {
                 return InterlockedDecrement(&refcount);
             }
-#	elif defined(_PTHREADS)
+#	elif defined(__STL_PTHREADS_ENABLED)
 	    // This should be portable, but performance is expected
 	    // to be quite awful.  This really needs platform specific
 	    // code.
@@ -939,7 +941,7 @@
 	    static cstrptr atomic_swap(cstrptr *p, cstrptr q) {
 		return (cstrptr) InterlockedExchange((LPLONG)p, (LONG)q);
 	    }
-#	elif defined(_PTHREADS)
+#	elif defined(__STL_PTHREADS_ENABLED)
 	    // This should be portable, but performance is expected
 	    // to be quite awful.  This really needs platform specific
 	    // code.



More information about the Gcc mailing list