From f7824180146341e3d89d1ee001dcda7b68c4939a Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 14 Jul 2004 03:04:25 +0000 Subject: [PATCH] Added a fork() replacement for pthread code... --- Makefile | 20 ++++++++++++++------ platform_unix.c | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index c3d9fe5..5b49cdc 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,19 @@ LINKER := gcc 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 @@ PLATFORMSRCS := platform_win32.c ui_stdio.c 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 @@ ifeq ($(strip $(use_zlib)),true) 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) diff --git a/platform_unix.c b/platform_unix.c index 5f44226..bca781b 100644 --- a/platform_unix.c +++ b/platform_unix.c @@ -11,11 +11,6 @@ #include #include -/* !!! FIXME: Why aren't we using fork(), anyhow? */ -#if !USE_PTHREAD -#error not implemented yet. -#endif - #if USE_PTHREAD #include #endif @@ -520,19 +515,44 @@ static void *spawn_thread(void *arg) 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);