threads: split up OS-dependent thread code

This commit is contained in:
Patrick Steinhardt 2016-06-20 17:44:04 +02:00
parent 69c71f2917
commit faebc1c6ec
8 changed files with 37 additions and 45 deletions

View File

@ -1186,7 +1186,7 @@ static int ll_find_deltas(git_packbuilder *pb, git_pobject **list,
git_mutex_init(&p[i].mutex); git_mutex_init(&p[i].mutex);
git_cond_init(&p[i].cond); 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]); threaded_find_deltas, &p[i]);
if (ret) { if (ret) {
giterr_set(GITERR_THREAD, "unable to create thread"); giterr_set(GITERR_THREAD, "unable to create thread");

View File

@ -41,16 +41,7 @@ typedef git_atomic git_atomic_ssize;
#ifdef GIT_THREADS #ifdef GIT_THREADS
#if !defined(GIT_WIN32) #if !defined(GIT_WIN32)
# include "unix/pthread.h"
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)
#endif #endif
/* Pthreads Mutex */ /* Pthreads Mutex */
@ -178,7 +169,7 @@ GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
#else #else
#define git_thread unsigned int #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 #define git_thread_join(id, status) (void)0
/* Pthreads Mutex */ /* Pthreads Mutex */

20
src/unix/pthread.h Normal file
View File

@ -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__ */

View File

@ -16,7 +16,7 @@
* void pointer. This requires the indirection. */ * void pointer. This requires the indirection. */
static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter) static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
{ {
git_win32_thread *thread = lpParameter; git_thread *thread = lpParameter;
thread->result = thread->proc(thread->param); thread->result = thread->proc(thread->param);
@ -25,14 +25,11 @@ static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
return CLEAN_THREAD_EXIT; return CLEAN_THREAD_EXIT;
} }
int git_win32__thread_create( int git_thread_create(
git_win32_thread *GIT_RESTRICT thread, git_thread *GIT_RESTRICT thread,
const pthread_attr_t *GIT_RESTRICT attr,
void *(*start_routine)(void*), void *(*start_routine)(void*),
void *GIT_RESTRICT arg) void *GIT_RESTRICT arg)
{ {
GIT_UNUSED(attr);
thread->result = NULL; thread->result = NULL;
thread->param = arg; thread->param = arg;
thread->proc = start_routine; thread->proc = start_routine;
@ -42,8 +39,8 @@ int git_win32__thread_create(
return thread->thread ? 0 : -1; return thread->thread ? 0 : -1;
} }
int git_win32__thread_join( int git_thread_join(
git_win32_thread *thread, git_thread *thread,
void **value_ptr) void **value_ptr)
{ {
DWORD exit; DWORD exit;

View File

@ -21,7 +21,7 @@ typedef struct {
void *(*proc)(void *); void *(*proc)(void *);
void *param; void *param;
void *result; void *result;
} git_win32_thread; } git_thread;
typedef int pthread_mutexattr_t; typedef int pthread_mutexattr_t;
typedef int pthread_condattr_t; typedef int pthread_condattr_t;
@ -42,26 +42,10 @@ typedef struct {
#define PTHREAD_MUTEX_INITIALIZER {(void*)-1} #define PTHREAD_MUTEX_INITIALIZER {(void*)-1}
int git_win32__thread_create( int git_thread_create(git_thread *GIT_RESTRICT,
git_win32_thread *GIT_RESTRICT,
const pthread_attr_t *GIT_RESTRICT,
void *(*) (void *), void *(*) (void *),
void *GIT_RESTRICT); void *GIT_RESTRICT);
int git_thread_join(git_thread *, void **);
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 pthread_mutex_init( int pthread_mutex_init(
pthread_mutex_t *GIT_RESTRICT mutex, pthread_mutex_t *GIT_RESTRICT mutex,

View File

@ -220,7 +220,7 @@ void test_object_cache__threadmania(void)
fn = (th & 1) ? cache_parsed : cache_raw; fn = (th & 1) ? cache_parsed : cache_raw;
#ifdef GIT_THREADS #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 #else
cl_assert(fn(data) == data); cl_assert(fn(data) == data);
git__free(data); git__free(data);
@ -267,7 +267,7 @@ void test_object_cache__fast_thread_rush(void)
data[th] = th; data[th] = th;
#ifdef GIT_THREADS #ifdef GIT_THREADS
cl_git_pass( cl_git_pass(
git_thread_create(&t[th], NULL, cache_quick, &data[th])); git_thread_create(&t[th], cache_quick, &data[th]));
#else #else
cl_assert(cache_quick(&data[th]) == &data[th]); cl_assert(cache_quick(&data[th]) == &data[th]);
#endif #endif

View File

@ -75,7 +75,7 @@ void test_threads_refdb__iterator(void)
for (t = 0; t < THREADS; ++t) { for (t = 0; t < THREADS; ++t) {
id[t] = t; id[t] = t;
#ifdef GIT_THREADS #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 #else
th[t] = t; th[t] = t;
iterate_refs(&id[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... * for now by just running on a single thread...
*/ */
/* #ifdef GIT_THREADS */ /* #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 */ /* #else */
fn(&id[t]); fn(&id[t]);
/* #endif */ /* #endif */
@ -211,7 +211,7 @@ void test_threads_refdb__edit_while_iterate(void)
for (t = 0; t < THREADS; ++t) { for (t = 0; t < THREADS; ++t) {
id[t] = 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) { for (t = 0; t < THREADS; ++t) {

View File

@ -24,7 +24,7 @@ void run_in_parallel(
for (t = 0; t < threads; ++t) { for (t = 0; t < threads; ++t) {
id[t] = t; id[t] = t;
#ifdef GIT_THREADS #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 #else
cl_assert(func(&id[t]) == &id[t]); cl_assert(func(&id[t]) == &id[t]);
#endif #endif