Skip to content

Commit 6551004

Browse files
committed
threads: split up OS-dependent rwlock code
1 parent 139bffa commit 6551004

File tree

4 files changed

+31
-43
lines changed

4 files changed

+31
-43
lines changed

src/thread-utils.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,6 @@ typedef git_atomic git_atomic_ssize;
4646
# include "unix/pthread.h"
4747
#endif
4848

49-
/* Pthread (-ish) rwlock
50-
*
51-
* This differs from normal pthreads rwlocks in two ways:
52-
* 1. Separate APIs for releasing read locks and write locks (as
53-
* opposed to the pure POSIX API which only has one unlock fn)
54-
* 2. You should not use recursive read locks (i.e. grabbing a read
55-
* lock in a thread that already holds a read lock) because the
56-
* Windows implementation doesn't support it
57-
*/
58-
#define git_rwlock pthread_rwlock_t
59-
#define git_rwlock_init(a) pthread_rwlock_init(a, NULL)
60-
#define git_rwlock_rdlock(a) pthread_rwlock_rdlock(a)
61-
#define git_rwlock_rdunlock(a) pthread_rwlock_rdunlock(a)
62-
#define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a)
63-
#define git_rwlock_wrunlock(a) pthread_rwlock_wrunlock(a)
64-
#define git_rwlock_free(a) pthread_rwlock_destroy(a)
65-
#define GIT_RWLOCK_STATIC_INIT PTHREAD_RWLOCK_INITIALIZER
66-
67-
#ifndef GIT_WIN32
68-
#define pthread_rwlock_rdunlock pthread_rwlock_unlock
69-
#define pthread_rwlock_wrunlock pthread_rwlock_unlock
70-
#endif
71-
72-
7349
GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
7450
{
7551
#if defined(GIT_WIN32)

src/unix/pthread.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,22 @@ typedef struct {
3232
#define git_cond_signal(c) pthread_cond_signal(c)
3333
#define git_cond_broadcast(c) pthread_cond_broadcast(c)
3434

35+
/* Pthread (-ish) rwlock
36+
*
37+
* This differs from normal pthreads rwlocks in two ways:
38+
* 1. Separate APIs for releasing read locks and write locks (as
39+
* opposed to the pure POSIX API which only has one unlock fn)
40+
* 2. You should not use recursive read locks (i.e. grabbing a read
41+
* lock in a thread that already holds a read lock) because the
42+
* Windows implementation doesn't support it
43+
*/
44+
#define git_rwlock pthread_rwlock_t
45+
#define git_rwlock_init(a) pthread_rwlock_init(a, NULL)
46+
#define git_rwlock_rdlock(a) pthread_rwlock_rdlock(a)
47+
#define git_rwlock_rdunlock(a) pthread_rwlock_unlock(a)
48+
#define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a)
49+
#define git_rwlock_wrunlock(a) pthread_rwlock_unlock(a)
50+
#define git_rwlock_free(a) pthread_rwlock_destroy(a)
51+
#define GIT_RWLOCK_STATIC_INIT PTHREAD_RWLOCK_INITIALIZER
52+
3553
#endif /* INCLUDE_unix_pthread_h__ */

src/win32/pthread.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,8 @@ static win32_srwlock_fn win32_srwlock_release_shared;
172172
static win32_srwlock_fn win32_srwlock_acquire_exclusive;
173173
static win32_srwlock_fn win32_srwlock_release_exclusive;
174174

175-
int pthread_rwlock_init(
176-
pthread_rwlock_t *GIT_RESTRICT lock,
177-
const pthread_rwlockattr_t *GIT_RESTRICT attr)
175+
int git_rwlock_init(git_rwlock *GIT_RESTRICT lock)
178176
{
179-
GIT_UNUSED(attr);
180-
181177
if (win32_srwlock_initialize)
182178
win32_srwlock_initialize(&lock->native.srwl);
183179
else
@@ -186,7 +182,7 @@ int pthread_rwlock_init(
186182
return 0;
187183
}
188184

189-
int pthread_rwlock_rdlock(pthread_rwlock_t *lock)
185+
int git_rwlock_rdlock(git_rwlock *lock)
190186
{
191187
if (win32_srwlock_acquire_shared)
192188
win32_srwlock_acquire_shared(&lock->native.srwl);
@@ -196,7 +192,7 @@ int pthread_rwlock_rdlock(pthread_rwlock_t *lock)
196192
return 0;
197193
}
198194

199-
int pthread_rwlock_rdunlock(pthread_rwlock_t *lock)
195+
int git_rwlock_rdunlock(git_rwlock *lock)
200196
{
201197
if (win32_srwlock_release_shared)
202198
win32_srwlock_release_shared(&lock->native.srwl);
@@ -206,7 +202,7 @@ int pthread_rwlock_rdunlock(pthread_rwlock_t *lock)
206202
return 0;
207203
}
208204

209-
int pthread_rwlock_wrlock(pthread_rwlock_t *lock)
205+
int git_rwlock_wrlock(git_rwlock *lock)
210206
{
211207
if (win32_srwlock_acquire_exclusive)
212208
win32_srwlock_acquire_exclusive(&lock->native.srwl);
@@ -216,7 +212,7 @@ int pthread_rwlock_wrlock(pthread_rwlock_t *lock)
216212
return 0;
217213
}
218214

219-
int pthread_rwlock_wrunlock(pthread_rwlock_t *lock)
215+
int git_rwlock_wrunlock(git_rwlock *lock)
220216
{
221217
if (win32_srwlock_release_exclusive)
222218
win32_srwlock_release_exclusive(&lock->native.srwl);
@@ -226,7 +222,7 @@ int pthread_rwlock_wrunlock(pthread_rwlock_t *lock)
226222
return 0;
227223
}
228224

229-
int pthread_rwlock_destroy(pthread_rwlock_t *lock)
225+
int git_rwlock_free(git_rwlock *lock)
230226
{
231227
if (!win32_srwlock_initialize)
232228
DeleteCriticalSection(&lock->native.csec);

src/win32/pthread.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef struct {
3838
GIT_SRWLOCK srwl;
3939
CRITICAL_SECTION csec;
4040
} native;
41-
} pthread_rwlock_t;
41+
} git_rwlock;
4242

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

@@ -59,14 +59,12 @@ int git_cond_signal(git_cond *);
5959

6060
int pthread_num_processors_np(void);
6161

62-
int pthread_rwlock_init(
63-
pthread_rwlock_t *GIT_RESTRICT lock,
64-
const pthread_rwlockattr_t *GIT_RESTRICT attr);
65-
int pthread_rwlock_rdlock(pthread_rwlock_t *);
66-
int pthread_rwlock_rdunlock(pthread_rwlock_t *);
67-
int pthread_rwlock_wrlock(pthread_rwlock_t *);
68-
int pthread_rwlock_wrunlock(pthread_rwlock_t *);
69-
int pthread_rwlock_destroy(pthread_rwlock_t *);
62+
int git_rwlock_init(git_rwlock *GIT_RESTRICT lock);
63+
int git_rwlock_rdlock(git_rwlock *);
64+
int git_rwlock_rdunlock(git_rwlock *);
65+
int git_rwlock_wrlock(git_rwlock *);
66+
int git_rwlock_wrunlock(git_rwlock *);
67+
int git_rwlock_free(git_rwlock *);
7068

7169
extern int win32_pthread_initialize(void);
7270

0 commit comments

Comments
 (0)