mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-09 16:45:49 +00:00
threads: split up OS-dependent thread code
This commit is contained in:
parent
69c71f2917
commit
faebc1c6ec
@ -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");
|
||||
|
@ -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 */
|
||||
|
20
src/unix/pthread.h
Normal file
20
src/unix/pthread.h
Normal 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__ */
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user