Skip to content

Commit cdf0553

Browse files
dschogitster
authored andcommitted
git grep: be careful to use mutexes only when they are initialized
Rather nasty things happen when a mutex is not initialized but locked nevertheless. Now, when we're not running in a threaded manner, the mutex is not initialized, which is correct. But then we went and used the mutex anyway, which -- at least on Windows -- leads to a hard crash (ordinarily it would be called a segmentation fault, but in Windows speak it is an access violation). This problem was identified by our faithful tests when run in the msysGit environment. To avoid having to wrap the line due to the 80 column limit, we use the name "WHEN_THREADED" instead of "IF_USE_THREADS" because it is one character shorter. Which is all we need in this case. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f384a2e commit cdf0553

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

builtin/grep.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ static pthread_mutex_t grep_mutex;
7777
/* Used to serialize calls to read_sha1_file. */
7878
static pthread_mutex_t read_sha1_mutex;
7979

80-
#define grep_lock() pthread_mutex_lock(&grep_mutex)
81-
#define grep_unlock() pthread_mutex_unlock(&grep_mutex)
82-
#define read_sha1_lock() pthread_mutex_lock(&read_sha1_mutex)
83-
#define read_sha1_unlock() pthread_mutex_unlock(&read_sha1_mutex)
80+
#define WHEN_THREADED(x) do { if (use_threads) (x); } while (0)
81+
#define grep_lock() WHEN_THREADED(pthread_mutex_lock(&grep_mutex))
82+
#define grep_unlock() WHEN_THREADED(pthread_mutex_unlock(&grep_mutex))
83+
#define read_sha1_lock() WHEN_THREADED(pthread_mutex_lock(&read_sha1_mutex))
84+
#define read_sha1_unlock() WHEN_THREADED(pthread_mutex_unlock(&read_sha1_mutex))
8485

8586
/* Signalled when a new work_item is added to todo. */
8687
static pthread_cond_t cond_add;

0 commit comments

Comments
 (0)