Skip to content

Commit bb0edf8

Browse files
author
Edward Thomson
authored
Merge pull request libgit2#3830 from pks-t/pks/thread-namespacing
Thread namespacing
2 parents c80efb5 + aab266c commit bb0edf8

File tree

12 files changed

+181
-233
lines changed

12 files changed

+181
-233
lines changed

src/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
# include "win32/error.h"
4646
# include "win32/version.h"
4747
# ifdef GIT_THREADS
48-
# include "win32/pthread.h"
48+
# include "win32/thread.h"
4949
# endif
5050
# if defined(GIT_MSVC_CRTDBG)
5151
# include "win32/w32_stack.h"

src/global.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static int synchronized_threads_init(void)
134134

135135
_tls_index = TlsAlloc();
136136

137-
win32_pthread_initialize();
137+
git_threads_init();
138138

139139
if (git_mutex_init(&git__mwindow_mutex))
140140
return -1;

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: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -40,58 +40,12 @@ typedef git_atomic git_atomic_ssize;
4040

4141
#ifdef GIT_THREADS
4242

43-
#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-
54-
#endif
55-
56-
/* Pthreads Mutex */
57-
#define git_mutex pthread_mutex_t
58-
#define git_mutex_init(a) pthread_mutex_init(a, NULL)
59-
#define git_mutex_lock(a) pthread_mutex_lock(a)
60-
#define git_mutex_unlock(a) pthread_mutex_unlock(a)
61-
#define git_mutex_free(a) pthread_mutex_destroy(a)
62-
63-
/* Pthreads condition vars */
64-
#define git_cond pthread_cond_t
65-
#define git_cond_init(c) pthread_cond_init(c, NULL)
66-
#define git_cond_free(c) pthread_cond_destroy(c)
67-
#define git_cond_wait(c, l) pthread_cond_wait(c, l)
68-
#define git_cond_signal(c) pthread_cond_signal(c)
69-
#define git_cond_broadcast(c) pthread_cond_broadcast(c)
70-
71-
/* Pthread (-ish) rwlock
72-
*
73-
* This differs from normal pthreads rwlocks in two ways:
74-
* 1. Separate APIs for releasing read locks and write locks (as
75-
* opposed to the pure POSIX API which only has one unlock fn)
76-
* 2. You should not use recursive read locks (i.e. grabbing a read
77-
* lock in a thread that already holds a read lock) because the
78-
* Windows implementation doesn't support it
79-
*/
80-
#define git_rwlock pthread_rwlock_t
81-
#define git_rwlock_init(a) pthread_rwlock_init(a, NULL)
82-
#define git_rwlock_rdlock(a) pthread_rwlock_rdlock(a)
83-
#define git_rwlock_rdunlock(a) pthread_rwlock_rdunlock(a)
84-
#define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a)
85-
#define git_rwlock_wrunlock(a) pthread_rwlock_wrunlock(a)
86-
#define git_rwlock_free(a) pthread_rwlock_destroy(a)
87-
#define GIT_RWLOCK_STATIC_INIT PTHREAD_RWLOCK_INITIALIZER
88-
89-
#ifndef GIT_WIN32
90-
#define pthread_rwlock_rdunlock pthread_rwlock_unlock
91-
#define pthread_rwlock_wrunlock pthread_rwlock_unlock
43+
#ifdef GIT_WIN32
44+
# include "win32/thread.h"
45+
#else
46+
# include "unix/pthread.h"
9247
#endif
9348

94-
9549
GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
9650
{
9751
#if defined(GIT_WIN32)
@@ -178,7 +132,7 @@ GIT_INLINE(int64_t) git_atomic64_add(git_atomic64 *a, int64_t addend)
178132
#else
179133

180134
#define git_thread unsigned int
181-
#define git_thread_create(thread, attr, start_routine, arg) 0
135+
#define git_thread_create(thread, start_routine, arg) 0
182136
#define git_thread_join(id, status) (void)0
183137

184138
/* Pthreads Mutex */

src/unix/pthread.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_threads_init() (void)0
16+
#define git_thread_create(git_thread_ptr, start_routine, arg) \
17+
pthread_create(&(git_thread_ptr)->thread, NULL, start_routine, arg)
18+
#define git_thread_join(git_thread_ptr, status) \
19+
pthread_join((git_thread_ptr)->thread, status)
20+
21+
/* Git Mutex */
22+
#define git_mutex pthread_mutex_t
23+
#define git_mutex_init(a) pthread_mutex_init(a, NULL)
24+
#define git_mutex_lock(a) pthread_mutex_lock(a)
25+
#define git_mutex_unlock(a) pthread_mutex_unlock(a)
26+
#define git_mutex_free(a) pthread_mutex_destroy(a)
27+
28+
/* Git condition vars */
29+
#define git_cond pthread_cond_t
30+
#define git_cond_init(c) pthread_cond_init(c, NULL)
31+
#define git_cond_free(c) pthread_cond_destroy(c)
32+
#define git_cond_wait(c, l) pthread_cond_wait(c, l)
33+
#define git_cond_signal(c) pthread_cond_signal(c)
34+
#define git_cond_broadcast(c) pthread_cond_broadcast(c)
35+
36+
/* Pthread (-ish) rwlock
37+
*
38+
* This differs from normal pthreads rwlocks in two ways:
39+
* 1. Separate APIs for releasing read locks and write locks (as
40+
* opposed to the pure POSIX API which only has one unlock fn)
41+
* 2. You should not use recursive read locks (i.e. grabbing a read
42+
* lock in a thread that already holds a read lock) because the
43+
* Windows implementation doesn't support it
44+
*/
45+
#define git_rwlock pthread_rwlock_t
46+
#define git_rwlock_init(a) pthread_rwlock_init(a, NULL)
47+
#define git_rwlock_rdlock(a) pthread_rwlock_rdlock(a)
48+
#define git_rwlock_rdunlock(a) pthread_rwlock_unlock(a)
49+
#define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a)
50+
#define git_rwlock_wrunlock(a) pthread_rwlock_unlock(a)
51+
#define git_rwlock_free(a) pthread_rwlock_destroy(a)
52+
#define GIT_RWLOCK_STATIC_INIT PTHREAD_RWLOCK_INITIALIZER
53+
54+
#endif /* INCLUDE_unix_pthread_h__ */

src/win32/precompiled.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <io.h>
1717
#include <direct.h>
1818
#ifdef GIT_THREADS
19-
#include "win32/pthread.h"
19+
#include "win32/thread.h"
2020
#endif
2121

2222
#include "git2.h"

src/win32/pthread.h

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)