This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

MT-safe string, part 1



Attached below are diffs for the essentials of MT-safe basic_string<>.

What is missing is the #include and the configure script apparatus to 
choose the right include file to declare (and define) the primitives
used.  (The corresponding apparatus in Glibc refers to 
sysdeps/*/atomicity.h.  For some reason the x86 (x >= 4) version of 
that file, as posted here earlier, is not there.)

Note that for most uses on Linux, just substituting different header 
includes is _not_ sufficient.  Most distributions are built for a "386" 
target, which lacks the appropriate instructions.  (SPARC-1 and HPPA, 
I gather, also lack appropriate instructions, so MT on them will be 
miserable.)  Linking an alternatively-compiled version of the library, 
a common adaptation, wouldn't be enough, because these are inline 
operations which affect non-library object files.

Probably the default build for x86 must declare non-inline functions, 
which refer to one of a set of shared libraries (or section of a single 
library) chosen at runtime via some abominable ldso trickery.  User 
programs built with more knowledge about the target processor (e.g. a 
486 or better) should be arranged (preferably automatically) to #include 
instead something that defines the functions with appropriate assembly 
code, inline, and (thus) at runtime just ignore the shared library 
version.  (The generic version still be called by any object module 
built with the generic header, so the semantics of both implementations
must be compatible.)

If someone can point me at the right place to hack the apparatus in,
and/or an example of similar things done, I can do more of the job.

Nathan Myers
ncm@cantrip.org

--- basic_string.h.orig	Wed Mar 15 01:33:51 2000
+++ basic_string.h	Wed Mar 15 01:59:26 2000
@@ -137,9 +137,10 @@
 	// In addition, this implementation quarters this ammount.
 	static size_type 	_S_max_size;
 	static _CharT 		_S_terminal;
+
 	size_type 		_M_length;
 	size_type 		_M_capacity;
-	int 			_M_state;
+	uint32_t		_M_state;
 	
 	_CharT* 
 	_M_refdata() throw()
@@ -161,7 +162,7 @@
 	void 
 	_M_dispose(const _Alloc& __a)
 	{ 
-	  if (_M_state-- <= 0)  
+	  if (exchange_and_add(&_M_state, -1) <= 0)  
 	    _M_destroy(__a); 
 	}  // XXX MT
 
@@ -171,7 +172,7 @@
 	_CharT* 
 	_M_refcopy() throw()
 	{ 
-	  ++_M_state; 
+	  atomic_add(&_M_state, 1); 
 	  return _M_refdata(); 
 	}  // XXX MT
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]