threads: split up OS-dependent rwlock code

This commit is contained in:
Patrick Steinhardt 2016-06-20 17:49:47 +02:00 committed by Carlos Martín Nieto
parent fabd477125
commit 68343f26dd
4 changed files with 31 additions and 43 deletions

View File

@ -46,30 +46,6 @@ typedef git_atomic git_atomic_ssize;
# include "unix/pthread.h" # include "unix/pthread.h"
#endif #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) GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
{ {
#if defined(GIT_WIN32) #if defined(GIT_WIN32)

View File

@ -32,4 +32,22 @@ typedef struct {
#define git_cond_signal(c) pthread_cond_signal(c) #define git_cond_signal(c) pthread_cond_signal(c)
#define git_cond_broadcast(c) pthread_cond_broadcast(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__ */ #endif /* INCLUDE_unix_pthread_h__ */

View File

@ -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_acquire_exclusive;
static win32_srwlock_fn win32_srwlock_release_exclusive; static win32_srwlock_fn win32_srwlock_release_exclusive;
int pthread_rwlock_init( int git_rwlock_init(git_rwlock *GIT_RESTRICT lock)
pthread_rwlock_t *GIT_RESTRICT lock,
const pthread_rwlockattr_t *GIT_RESTRICT attr)
{ {
GIT_UNUSED(attr);
if (win32_srwlock_initialize) if (win32_srwlock_initialize)
win32_srwlock_initialize(&lock->native.srwl); win32_srwlock_initialize(&lock->native.srwl);
else else
@ -186,7 +182,7 @@ int pthread_rwlock_init(
return 0; return 0;
} }
int pthread_rwlock_rdlock(pthread_rwlock_t *lock) int git_rwlock_rdlock(git_rwlock *lock)
{ {
if (win32_srwlock_acquire_shared) if (win32_srwlock_acquire_shared)
win32_srwlock_acquire_shared(&lock->native.srwl); win32_srwlock_acquire_shared(&lock->native.srwl);
@ -196,7 +192,7 @@ int pthread_rwlock_rdlock(pthread_rwlock_t *lock)
return 0; return 0;
} }
int pthread_rwlock_rdunlock(pthread_rwlock_t *lock) int git_rwlock_rdunlock(git_rwlock *lock)
{ {
if (win32_srwlock_release_shared) if (win32_srwlock_release_shared)
win32_srwlock_release_shared(&lock->native.srwl); win32_srwlock_release_shared(&lock->native.srwl);
@ -206,7 +202,7 @@ int pthread_rwlock_rdunlock(pthread_rwlock_t *lock)
return 0; return 0;
} }
int pthread_rwlock_wrlock(pthread_rwlock_t *lock) int git_rwlock_wrlock(git_rwlock *lock)
{ {
if (win32_srwlock_acquire_exclusive) if (win32_srwlock_acquire_exclusive)
win32_srwlock_acquire_exclusive(&lock->native.srwl); win32_srwlock_acquire_exclusive(&lock->native.srwl);
@ -216,7 +212,7 @@ int pthread_rwlock_wrlock(pthread_rwlock_t *lock)
return 0; return 0;
} }
int pthread_rwlock_wrunlock(pthread_rwlock_t *lock) int git_rwlock_wrunlock(git_rwlock *lock)
{ {
if (win32_srwlock_release_exclusive) if (win32_srwlock_release_exclusive)
win32_srwlock_release_exclusive(&lock->native.srwl); win32_srwlock_release_exclusive(&lock->native.srwl);
@ -226,7 +222,7 @@ int pthread_rwlock_wrunlock(pthread_rwlock_t *lock)
return 0; return 0;
} }
int pthread_rwlock_destroy(pthread_rwlock_t *lock) int git_rwlock_free(git_rwlock *lock)
{ {
if (!win32_srwlock_initialize) if (!win32_srwlock_initialize)
DeleteCriticalSection(&lock->native.csec); DeleteCriticalSection(&lock->native.csec);

View File

@ -38,7 +38,7 @@ typedef struct {
GIT_SRWLOCK srwl; GIT_SRWLOCK srwl;
CRITICAL_SECTION csec; CRITICAL_SECTION csec;
} native; } native;
} pthread_rwlock_t; } git_rwlock;
#define PTHREAD_MUTEX_INITIALIZER {(void*)-1} #define PTHREAD_MUTEX_INITIALIZER {(void*)-1}
@ -59,14 +59,12 @@ int git_cond_signal(git_cond *);
int pthread_num_processors_np(void); int pthread_num_processors_np(void);
int pthread_rwlock_init( int git_rwlock_init(git_rwlock *GIT_RESTRICT lock);
pthread_rwlock_t *GIT_RESTRICT lock, int git_rwlock_rdlock(git_rwlock *);
const pthread_rwlockattr_t *GIT_RESTRICT attr); int git_rwlock_rdunlock(git_rwlock *);
int pthread_rwlock_rdlock(pthread_rwlock_t *); int git_rwlock_wrlock(git_rwlock *);
int pthread_rwlock_rdunlock(pthread_rwlock_t *); int git_rwlock_wrunlock(git_rwlock *);
int pthread_rwlock_wrlock(pthread_rwlock_t *); int git_rwlock_free(git_rwlock *);
int pthread_rwlock_wrunlock(pthread_rwlock_t *);
int pthread_rwlock_destroy(pthread_rwlock_t *);
extern int win32_pthread_initialize(void); extern int win32_pthread_initialize(void);