threads: introduce `git_thread_exit` Introduce `git_thread_exit`, which will allow threads to terminate at an arbitrary time, returning a `void *`. On Windows, this means that we need to store the current `git_thread` in TLS, so that we can set its `return` value when terminating. We cannot simply use `ExitThread`, since Win32 returns `DWORD`s from threads; we return `void *`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
diff --git a/src/global.h b/src/global.h
index 2199515..88f40aa 100644
--- a/src/global.h
+++ b/src/global.h
@@ -16,6 +16,12 @@ typedef struct {
git_error error_t;
git_buf error_buf;
char oid_fmt[GIT_OID_HEXSZ+1];
+
+ /* On Windows, this is the current child thread that was started by
+ * `git_thread_create`. This is used to set the thread's exit code
+ * when terminated by `git_thread_exit`. It is unused on POSIX.
+ */
+ git_thread *current_thread;
} git_global_st;
#ifdef GIT_OPENSSL
diff --git a/src/unix/pthread.h b/src/unix/pthread.h
index 0f3f179..3f23d10 100644
--- a/src/unix/pthread.h
+++ b/src/unix/pthread.h
@@ -17,6 +17,8 @@ typedef struct {
pthread_create(&(git_thread_ptr)->thread, NULL, start_routine, arg)
#define git_thread_join(git_thread_ptr, status) \
pthread_join((git_thread_ptr)->thread, status)
+#define git_thread_currentid() ((size_t)(pthread_self()))
+#define git_thread_exit(retval) pthread_exit(retval)
/* Git Mutex */
#define git_mutex pthread_mutex_t
diff --git a/src/win32/thread.c b/src/win32/thread.c
index 80d56ce..87318c9 100644
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -26,6 +26,9 @@ static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
{
git_thread *thread = lpParameter;
+ /* Set the current thread for `git_thread_exit` */
+ GIT_GLOBAL->current_thread = thread;
+
thread->result = thread->proc(thread->param);
git__free_tls_data();
@@ -95,6 +98,21 @@ int git_thread_join(
return 0;
}
+void git_thread_exit(void *value)
+{
+ assert(GIT_GLOBAL->current_thread);
+ GIT_GLOBAL->current_thread->result = value;
+
+ git__free_tls_data();
+
+ ExitThread(CLEAN_THREAD_EXIT);
+}
+
+size_t git_thread_currentid(void)
+{
+ return GetCurrentThreadId();
+}
+
int git_mutex_init(git_mutex *GIT_RESTRICT mutex)
{
InitializeCriticalSection(mutex);
diff --git a/src/win32/thread.h b/src/win32/thread.h
index 0d01822..7f4a217 100644
--- a/src/win32/thread.h
+++ b/src/win32/thread.h
@@ -41,6 +41,8 @@ int git_thread_create(git_thread *GIT_RESTRICT,
void *(*) (void *),
void *GIT_RESTRICT);
int git_thread_join(git_thread *, void **);
+size_t git_thread_currentid(void);
+void git_thread_exit(void *);
int git_mutex_init(git_mutex *GIT_RESTRICT mutex);
int git_mutex_free(git_mutex *);
diff --git a/tests/threads/basic.c b/tests/threads/basic.c
index 9c342bc..685452d 100644
--- a/tests/threads/basic.c
+++ b/tests/threads/basic.c
@@ -48,3 +48,30 @@ void test_threads_basic__set_error(void)
{
run_in_parallel(1, 4, set_error, NULL, NULL);
}
+
+static void *return_normally(void *param)
+{
+ return param;
+}
+
+static void *exit_abruptly(void *param)
+{
+ git_thread_exit(param);
+ return NULL;
+}
+
+void test_threads_basic__exit(void)
+{
+ git_thread thread;
+ void *result;
+
+ /* Ensure that the return value of the threadproc is returned. */
+ cl_git_pass(git_thread_create(&thread, return_normally, (void *)424242));
+ cl_git_pass(git_thread_join(&thread, &result));
+ cl_assert_equal_sz(424242, (size_t)result);
+
+ /* Ensure that the return value of `git_thread_exit` is returned. */
+ cl_git_pass(git_thread_create(&thread, return_normally, (void *)232323));
+ cl_git_pass(git_thread_join(&thread, &result));
+ cl_assert_equal_sz(232323, (size_t)result);
+}
\ No newline at end of file