Made static destructors thread-safe
authorAndrew Waterman <waterman@r53.millennium.berkeley.edu>
Wed, 4 Nov 2009 06:30:44 +0000 (22:30 -0800)
committerAndrew Waterman <waterman@r53.millennium.berkeley.edu>
Wed, 4 Nov 2009 06:30:44 +0000 (22:30 -0800)
user/parlib/src/parlibmain.c

index b1a2f03..be0fe17 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <debug.h>
+#include <hart.h>
 
 // call static destructors.
 void parlib_dtors()
@@ -11,6 +12,11 @@ void parlib_dtors()
        typedef void (*dtor)(void);
        extern dtor __DTOR_LIST__[], __DTOR_END__[];
 
+       // make sure only one thread actually runs the dtors
+       static size_t already_done = 0;
+       if(hart_swap(&already_done,1) == 1)
+               return;
+
        for(int i = 0; i < __DTOR_END__ - __DTOR_LIST__; i++)
                __DTOR_LIST__[i]();
 }