Skip to content

Commit 1c13540

Browse files
committed
threads: split up OS-dependent mutex code
1 parent faebc1c commit 1c13540

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

src/thread-utils.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,12 @@ typedef git_atomic git_atomic_ssize;
4040

4141
#ifdef GIT_THREADS
4242

43-
#if !defined(GIT_WIN32)
43+
#ifdef GIT_WIN32
44+
# include "win32/pthread.h"
45+
#else
4446
# include "unix/pthread.h"
4547
#endif
4648

47-
/* Pthreads Mutex */
48-
#define git_mutex pthread_mutex_t
49-
#define git_mutex_init(a) pthread_mutex_init(a, NULL)
50-
#define git_mutex_lock(a) pthread_mutex_lock(a)
51-
#define git_mutex_unlock(a) pthread_mutex_unlock(a)
52-
#define git_mutex_free(a) pthread_mutex_destroy(a)
53-
5449
/* Pthreads condition vars */
5550
#define git_cond pthread_cond_t
5651
#define git_cond_init(c) pthread_cond_init(c, NULL)

src/unix/pthread.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ typedef struct {
1717
#define git_thread_join(git_thread_ptr, status) \
1818
pthread_join((git_thread_ptr)->thread, status)
1919

20+
/* Git Mutex */
21+
#define git_mutex pthread_mutex_t
22+
#define git_mutex_init(a) pthread_mutex_init(a, NULL)
23+
#define git_mutex_lock(a) pthread_mutex_lock(a)
24+
#define git_mutex_unlock(a) pthread_mutex_unlock(a)
25+
#define git_mutex_free(a) pthread_mutex_destroy(a)
26+
2027
#endif /* INCLUDE_unix_pthread_h__ */

src/win32/pthread.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,25 @@ int git_thread_join(
6767
return 0;
6868
}
6969

70-
int pthread_mutex_init(
71-
pthread_mutex_t *GIT_RESTRICT mutex,
72-
const pthread_mutexattr_t *GIT_RESTRICT mutexattr)
70+
int git_mutex_init(git_mutex *GIT_RESTRICT mutex)
7371
{
74-
GIT_UNUSED(mutexattr);
7572
InitializeCriticalSection(mutex);
7673
return 0;
7774
}
7875

79-
int pthread_mutex_destroy(pthread_mutex_t *mutex)
76+
int git_mutex_free(git_mutex *mutex)
8077
{
8178
DeleteCriticalSection(mutex);
8279
return 0;
8380
}
8481

85-
int pthread_mutex_lock(pthread_mutex_t *mutex)
82+
int git_mutex_lock(git_mutex *mutex)
8683
{
8784
EnterCriticalSection(mutex);
8885
return 0;
8986
}
9087

91-
int pthread_mutex_unlock(pthread_mutex_t *mutex)
88+
int git_mutex_unlock(git_mutex *mutex)
9289
{
9390
LeaveCriticalSection(mutex);
9491
return 0;
@@ -124,7 +121,7 @@ int pthread_cond_destroy(pthread_cond_t *cond)
124121
return 0;
125122
}
126123

127-
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
124+
int pthread_cond_wait(pthread_cond_t *cond, git_mutex *mutex)
128125
{
129126
int error;
130127
DWORD wait_result;
@@ -133,7 +130,7 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
133130
return EINVAL;
134131

135132
/* The caller must be holding the mutex. */
136-
error = pthread_mutex_unlock(mutex);
133+
error = git_mutex_unlock(mutex);
137134

138135
if (error)
139136
return error;
@@ -142,7 +139,7 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
142139
assert(WAIT_OBJECT_0 == wait_result);
143140
GIT_UNUSED(wait_result);
144141

145-
return pthread_mutex_lock(mutex);
142+
return git_mutex_lock(mutex);
146143
}
147144

148145
int pthread_cond_signal(pthread_cond_t *cond)

src/win32/pthread.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ typedef int pthread_condattr_t;
2828
typedef int pthread_attr_t;
2929
typedef int pthread_rwlockattr_t;
3030

31-
typedef CRITICAL_SECTION pthread_mutex_t;
31+
typedef CRITICAL_SECTION git_mutex;
3232
typedef HANDLE pthread_cond_t;
3333

3434
typedef struct { void *Ptr; } GIT_SRWLOCK;
@@ -47,16 +47,14 @@ int git_thread_create(git_thread *GIT_RESTRICT,
4747
void *GIT_RESTRICT);
4848
int git_thread_join(git_thread *, void **);
4949

50-
int pthread_mutex_init(
51-
pthread_mutex_t *GIT_RESTRICT mutex,
52-
const pthread_mutexattr_t *GIT_RESTRICT mutexattr);
53-
int pthread_mutex_destroy(pthread_mutex_t *);
54-
int pthread_mutex_lock(pthread_mutex_t *);
55-
int pthread_mutex_unlock(pthread_mutex_t *);
50+
int git_mutex_init(git_mutex *GIT_RESTRICT mutex);
51+
int git_mutex_free(git_mutex *);
52+
int git_mutex_lock(git_mutex *);
53+
int git_mutex_unlock(git_mutex *);
5654

5755
int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *);
5856
int pthread_cond_destroy(pthread_cond_t *);
59-
int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
57+
int pthread_cond_wait(pthread_cond_t *, git_mutex *);
6058
int pthread_cond_signal(pthread_cond_t *);
6159
/* pthread_cond_broadcast is not supported on Win32 yet. */
6260

0 commit comments

Comments
 (0)