Commit 6551004f by Patrick Steinhardt

threads: split up OS-dependent rwlock code

parent 139bffa0
......@@ -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)
......
......@@ -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__ */
......@@ -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);
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment