From d3d5d0c2e55532839b1d0ad9e8b0c7d353fc51a7 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sat, 27 Oct 2007 23:21:20 +0200 Subject: [PATCH] Implement asynchronous functions using threads on Windows. A kill(2) is removed from upload-pack.c because we cannot assume that rev-list is run as a process. Signed-off-by: Johannes Sixt --- run-command.c | 29 ++++++++++++++++++++++++++++- run-command.h | 5 +++++ upload-pack.c | 5 +---- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/run-command.c b/run-command.c index dca8626e22..d2e5f183d2 100644 --- a/run-command.c +++ b/run-command.c @@ -186,13 +186,23 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const return run_command(&cmd); } +#ifdef __MINGW32__ +static __stdcall unsigned run_thread(void *data) +{ + struct async *async = data; + return async->proc(async->fd_for_proc, async->data); +} +#endif + int start_async(struct async *async) { int pipe_out[2]; if (pipe(pipe_out) < 0) return error("cannot create pipe: %s", strerror(errno)); + async->out = pipe_out[0]; +#ifndef __MINGW32__ async->pid = fork(); if (async->pid < 0) { error("fork (async) failed: %s", strerror(errno)); @@ -203,16 +213,33 @@ int start_async(struct async *async) close(pipe_out[0]); exit(!!async->proc(pipe_out[1], async->data)); } - async->out = pipe_out[0]; close(pipe_out[1]); +#else + async->fd_for_proc = pipe_out[1]; + async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL); + if (!async->tid) { + error("cannot create thread: %s", strerror(errno)); + close_pair(pipe_out); + return -1; + } +#endif return 0; } int finish_async(struct async *async) { +#ifndef __MINGW32__ int ret = 0; if (wait_or_whine(async->pid)) ret = error("waitpid (async) failed"); +#else + DWORD ret = 0; + if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0) + ret = error("waiting for thread failed: %lu", GetLastError()); + else if (!GetExitCodeThread(async->tid, &ret)) + ret = error("cannot get thread exit code: %lu", GetLastError()); + CloseHandle(async->tid); +#endif return ret; } diff --git a/run-command.h b/run-command.h index 94e1e9d516..407d3c3bc3 100644 --- a/run-command.h +++ b/run-command.h @@ -59,7 +59,12 @@ struct async { int (*proc)(int fd, void *data); void *data; int out; /* caller reads from here and closes it */ +#ifndef __MINGW32__ pid_t pid; +#else + HANDLE tid; + int fd_for_proc; +#endif }; int start_async(struct async *async); diff --git a/upload-pack.c b/upload-pack.c index 369cb1f77c..0d7aa18bfe 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -170,11 +170,8 @@ static void create_pack_file(void) pack_objects.git_cmd = 1; pack_objects.argv = argv; - if (start_command(&pack_objects)) { - /* daemon sets things up to ignore TERM */ - kill(rev_list.pid, SIGKILL); + if (start_command(&pack_objects)) die("git-upload-pack: unable to fork git-pack-objects"); - } #ifndef __MINGW32__ /* We read from pack_objects.err to capture stderr output for