From 68343f26dd312a87a7e6139f541602bd759ea04a Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Mon, 20 Jun 2016 17:49:47 +0200 Subject: [PATCH] threads: split up OS-dependent rwlock code --- src/thread-utils.h | 24 ------------------------ src/unix/pthread.h | 18 ++++++++++++++++++ src/win32/pthread.c | 16 ++++++---------- src/win32/pthread.h | 16 +++++++--------- 4 files changed, 31 insertions(+), 43 deletions(-) diff --git a/src/thread-utils.h b/src/thread-utils.h index 1eb51de25..f75e44087 100644 --- a/src/thread-utils.h +++ b/src/thread-utils.h @@ -46,30 +46,6 @@ typedef git_atomic git_atomic_ssize; # include "unix/pthread.h" #endif -/* Pthread (-ish) rwlock - * - * This differs from normal pthreads rwlocks in two ways: - * 1. Separate APIs for releasing read locks and write locks (as - * opposed to the pure POSIX API which only has one unlock fn) - * 2. You should not use recursive read locks (i.e. grabbing a read - * lock in a thread that already holds a read lock) because the - * Windows implementation doesn't support it - */ -#define git_rwlock pthread_rwlock_t -#define git_rwlock_init(a) pthread_rwlock_init(a, NULL) -#define git_rwlock_rdlock(a) pthread_rwlock_rdlock(a) -#define git_rwlock_rdunlock(a) pthread_rwlock_rdunlock(a) -#define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a) -#define git_rwlock_wrunlock(a) pthread_rwlock_wrunlock(a) -#define git_rwlock_free(a) pthread_rwlock_destroy(a) -#define GIT_RWLOCK_STATIC_INIT PTHREAD_RWLOCK_INITIALIZER - -#ifndef GIT_WIN32 -#define pthread_rwlock_rdunlock pthread_rwlock_unlock -#define pthread_rwlock_wrunlock pthread_rwlock_unlock -#endif - - GIT_INLINE(void) git_atomic_set(git_atomic *a, int val) { #if defined(GIT_WIN32) diff --git a/src/unix/pthread.h b/src/unix/pthread.h index 0cba59b9c..773ce22f9 100644 --- a/src/unix/pthread.h +++ b/src/unix/pthread.h @@ -32,4 +32,22 @@ typedef struct { #define git_cond_signal(c) pthread_cond_signal(c) #define git_cond_broadcast(c) pthread_cond_broadcast(c) +/* Pthread (-ish) rwlock + * + * This differs from normal pthreads rwlocks in two ways: + * 1. Separate APIs for releasing read locks and write locks (as + * opposed to the pure POSIX API which only has one unlock fn) + * 2. You should not use recursive read locks (i.e. grabbing a read + * lock in a thread that already holds a read lock) because the + * Windows implementation doesn't support it + */ +#define git_rwlock pthread_rwlock_t +#define git_rwlock_init(a) pthread_rwlock_init(a, NULL) +#define git_rwlock_rdlock(a) pthread_rwlock_rdlock(a) +#define git_rwlock_rdunlock(a) pthread_rwlock_unlock(a) +#define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a) +#define git_rwlock_wrunlock(a) pthread_rwlock_unlock(a) +#define git_rwlock_free(a) pthread_rwlock_destroy(a) +#define GIT_RWLOCK_STATIC_INIT PTHREAD_RWLOCK_INITIALIZER + #endif /* INCLUDE_unix_pthread_h__ */ diff --git a/src/win32/pthread.c b/src/win32/pthread.c index 9ce062ab8..62a691dba 100644 --- a/src/win32/pthread.c +++ b/src/win32/pthread.c @@ -172,12 +172,8 @@ static win32_srwlock_fn win32_srwlock_release_shared; static win32_srwlock_fn win32_srwlock_acquire_exclusive; static win32_srwlock_fn win32_srwlock_release_exclusive; -int pthread_rwlock_init( - pthread_rwlock_t *GIT_RESTRICT lock, - const pthread_rwlockattr_t *GIT_RESTRICT attr) +int git_rwlock_init(git_rwlock *GIT_RESTRICT lock) { - GIT_UNUSED(attr); - if (win32_srwlock_initialize) win32_srwlock_initialize(&lock->native.srwl); else @@ -186,7 +182,7 @@ int pthread_rwlock_init( return 0; } -int pthread_rwlock_rdlock(pthread_rwlock_t *lock) +int git_rwlock_rdlock(git_rwlock *lock) { if (win32_srwlock_acquire_shared) win32_srwlock_acquire_shared(&lock->native.srwl); @@ -196,7 +192,7 @@ int pthread_rwlock_rdlock(pthread_rwlock_t *lock) return 0; } -int pthread_rwlock_rdunlock(pthread_rwlock_t *lock) +int git_rwlock_rdunlock(git_rwlock *lock) { if (win32_srwlock_release_shared) win32_srwlock_release_shared(&lock->native.srwl); @@ -206,7 +202,7 @@ int pthread_rwlock_rdunlock(pthread_rwlock_t *lock) return 0; } -int pthread_rwlock_wrlock(pthread_rwlock_t *lock) +int git_rwlock_wrlock(git_rwlock *lock) { if (win32_srwlock_acquire_exclusive) win32_srwlock_acquire_exclusive(&lock->native.srwl); @@ -216,7 +212,7 @@ int pthread_rwlock_wrlock(pthread_rwlock_t *lock) return 0; } -int pthread_rwlock_wrunlock(pthread_rwlock_t *lock) +int git_rwlock_wrunlock(git_rwlock *lock) { if (win32_srwlock_release_exclusive) win32_srwlock_release_exclusive(&lock->native.srwl); @@ -226,7 +222,7 @@ int pthread_rwlock_wrunlock(pthread_rwlock_t *lock) return 0; } -int pthread_rwlock_destroy(pthread_rwlock_t *lock) +int git_rwlock_free(git_rwlock *lock) { if (!win32_srwlock_initialize) DeleteCriticalSection(&lock->native.csec); diff --git a/src/win32/pthread.h b/src/win32/pthread.h index 35cb810e7..ef9285500 100644 --- a/src/win32/pthread.h +++ b/src/win32/pthread.h @@ -38,7 +38,7 @@ typedef struct { GIT_SRWLOCK srwl; CRITICAL_SECTION csec; } native; -} pthread_rwlock_t; +} git_rwlock; #define PTHREAD_MUTEX_INITIALIZER {(void*)-1} @@ -59,14 +59,12 @@ int git_cond_signal(git_cond *); int pthread_num_processors_np(void); -int pthread_rwlock_init( - pthread_rwlock_t *GIT_RESTRICT lock, - const pthread_rwlockattr_t *GIT_RESTRICT attr); -int pthread_rwlock_rdlock(pthread_rwlock_t *); -int pthread_rwlock_rdunlock(pthread_rwlock_t *); -int pthread_rwlock_wrlock(pthread_rwlock_t *); -int pthread_rwlock_wrunlock(pthread_rwlock_t *); -int pthread_rwlock_destroy(pthread_rwlock_t *); +int git_rwlock_init(git_rwlock *GIT_RESTRICT lock); +int git_rwlock_rdlock(git_rwlock *); +int git_rwlock_rdunlock(git_rwlock *); +int git_rwlock_wrlock(git_rwlock *); +int git_rwlock_wrunlock(git_rwlock *); +int git_rwlock_free(git_rwlock *); extern int win32_pthread_initialize(void);