Skip to content

Commit 9374919

Browse files
j6tgitster
authored andcommitted
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>
1 parent a9a7463 commit 9374919

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

builtin-grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include "quote.h"
1717

1818
#ifndef NO_PTHREADS
19-
#include "thread-utils.h"
2019
#include <pthread.h>
20+
#include "thread-utils.h"
2121
#endif
2222

2323
static char const * const grep_usage[] = {

builtin-pack-objects.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include "refs.h"
1919

2020
#ifndef NO_PTHREADS
21-
#include "thread-utils.h"
2221
#include <pthread.h>
22+
#include "thread-utils.h"
2323
#endif
2424

2525
static const char pack_usage[] =
@@ -1586,7 +1586,7 @@ static pthread_cond_t progress_cond;
15861586
*/
15871587
static void init_threaded_search(void)
15881588
{
1589-
pthread_mutex_init(&read_mutex, NULL);
1589+
init_recursive_mutex(&read_mutex);
15901590
pthread_mutex_init(&cache_mutex, NULL);
15911591
pthread_mutex_init(&progress_mutex, NULL);
15921592
pthread_cond_init(&progress_cond, NULL);

compat/win32/pthread.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
*/
1919
#define pthread_mutex_t CRITICAL_SECTION
2020

21-
#define pthread_mutex_init(a,b) InitializeCriticalSection((a))
21+
#define pthread_mutex_init(a,b) (InitializeCriticalSection((a)), 0)
2222
#define pthread_mutex_destroy(a) DeleteCriticalSection((a))
2323
#define pthread_mutex_lock EnterCriticalSection
2424
#define pthread_mutex_unlock LeaveCriticalSection
2525

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+
2632
/*
2733
* Implement simple condition variable for Windows threads, based on ACE
2834
* implementation.

thread-utils.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "cache.h"
2+
#include <pthread.h>
23

34
#if defined(hpux) || defined(__hpux) || defined(_hpux)
45
# include <sys/pstat.h>
@@ -43,3 +44,18 @@ int online_cpus(void)
4344

4445
return 1;
4546
}
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#define THREAD_COMPAT_H
33

44
extern int online_cpus(void);
5+
extern int init_recursive_mutex(pthread_mutex_t*);
56

67
#endif /* THREAD_COMPAT_H */

0 commit comments

Comments
 (0)