Thread-safe xmalloc and xrealloc needs a recursive mutex
The mutex used to protect object access (read_mutex) may need to be acquired recursively. Introduce init_recursive_mutex() helper function in thread-utils.c that constructs a mutex with the PHREAD_MUTEX_RECURSIVE attribute. pthread_mutex_init() emulation on Win32 is already recursive as it is implemented on top of the CRITICAL_SECTION type, which is recursive. http://msdn.microsoft.com/en-us/library/ms682530%28VS.85%29.aspx Add do-nothing compatibility wrappers for pthread_mutexattr* functions. Initial-version-by: Fredrik Kuivinen <frekui@gmail.com> Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Sixt committed
Apr 8, 2010 at 09:15 UTC
937491944292fa3303b565b9bd8914c6b644ab13
5 files changed
+27
-4
builtin-grep.c
+1
-1
@@ -16,8 +16,8 @@
16
#include "quote.h"
17
18
#ifndef NO_PTHREADS
19
-#include "thread-utils.h"
19
#include <pthread.h>
20
+#include "thread-utils.h"
21
#endif
22
23
static char const * const grep_usage[] = {
builtin-pack-objects.c
+2
-2
@@ -18,8 +18,8 @@
18
#include "refs.h"
19
20
#ifndef NO_PTHREADS
21
-#include "thread-utils.h"
21
#include <pthread.h>
22
+#include "thread-utils.h"
23
#endif
24
25
static const char pack_usage[] =
@@ -1586,7 +1586,7 @@ static pthread_cond_t progress_cond;
1586
*/
1587
static void init_threaded_search(void)
1588
{
1589
- pthread_mutex_init(&read_mutex, NULL);
1589
+ init_recursive_mutex(&read_mutex);
1590
pthread_mutex_init(&cache_mutex, NULL);
1591
pthread_mutex_init(&progress_mutex, NULL);
1592
pthread_cond_init(&progress_cond, NULL);
compat/win32/pthread.h
+7
-1
@@ -18,11 +18,17 @@
18
*/
19
#define pthread_mutex_t CRITICAL_SECTION
20
21
-#define pthread_mutex_init(a,b) InitializeCriticalSection((a))
21
+#define pthread_mutex_init(a,b) (InitializeCriticalSection((a)), 0)
22
#define pthread_mutex_destroy(a) DeleteCriticalSection((a))
23
#define pthread_mutex_lock EnterCriticalSection
24
#define pthread_mutex_unlock LeaveCriticalSection
25
26
+typedef int pthread_mutexattr_t;
27
+#define pthread_mutexattr_init(a) (*(a) = 0)
28
+#define pthread_mutexattr_destroy(a) do {} while (0)
29
+#define pthread_mutexattr_settype(a, t) 0
30
+#define PTHREAD_MUTEX_RECURSIVE 0
31
+
32
/*
33
* Implement simple condition variable for Windows threads, based on ACE
34
* implementation.
thread-utils.c
+16
@@ -1,4 +1,5 @@
1
#include "cache.h"
2
+#include <pthread.h>
3
4
#if defined(hpux) || defined(__hpux) || defined(_hpux)
5
# include <sys/pstat.h>
@@ -43,3 +44,18 @@ int online_cpus(void)
44
45
return 1;
46
}
47
+
48
+int init_recursive_mutex(pthread_mutex_t *m)
49
+{
50
+ pthread_mutexattr_t a;
51
+ int ret;
52
+
53
+ ret = pthread_mutexattr_init(&a);
54
+ if (!ret) {
55
+ ret = pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE);
56
+ if (!ret)
57
+ ret = pthread_mutex_init(m, &a);
58
+ pthread_mutexattr_destroy(&a);
59
+ }
60
+ return ret;
61
+}
thread-utils.h
+1
@@ -2,5 +2,6 @@
2
#define THREAD_COMPAT_H
3
4
extern int online_cpus(void);
5
+extern int init_recursive_mutex(pthread_mutex_t*);
6
7
#endif /* THREAD_COMPAT_H */