]> git.proxmox.com Git - libgit2.git/commitdiff
threads: split up OS-dependent thread code
authorPatrick Steinhardt <ps@pks.im>
Mon, 20 Jun 2016 15:44:04 +0000 (17:44 +0200)
committerPatrick Steinhardt <ps@pks.im>
Mon, 20 Jun 2016 17:32:59 +0000 (19:32 +0200)
src/pack-objects.c
src/thread-utils.h
src/unix/pthread.h [new file with mode: 0644]
src/win32/pthread.c
src/win32/pthread.h
tests/object/cache.c
tests/threads/refdb.c
tests/threads/thread_helpers.c

index 11e13f7d45dd96e08bc84630a2d7bc7b89819660..29231e0288c2e7573448b5c99e0b4b39d16b5d1c 100644 (file)
@@ -1186,7 +1186,7 @@ static int ll_find_deltas(git_packbuilder *pb, git_pobject **list,
                git_mutex_init(&p[i].mutex);
                git_cond_init(&p[i].cond);
 
-               ret = git_thread_create(&p[i].thread, NULL,
+               ret = git_thread_create(&p[i].thread,
                                        threaded_find_deltas, &p[i]);
                if (ret) {
                        giterr_set(GITERR_THREAD, "unable to create thread");
index 14c8a41ff14ef1393bbc0e33e14ccf76d165baaf..11c026f331a937af9eb0982c2d934b9aff69b58a 100644 (file)
@@ -41,16 +41,7 @@ typedef git_atomic git_atomic_ssize;
 #ifdef GIT_THREADS
 
 #if !defined(GIT_WIN32)
-
-typedef struct {
-       pthread_t thread;
-} git_thread;
-
-#define git_thread_create(git_thread_ptr, attr, start_routine, arg) \
-       pthread_create(&(git_thread_ptr)->thread, attr, start_routine, arg)
-#define git_thread_join(git_thread_ptr, status) \
-       pthread_join((git_thread_ptr)->thread, status)
-
+#   include "unix/pthread.h"
 #endif
 
 /* Pthreads Mutex */
@@ -178,7 +169,7 @@ GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
 #else
 
 #define git_thread unsigned int
-#define git_thread_create(thread, attr, start_routine, arg) 0
+#define git_thread_create(thread, start_routine, arg) 0
 #define git_thread_join(id, status) (void)0
 
 /* Pthreads Mutex */
diff --git a/src/unix/pthread.h b/src/unix/pthread.h
new file mode 100644 (file)
index 0000000..87384a4
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#ifndef INCLUDE_unix_pthread_h__
+#define INCLUDE_unix_pthread_h__
+
+typedef struct {
+       pthread_t thread;
+} git_thread;
+
+#define git_thread_create(git_thread_ptr, start_routine, arg) \
+       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)
+
+#endif /* INCLUDE_unix_pthread_h__ */
index a1cc1893251e3ec49aaec0b9332253375c32f40d..d8ed4bb1bb61312550afbd9e5e26a977d09bb6e6 100644 (file)
@@ -16,7 +16,7 @@
  * void pointer. This requires the indirection. */
 static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
 {
-       git_win32_thread *thread = lpParameter;
+       git_thread *thread = lpParameter;
 
        thread->result = thread->proc(thread->param);
 
@@ -25,14 +25,11 @@ static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
        return CLEAN_THREAD_EXIT;
 }
 
-int git_win32__thread_create(
-       git_win32_thread *GIT_RESTRICT thread,
-       const pthread_attr_t *GIT_RESTRICT attr,
+int git_thread_create(
+       git_thread *GIT_RESTRICT thread,
        void *(*start_routine)(void*),
        void *GIT_RESTRICT arg)
 {
-       GIT_UNUSED(attr);
-
        thread->result = NULL;
        thread->param = arg;
        thread->proc = start_routine;
@@ -42,8 +39,8 @@ int git_win32__thread_create(
        return thread->thread ? 0 : -1;
 }
 
-int git_win32__thread_join(
-       git_win32_thread *thread,
+int git_thread_join(
+       git_thread *thread,
        void **value_ptr)
 {
        DWORD exit;
index e4826ca7f5bfef6fcd0289e5e233c5d63070190b..fa99b01d68ab3566c3ea12a093139c1c375bf022 100644 (file)
@@ -21,7 +21,7 @@ typedef struct {
        void *(*proc)(void *);
        void *param;
        void *result;
-} git_win32_thread;
+} git_thread;
 
 typedef int pthread_mutexattr_t;
 typedef int pthread_condattr_t;
@@ -42,26 +42,10 @@ typedef struct {
 
 #define PTHREAD_MUTEX_INITIALIZER  {(void*)-1}
 
-int git_win32__thread_create(
-       git_win32_thread *GIT_RESTRICT,
-       const pthread_attr_t *GIT_RESTRICT,
+int git_thread_create(git_thread *GIT_RESTRICT,
        void *(*) (void *),
        void *GIT_RESTRICT);
-
-int git_win32__thread_join(
-       git_win32_thread *,
-       void **);
-
-#ifdef GIT_THREADS
-
-typedef git_win32_thread git_thread;
-
-#define git_thread_create(git_thread_ptr, attr, start_routine, arg) \
-       git_win32__thread_create(git_thread_ptr, attr, start_routine, arg)
-#define git_thread_join(git_thread_ptr, status) \
-       git_win32__thread_join(git_thread_ptr, status)
-
-#endif
+int git_thread_join(git_thread *, void **);
 
 int pthread_mutex_init(
        pthread_mutex_t *GIT_RESTRICT mutex,
index bdf12da7ae2e8ae65f50eb33c59d1b9c2db4ee1f..680f23630f57b357718b36ffd56dcee54a689848 100644 (file)
@@ -220,7 +220,7 @@ void test_object_cache__threadmania(void)
                        fn = (th & 1) ? cache_parsed : cache_raw;
 
 #ifdef GIT_THREADS
-                       cl_git_pass(git_thread_create(&t[th], NULL, fn, data));
+                       cl_git_pass(git_thread_create(&t[th], fn, data));
 #else
                        cl_assert(fn(data) == data);
                        git__free(data);
@@ -267,7 +267,7 @@ void test_object_cache__fast_thread_rush(void)
                        data[th] = th;
 #ifdef GIT_THREADS
                        cl_git_pass(
-                               git_thread_create(&t[th], NULL, cache_quick, &data[th]));
+                               git_thread_create(&t[th], cache_quick, &data[th]));
 #else
                        cl_assert(cache_quick(&data[th]) == &data[th]);
 #endif
index 6589e3922cf8dc57f74201e35a54607fc2f5187d..f869bcb44b8bcaa6e530842706a99c1bc9cb190a 100644 (file)
@@ -75,7 +75,7 @@ void test_threads_refdb__iterator(void)
                for (t = 0; t < THREADS; ++t) {
                        id[t] = t;
 #ifdef GIT_THREADS
-                       cl_git_pass(git_thread_create(&th[t], NULL, iterate_refs, &id[t]));
+                       cl_git_pass(git_thread_create(&th[t], iterate_refs, &id[t]));
 #else
                        th[t] = t;
                        iterate_refs(&id[t]);
@@ -196,7 +196,7 @@ void test_threads_refdb__edit_while_iterate(void)
                 * for now by just running on a single thread...
                 */
 /* #ifdef GIT_THREADS */
-/*             cl_git_pass(git_thread_create(&th[t], NULL, fn, &id[t])); */
+/*             cl_git_pass(git_thread_create(&th[t], fn, &id[t])); */
 /* #else */
                fn(&id[t]);
 /* #endif */
@@ -211,7 +211,7 @@ void test_threads_refdb__edit_while_iterate(void)
 
        for (t = 0; t < THREADS; ++t) {
                id[t] = t;
-               cl_git_pass(git_thread_create(&th[t], NULL, iterate_refs, &id[t]));
+               cl_git_pass(git_thread_create(&th[t], iterate_refs, &id[t]));
        }
 
        for (t = 0; t < THREADS; ++t) {
index 760a7bd33b78d2c159b0b0bf3d3a575dc285ffab..54bf6097d15f428eca21b3658c934b634dc276be 100644 (file)
@@ -24,7 +24,7 @@ void run_in_parallel(
                for (t = 0; t < threads; ++t) {
                        id[t] = t;
 #ifdef GIT_THREADS
-                       cl_git_pass(git_thread_create(&th[t], NULL, func, &id[t]));
+                       cl_git_pass(git_thread_create(&th[t], func, &id[t]));
 #else
                        cl_assert(func(&id[t]) == &id[t]);
 #endif