hs_add_root: use use rts_lock()/rts_unlock() for a bit of extra safety
[ghc-hetmet.git] / rts / RtsStartup.c
index 4f84468..774de72 100644 (file)
@@ -281,10 +281,6 @@ hs_init(int *argc, char **argv[])
     x86_init_fpu();
 #endif
 
-#if defined(THREADED_RTS)
-    ioManagerStart();
-#endif
-
     /* Record initialization times */
     stat_endInit();
 }
@@ -334,7 +330,9 @@ hs_add_root(void (*init_root)(void))
 {
     bdescr *bd;
     nat init_sp;
-    Capability *cap = &MainCapability;
+    Capability *cap;
+
+    cap = rts_lock();
 
     if (hs_init_count <= 0) {
        barf("hs_add_root() must be called after hs_init()");
@@ -360,6 +358,13 @@ hs_add_root(void (*init_root)(void))
     // This must be done after module initialisation.
     // ToDo: make this work in the presence of multiple hs_add_root()s.
     initProfiling2();
+
+    rts_unlock(cap);
+
+    // ditto.
+#if defined(THREADED_RTS)
+    ioManagerStart();
+#endif
 }
 
 /* ----------------------------------------------------------------------------
@@ -498,7 +503,7 @@ hs_exit_(rtsBool wait_foreign)
     // Originally, this was in report_ccs_profiling().  Now, retainer
     // profiling might tack some extra stuff on to the end of this file
     // during endProfiling().
-    fclose(prof_file);
+    if (prof_file != NULL) fclose(prof_file);
 #endif
 
 #if defined(TICKY_TICKY)