From 200a76b74db5c2c75bcf73773cb85c5603ec038e Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sat, 6 Mar 2010 16:40:42 +0100 Subject: Reimplement async procedures using pthreads On Windows, async procedures have always been run in threads, and the implementation used Windows specific APIs. Rewrite the code to use pthreads. A new configuration option is introduced so that the threaded implementation can also be used on POSIX systems. Since this option is intended only as playground on POSIX, but is mandatory on Windows, the option is not documented. One detail is that on POSIX it is necessary to set FD_CLOEXEC on the pipe handles. On Windows, this is not needed because pipe handles are not inherited to child processes, and the new calls to set_cloexec() are effectively no-ops. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- run-command.c | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'run-command.c') diff --git a/run-command.c b/run-command.c index 0cd7f02ffe..77aefff2e2 100644 --- a/run-command.c +++ b/run-command.c @@ -82,6 +82,7 @@ static NORETURN void die_child(const char *err, va_list params) write(child_err, "\n", 1); exit(128); } +#endif static inline void set_cloexec(int fd) { @@ -89,7 +90,6 @@ static inline void set_cloexec(int fd) if (flags >= 0) fcntl(fd, F_SETFD, flags | FD_CLOEXEC); } -#endif static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure) { @@ -447,11 +447,12 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const return run_command(&cmd); } -#ifdef WIN32 -static unsigned __stdcall run_thread(void *data) +#ifdef ASYNC_AS_THREAD +static void *run_thread(void *data) { struct async *async = data; - return async->proc(async->proc_in, async->proc_out, async->data); + intptr_t ret = async->proc(async->proc_in, async->proc_out, async->data); + return (void *)ret; } #endif @@ -497,7 +498,7 @@ int start_async(struct async *async) else proc_out = -1; -#ifndef WIN32 +#ifndef ASYNC_AS_THREAD /* Flush stdio before fork() to avoid cloning buffers */ fflush(NULL); @@ -524,12 +525,18 @@ int start_async(struct async *async) else if (async->out) close(async->out); #else + if (proc_in >= 0) + set_cloexec(proc_in); + if (proc_out >= 0) + set_cloexec(proc_out); async->proc_in = proc_in; async->proc_out = proc_out; - async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL); - if (!async->tid) { - error("cannot create thread: %s", strerror(errno)); - goto error; + { + int err = pthread_create(&async->tid, NULL, run_thread, async); + if (err) { + error("cannot create thread: %s", strerror(err)); + goto error; + } } #endif return 0; @@ -549,17 +556,15 @@ error: int finish_async(struct async *async) { -#ifndef WIN32 - int ret = wait_or_whine(async->pid, "child process", 0); +#ifndef ASYNC_AS_THREAD + return wait_or_whine(async->pid, "child process", 0); #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); + void *ret = (void *)(intptr_t)(-1); + + if (pthread_join(async->tid, &ret)) + error("pthread_join failed"); + return (int)(intptr_t)ret; #endif - return ret; } int run_hook(const char *index_file, const char *name, ...) -- cgit v1.2.1 From 0ea1c89ba616397ef7e5f6f601ef7a24d2c27b8e Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sat, 6 Mar 2010 16:40:43 +0100 Subject: Dying in an async procedure should only exit the thread, not the process. Async procedures are intended as helpers that perform a very restricted task, and the caller usually has to manage them in a larger context. Conceptually, the async procedure is not concerned with the "bigger picture" in whose context it is run. When it dies, it is not supposed to destroy this "bigger picture", but rather only its own limit view of the world. On POSIX, the async procedure is run in its own process, and exiting this process naturally had only these limited effects. On Windows (or when ASYNC_AS_THREAD is set), calling die() exited the whole process, destroying the caller (the "big picture") as well. This fixes it to exit only the thread. Without ASYNC_AS_THREAD, one particular effect of exiting the async procedure process is that it automatically closes file descriptors, most notably the writable end of the pipe that the async procedure writes to. The async API already requires that the async procedure closes the pipe ends when it exits normally. But for calls to die() no requirements are imposed. In the non-threaded case the pipe ends are closed implicitly by the exiting process, but in the threaded case, the die routine must take care of closing them. Now t5530-upload-pack-error.sh passes on Windows. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- run-command.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'run-command.c') diff --git a/run-command.c b/run-command.c index 77aefff2e2..66cc4bfa57 100644 --- a/run-command.c +++ b/run-command.c @@ -448,12 +448,35 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const } #ifdef ASYNC_AS_THREAD +static pthread_t main_thread; +static int main_thread_set; +static pthread_key_t async_key; + static void *run_thread(void *data) { struct async *async = data; + + pthread_setspecific(async_key, async); + intptr_t ret = async->proc(async->proc_in, async->proc_out, async->data); return (void *)ret; } + +static NORETURN void die_async(const char *err, va_list params) +{ + vreportf("fatal: ", err, params); + + if (!pthread_equal(main_thread, pthread_self())) { + struct async *async = pthread_getspecific(async_key); + if (async->proc_in >= 0) + close(async->proc_in); + if (async->proc_out >= 0) + close(async->proc_out); + pthread_exit((void *)128); + } + + exit(128); +} #endif int start_async(struct async *async) @@ -525,6 +548,17 @@ int start_async(struct async *async) else if (async->out) close(async->out); #else + if (!main_thread_set) { + /* + * We assume that the first time that start_async is called + * it is from the main thread. + */ + main_thread_set = 1; + main_thread = pthread_self(); + pthread_key_create(&async_key, NULL); + set_die_routine(die_async); + } + if (proc_in >= 0) set_cloexec(proc_in); if (proc_out >= 0) -- cgit v1.2.1 From f6b6098316192475ff0b3fa2ba894d7e555bdfac Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Tue, 9 Mar 2010 21:00:36 +0100 Subject: Enable threaded async procedures whenever pthreads is available Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- run-command.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'run-command.c') diff --git a/run-command.c b/run-command.c index 66cc4bfa57..61b153987b 100644 --- a/run-command.c +++ b/run-command.c @@ -447,7 +447,7 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const return run_command(&cmd); } -#ifdef ASYNC_AS_THREAD +#ifndef NO_PTHREADS static pthread_t main_thread; static int main_thread_set; static pthread_key_t async_key; @@ -455,10 +455,10 @@ static pthread_key_t async_key; static void *run_thread(void *data) { struct async *async = data; + intptr_t ret; pthread_setspecific(async_key, async); - - intptr_t ret = async->proc(async->proc_in, async->proc_out, async->data); + ret = async->proc(async->proc_in, async->proc_out, async->data); return (void *)ret; } @@ -521,7 +521,7 @@ int start_async(struct async *async) else proc_out = -1; -#ifndef ASYNC_AS_THREAD +#ifdef NO_PTHREADS /* Flush stdio before fork() to avoid cloning buffers */ fflush(NULL); @@ -590,7 +590,7 @@ error: int finish_async(struct async *async) { -#ifndef ASYNC_AS_THREAD +#ifdef NO_PTHREADS return wait_or_whine(async->pid, "child process", 0); #else void *ret = (void *)(intptr_t)(-1); -- cgit v1.2.1