Skip to content

Commit

Permalink
Added a fork() replacement for pthread code...
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jul 14, 2004
1 parent 98eb02c commit f782418
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
20 changes: 14 additions & 6 deletions Makefile
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
42 changes: 31 additions & 11 deletions platform_unix.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit f782418

Please sign in to comment.