Add SRWLock implementation of rwlocks for Win32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
diff --git a/src/thread-utils.h b/src/thread-utils.h
index ffcdb4a..819e24e 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -66,12 +66,17 @@ typedef git_atomic git_atomic_ssize;
#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_rdunlock(a) pthread_rwlock_rdunlock(a)
#define git_rwlock_wrlock(a) pthread_rwlock_wrlock(a)
-#define git_rwlock_wrunlock(a) pthread_rwlock_unlock(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)
{
diff --git a/src/win32/pthread.c b/src/win32/pthread.c
index 2f263b3..41cb7a4 100644
--- a/src/win32/pthread.c
+++ b/src/win32/pthread.c
@@ -142,3 +142,41 @@ int pthread_num_processors_np(void)
return n ? n : 1;
}
+int pthread_rwlock_init(
+ pthread_rwlock_t *GIT_RESTRICT lock,
+ const pthread_rwlockattr_t *GIT_RESTRICT attr)
+{
+ (void)attr;
+ InitializeSRWLock(lock);
+ return 0;
+}
+
+int pthread_rwlock_rdlock(pthread_rwlock_t *lock)
+{
+ AcquireSRWLockShared(lock);
+ return 0;
+}
+
+int pthread_rwlock_rdunlock(pthread_rwlock_t *lock)
+{
+ ReleaseSRWLockShared(lock);
+ return 0;
+}
+
+int pthread_rwlock_wrlock(pthread_rwlock_t *lock)
+{
+ AcquireSRWLockExclusive(lock);
+ return 0;
+}
+
+int pthread_rwlock_wrunlock(pthread_rwlock_t *lock)
+{
+ ReleaseSRWLockExclusive(lock);
+ return 0;
+}
+
+int pthread_rwlock_destroy(pthread_rwlock_t *lock)
+{
+ (void)lock;
+ return 0;
+}
diff --git a/src/win32/pthread.h b/src/win32/pthread.h
index 8277ecf..54e5286 100644
--- a/src/win32/pthread.h
+++ b/src/win32/pthread.h
@@ -19,11 +19,15 @@
typedef int pthread_mutexattr_t;
typedef int pthread_condattr_t;
typedef int pthread_attr_t;
+typedef int pthread_rwlockattr_t;
+
typedef CRITICAL_SECTION pthread_mutex_t;
typedef HANDLE pthread_t;
typedef HANDLE pthread_cond_t;
+typedef SRWLOCK pthread_rwlock_t;
-#define PTHREAD_MUTEX_INITIALIZER {(void*)-1};
+#define PTHREAD_MUTEX_INITIALIZER {(void*)-1}
+#define PTHREAD_RWLOCK_INITIALIZER SRWLOCK_INIT
int pthread_create(
pthread_t *GIT_RESTRICT,
@@ -47,4 +51,12 @@ int pthread_cond_signal(pthread_cond_t *);
int pthread_num_processors_np(void);
+int pthread_rwlock_init(
+ pthread_rwlock_t *GIT_RESTRICT, const pthread_rwlockattr_t *GIT_RESTRICT);
+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 *);
+
#endif
diff --git a/tests-clar/core/sortedcache.c b/tests-clar/core/sortedcache.c
index 509d0df..c1869be 100644
--- a/tests-clar/core/sortedcache.c
+++ b/tests-clar/core/sortedcache.c
@@ -128,7 +128,9 @@ void test_core_sortedcache__in_memory(void)
cl_assert_equal_i(30, item->value);
cl_assert(git_sortedcache_lookup(sc, "abc") == NULL);
- cl_git_pass(git_sortedcache_rlock(sc)); /* grab more than one */
+ /* not on Windows:
+ * cl_git_pass(git_sortedcache_rlock(sc)); -- grab more than one
+ */
cl_assert((item = git_sortedcache_entry(sc, 0)) != NULL);
cl_assert_equal_s("aaa", item->path);
@@ -148,7 +150,7 @@ void test_core_sortedcache__in_memory(void)
cl_assert(git_sortedcache_entry(sc, 5) == NULL);
git_sortedcache_runlock(sc);
- git_sortedcache_runlock(sc);
+ /* git_sortedcache_runlock(sc); */
cl_assert_equal_i(0, free_count);