This is the mail archive of the libstdc++@gcc.gnu.org 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]
Other format: [Raw text]

Re: Make generic atomicity.h use gthr.h mutexes


On Mon, Nov 11, 2002 at 01:17:25PM -0600, Loren James Rittle wrote:
> In article <20021110012452.A20951@disaster.jaj.com>,
> Phil Edwards<phil@jaj.com> writes:
> 
> >> This patch has a slight problem in that you can't assume that a gthr.h
> >> mutex may be statically init'd.  You need to handle systems (esp. in
> >> the so-called generic code) that only support dynamic init.  Once you
> >> see how complex this is to get perfectly correct, you will understand
> >> why I have not volunteered to produce this patch... ;-)
> 
> > Good point.
> >
> > Something like this?  (I haven't tried to test it on a dynamic-init system,
> > since I haven't figure out how to fake one yet.)

I faked it out enough to test that the following compiles, and that all the
symbols are going where they should, and only show up in the appropriate
places, etc, etc.  The only thing I can't test is actual runtime behavior
on such a system.

On sane^Wstatic-init SMP athlon, modified to use the generic file, this
causes no regressions.


> Unfortunately, no.  Unless you can logically guarantee that
> __exchange_and_add will be called at least once before multi-threading
> starts, this code is fragile.  The only robust solutions for systems
> that provide no static mutex is to place the code somewhere that must
> run before threading starts

We don't have any of those guaranteed, AFAIK...

> OR wrap it in a __gthread_once_t construct.

...but we gots plenty of those.  Now, this also involves a static init of a
variable, but gthr.h doesn't leave us any choice.  I think all the models
of gthr* should be okay with this use of __GTHREAD_ONCE_INIT, however.
This is the documented way of using *_once_t-type varaibles.

What do you think?


Index: config/cpu/generic/atomicity.h
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/config/cpu/generic/atomicity.h,v
retrieving revision 1.4
diff -u -r1.4 atomicity.h
--- config/cpu/generic/atomicity.h	8 Nov 2002 19:24:41 -0000	1.4
+++ config/cpu/generic/atomicity.h	26 Nov 2002 18:36:59 -0000
@@ -37,19 +37,34 @@
 namespace __gnu_cxx
 {
   __gthread_mutex_t _Atomic_add_mutex __attribute__ ((__weak__))
-                                                      = __GTHREAD_MUTEX_INIT;
+#ifdef __GTHREAD_MUTEX_INIT
+                                                      = __GTHREAD_MUTEX_INIT
+#endif
+                                                      ;
+
+#ifndef __GTHREAD_MUTEX_INIT
+  #define __GTHREAD_NEED_MUTEX_ONCE_FUNCTION
+  __gthread_once_t _Atomic_add_mutex_once __attribute__ ((__weak__))
+                                                      = __GTHREAD_ONCE_INIT;
+  static void __gthread_atomic_add_mutex_once();
+#endif
 }
 
 static inline _Atomic_word
 __attribute__ ((__unused__))
 __exchange_and_add (volatile _Atomic_word* __mem, int __val)
 {
-   _Atomic_word __result;
+#ifndef __GTHREAD_MUTEX_INIT
+  __gthread_once (&__gnu_cxx::_Atomic_add_mutex_once,
+                  __gnu_cxx::__gthread_atomic_add_mutex_once);
+#endif
+
+  _Atomic_word __result;
 
-   __gthread_mutex_lock (&__gnu_cxx::_Atomic_add_mutex);
+  __gthread_mutex_lock (&__gnu_cxx::_Atomic_add_mutex);
 
-   __result = *__mem;
-   *__mem += __val;
+  __result = *__mem;
+  *__mem += __val;
 
   __gthread_mutex_unlock (&__gnu_cxx::_Atomic_add_mutex);
   return __result;
Index: src/misc-inst.cc
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/src/misc-inst.cc,v
retrieving revision 1.20
diff -u -r1.20 misc-inst.cc
--- src/misc-inst.cc	5 Nov 2002 19:17:37 -0000	1.20
+++ src/misc-inst.cc	26 Nov 2002 18:36:05 -0000
@@ -87,3 +87,14 @@
      string*, __false_type);
 #endif
 } // namespace std
+
+namespace __gnu_cxx
+{
+  #ifdef __GTHREAD_NEED_MUTEX_ONCE_FUNCTION
+  // generic atomicity.h without static initialization
+  static void __gthread_atomic_add_mutex_once()
+  {
+    __GTHREAD_MUTEX_INIT_FUNCTION (_Atomic_add_mutex);
+  }
+  #endif
+} // namespace __gnu_cxx


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