+ * Singleton fork(). Do not copy any running threads.
+ * ------------------------------------------------------------------------- */
+
+StgInt forkProcess(StgTSO* tso) {
+
+#ifndef mingw32_TARGET_OS
+ pid_t pid;
+ StgTSO* t,*next;
+ StgMainThread *m;
+ rtsBool doKill;
+
+ IF_DEBUG(scheduler,sched_belch("forking!"));
+
+ pid = fork();
+ if (pid) { /* parent */
+
+ /* just return the pid */
+
+ } else { /* child */
+ /* wipe all other threads */
+ run_queue_hd = run_queue_tl = tso;
+ tso->link = END_TSO_QUEUE;
+
+ /* When clearing out the threads, we need to ensure
+ that a 'main thread' is left behind; if there isn't,
+ the Scheduler will shutdown next time it is entered.
+
+ ==> we don't kill a thread that's on the main_threads
+ list (nor the current thread.)
+
+ [ Attempts at implementing the more ambitious scheme of
+ killing the main_threads also, and then adding the
+ current thread onto the main_threads list if it wasn't
+ there already, failed -- waitThread() (for one) wasn't
+ up to it. If it proves to be desirable to also kill
+ the main threads, then this scheme will have to be
+ revisited (and fully debugged!)
+
+ -- sof 7/2002
+ ]
+ */
+ /* DO NOT TOUCH THE QUEUES directly because most of the code around
+ us is picky about finding the thread still in its queue when
+ handling the deleteThread() */
+
+ for (t = all_threads; t != END_TSO_QUEUE; t = next) {
+ next = t->link;
+
+ /* Don't kill the current thread.. */
+ if (t->id == tso->id) continue;
+ doKill=rtsTrue;
+ /* ..or a main thread */
+ for (m = main_threads; m != NULL; m = m->link) {
+ if (m->tso->id == t->id) {
+ doKill=rtsFalse;
+ break;
+ }
+ }
+ if (doKill) {
+ deleteThread(t);
+ }
+ }
+ }
+ return pid;
+#else /* mingw32 */
+ barf("forkProcess#: primop not implemented for mingw32, sorry! (%u)\n", tso->id);
+ /* pointlessly printing out the TSOs 'id' to avoid CC unused warning. */
+ return -1;
+#endif /* mingw32 */
+}
+
+/* ---------------------------------------------------------------------------