Added a fork() replacement for pthread code...
authorRyan C. Gordon <icculus@icculus.org>
Wed, 14 Jul 2004 03:04:25 +0000
changeset 65 35c52890a14f
parent 64 8f4e25f1ceb5
child 66 118c760d8e55
Added a fork() replacement for pthread code...
Makefile
platform_unix.c
--- a/Makefile	Wed Jul 14 02:48:12 2004 +0000
+++ b/Makefile	Wed Jul 14 03:04:25 2004 +0000
@@ -6,10 +6,19 @@
 BINDIR := bin
 SRCDIR := .
 
+# must be "macosx" or "unix" or "win32" ... not all necessarily work right now.
 platform := macosx
+
+# Add zlib support? Will compress all ADD/ADDORREPLACE/PATCH operations.
+# If you're going to compress the patch anyhow, this might not be wanted.
 use_zlib := true
+
+# Unix/Mac will try fork() if this is false.
 use_pthread := false
 
+
+# you probably shouldn't touch anything below this line.
+
 ifeq ($(strip $(platform)),macosx)
 PLATFORMDEF := -DPLATFORM_UNIX -DPLATFORM_MACOSX
 PLATFORMSRCS := platform_unix.c ui_carbon.c
@@ -22,11 +31,8 @@
 endif
 
 ifeq ($(strip $(platform)),unix)
-PLATFORMDEF := -DPLATFORM_UNIX
-PLATFORMSRCS := platform_unix.c ui_stdio.c
-
-# !!! FIXME: This is forced on for now.
-use_pthread := true
+  PLATFORMDEF := -DPLATFORM_UNIX
+  PLATFORMSRCS := platform_unix.c ui_stdio.c
 endif
 
 CFLAGS := $(PLATFORMDEF) -Wall -g -fsigned-char -fno-omit-frame-pointer -O0
@@ -37,8 +43,10 @@
 endif
 
 ifeq ($(strip $(use_pthread)),true)
-  LDFLAGS += -lpthread
   CFLAGS += -DUSE_PTHREAD=1
+  ifneq ($(strip $(platform)),macosx)
+    LDFLAGS += -lpthread
+  endif
 endif
 
 MOJOPATCHSRCS := mojopatch.c md5.c $(PLATFORMSRCS)
--- a/platform_unix.c	Wed Jul 14 02:48:12 2004 +0000
+++ b/platform_unix.c	Wed Jul 14 03:04:25 2004 +0000
@@ -11,11 +11,6 @@
 #include <errno.h>
 #include <assert.h>
 
-/* !!! FIXME: Why aren't we using fork(), anyhow? */
-#if !USE_PTHREAD
-#error not implemented yet.
-#endif
-
 #if USE_PTHREAD
 #include <pthread.h>
 #endif
@@ -520,19 +515,44 @@
 
 int spawn_xdelta(const char *cmdline)
 {
-#if !USE_PTHREAD
-    _fatal("No pthread support!");
-    return(0);
-#else
-    pthread_t thr;
-    void *rc;
     const char *binname = "xdelta";
     char *cmd = alloca(strlen(cmdline) + strlen(basedir) + strlen(binname) + 2);
     if (!cmd)
         return(0);
 
     sprintf(cmd, "\"%s%s\" %s", basedir, binname, cmdline);
+
+#if !USE_PTHREAD
+    int rc = 0;
+    pid_t pid = fork();
+    if (pid == -1)
+    {
+        int e = errno;
+        _fatal("fork() failed: %d (%s)", e, strerror(e));
+        return(0);
+    } /* if */
+
+    else if (pid == 0)   // child process.
+    {
+        rc = spawn_thread(cmd);
+        exit(1);  /* !!! FIXME    *((int *) rc) == 0 ); */
+    } /* else if */
+
+    else
+    {
+        while (waitpid(pid, &rc, WNOHANG) == 0)
+        {
+            ui_pump();
+            usleep(10000);
+        } /* while */
+        return(1);
+    } /* else */
+#else
+    pthread_t thr;
+    void *rc;
+
     thread_alive = 1;
+
     if (pthread_create(&thr, NULL, spawn_thread, cmd) != 0)
         return(0);