Skip to content

Commit faebc1c

Browse files
committed
threads: split up OS-dependent thread code
1 parent 69c71f2 commit faebc1c

File tree

8 files changed

+37
-45
lines changed

8 files changed

+37
-45
lines changed

src/pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ static int ll_find_deltas(git_packbuilder *pb, git_pobject **list,
11861186
git_mutex_init(&p[i].mutex);
11871187
git_cond_init(&p[i].cond);
11881188

1189-
ret = git_thread_create(&p[i].thread, NULL,
1189+
ret = git_thread_create(&p[i].thread,
11901190
threaded_find_deltas, &p[i]);
11911191
if (ret) {
11921192
giterr_set(GITERR_THREAD, "unable to create thread");

src/thread-utils.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,7 @@ typedef git_atomic git_atomic_ssize;
4141
#ifdef GIT_THREADS
4242

4343
#if !defined(GIT_WIN32)
44-
45-
typedef struct {
46-
pthread_t thread;
47-
} git_thread;
48-
49-
#define git_thread_create(git_thread_ptr, attr, start_routine, arg) \
50-
pthread_create(&(git_thread_ptr)->thread, attr, start_routine, arg)
51-
#define git_thread_join(git_thread_ptr, status) \
52-
pthread_join((git_thread_ptr)->thread, status)
53-
44+
# include "unix/pthread.h"
5445
#endif
5546

5647
/* Pthreads Mutex */
@@ -178,7 +169,7 @@ GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
178169
#else
179170

180171
#define git_thread unsigned int
181-
#define git_thread_create(thread, attr, start_routine, arg) 0
172+
#define git_thread_create(thread, start_routine, arg) 0
182173
#define git_thread_join(id, status) (void)0
183174

184175
/* Pthreads Mutex */

src/unix/pthread.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef INCLUDE_unix_pthread_h__
9+
#define INCLUDE_unix_pthread_h__
10+
11+
typedef struct {
12+
pthread_t thread;
13+
} git_thread;
14+
15+
#define git_thread_create(git_thread_ptr, start_routine, arg) \
16+
pthread_create(&(git_thread_ptr)->thread, NULL, start_routine, arg)
17+
#define git_thread_join(git_thread_ptr, status) \
18+
pthread_join((git_thread_ptr)->thread, status)
19+
20+
#endif /* INCLUDE_unix_pthread_h__ */

src/win32/pthread.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* void pointer. This requires the indirection. */
1717
static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
1818
{
19-
git_win32_thread *thread = lpParameter;
19+
git_thread *thread = lpParameter;
2020

2121
thread->result = thread->proc(thread->param);
2222

@@ -25,14 +25,11 @@ static DWORD WINAPI git_win32__threadproc(LPVOID lpParameter)
2525
return CLEAN_THREAD_EXIT;
2626
}
2727

28-
int git_win32__thread_create(
29-
git_win32_thread *GIT_RESTRICT thread,
30-
const pthread_attr_t *GIT_RESTRICT attr,
28+
int git_thread_create(
29+
git_thread *GIT_RESTRICT thread,
3130
void *(*start_routine)(void*),
3231
void *GIT_RESTRICT arg)
3332
{
34-
GIT_UNUSED(attr);
35-
3633
thread->result = NULL;
3734
thread->param = arg;
3835
thread->proc = start_routine;
@@ -42,8 +39,8 @@ int git_win32__thread_create(
4239
return thread->thread ? 0 : -1;
4340
}
4441

45-
int git_win32__thread_join(
46-
git_win32_thread *thread,
42+
int git_thread_join(
43+
git_thread *thread,
4744
void **value_ptr)
4845
{
4946
DWORD exit;

src/win32/pthread.h

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef struct {
2121
void *(*proc)(void *);
2222
void *param;
2323
void *result;
24-
} git_win32_thread;
24+
} git_thread;
2525

2626
typedef int pthread_mutexattr_t;
2727
typedef int pthread_condattr_t;
@@ -42,26 +42,10 @@ typedef struct {
4242

4343
#define PTHREAD_MUTEX_INITIALIZER {(void*)-1}
4444

45-
int git_win32__thread_create(
46-
git_win32_thread *GIT_RESTRICT,
47-
const pthread_attr_t *GIT_RESTRICT,
45+
int git_thread_create(git_thread *GIT_RESTRICT,
4846
void *(*) (void *),
4947
void *GIT_RESTRICT);
50-
51-
int git_win32__thread_join(
52-
git_win32_thread *,
53-
void **);
54-
55-
#ifdef GIT_THREADS
56-
57-
typedef git_win32_thread git_thread;
58-
59-
#define git_thread_create(git_thread_ptr, attr, start_routine, arg) \
60-
git_win32__thread_create(git_thread_ptr, attr, start_routine, arg)
61-
#define git_thread_join(git_thread_ptr, status) \
62-
git_win32__thread_join(git_thread_ptr, status)
63-
64-
#endif
48+
int git_thread_join(git_thread *, void **);
6549

6650
int pthread_mutex_init(
6751
pthread_mutex_t *GIT_RESTRICT mutex,

tests/object/cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ void test_object_cache__threadmania(void)
220220
fn = (th & 1) ? cache_parsed : cache_raw;
221221

222222
#ifdef GIT_THREADS
223-
cl_git_pass(git_thread_create(&t[th], NULL, fn, data));
223+
cl_git_pass(git_thread_create(&t[th], fn, data));
224224
#else
225225
cl_assert(fn(data) == data);
226226
git__free(data);
@@ -267,7 +267,7 @@ void test_object_cache__fast_thread_rush(void)
267267
data[th] = th;
268268
#ifdef GIT_THREADS
269269
cl_git_pass(
270-
git_thread_create(&t[th], NULL, cache_quick, &data[th]));
270+
git_thread_create(&t[th], cache_quick, &data[th]));
271271
#else
272272
cl_assert(cache_quick(&data[th]) == &data[th]);
273273
#endif

tests/threads/refdb.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void test_threads_refdb__iterator(void)
7575
for (t = 0; t < THREADS; ++t) {
7676
id[t] = t;
7777
#ifdef GIT_THREADS
78-
cl_git_pass(git_thread_create(&th[t], NULL, iterate_refs, &id[t]));
78+
cl_git_pass(git_thread_create(&th[t], iterate_refs, &id[t]));
7979
#else
8080
th[t] = t;
8181
iterate_refs(&id[t]);
@@ -196,7 +196,7 @@ void test_threads_refdb__edit_while_iterate(void)
196196
* for now by just running on a single thread...
197197
*/
198198
/* #ifdef GIT_THREADS */
199-
/* cl_git_pass(git_thread_create(&th[t], NULL, fn, &id[t])); */
199+
/* cl_git_pass(git_thread_create(&th[t], fn, &id[t])); */
200200
/* #else */
201201
fn(&id[t]);
202202
/* #endif */
@@ -211,7 +211,7 @@ void test_threads_refdb__edit_while_iterate(void)
211211

212212
for (t = 0; t < THREADS; ++t) {
213213
id[t] = t;
214-
cl_git_pass(git_thread_create(&th[t], NULL, iterate_refs, &id[t]));
214+
cl_git_pass(git_thread_create(&th[t], iterate_refs, &id[t]));
215215
}
216216

217217
for (t = 0; t < THREADS; ++t) {

tests/threads/thread_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void run_in_parallel(
2424
for (t = 0; t < threads; ++t) {
2525
id[t] = t;
2626
#ifdef GIT_THREADS
27-
cl_git_pass(git_thread_create(&th[t], NULL, func, &id[t]));
27+
cl_git_pass(git_thread_create(&th[t], func, &id[t]));
2828
#else
2929
cl_assert(func(&id[t]) == &id[t]);
3030
#endif

0 commit comments

Comments
 (0)