public inbox for ecos-devel@sourceware.org
 help / color / mirror / Atom feed
* pthread_once non conformant
@ 2009-11-01 20:52 Jerome Souquieres
  2009-11-10  4:06 ` Jonathan Larmour
  0 siblings, 1 reply; 3+ messages in thread
From: Jerome Souquieres @ 2009-11-01 20:52 UTC (permalink / raw)
  To: ecos-devel

Hi,

I think I've spotted a bug in pthread_once (in ecos v3.0). The current
implementation does not enforce the part of the specification that
says: "On return from pthread_once(), it is guaranteed that
init_routine() has completed".

Here is a proposal for a patch, I did not post it to ecos-patches
because I currently don't have a working eCos target to test this.


--- pthread.cxx	2009-01-29 18:47:52.000000000 +0100
+++ pthread-fixonce.cxx	2009-11-01 21:07:42.639875000 +0100
@@ -1328,22 +1328,23 @@ externC int pthread_once (pthread_once_t
     PTHREAD_ENTRY();

     PTHREAD_CHECK( once_control );
     PTHREAD_CHECK( init_routine );

-    pthread_once_t old;
-
-    // Do a test and set on the once_control object.
     pthread_mutex.lock();

-    old = *once_control;
-    *once_control = 1;
+    // If the once_control is zero, call the init_routine().
+    // the mutex must stay locked during init_routine because
+    // concurrent threads must be blocked until init_routine has
+    // done its business
+    if ( !*once_control )
+    {
+        init_routine();
+        *once_control = 1;
+    }

     pthread_mutex.unlock();
-
-    // If the once_control was zero, call the init_routine().
-    if( !old ) init_routine();

     PTHREAD_RETURN(0);
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-11-10 15:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-01 20:52 pthread_once non conformant Jerome Souquieres
2009-11-10  4:06 ` Jonathan Larmour
2009-11-10 15:24   ` Jerome Souquieres

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).