@matheusbsbs / matheus-bagliano / commits / 5f02d31597
Fork 0

Fix use of mutex in threaded grep

The program can decide at runtime not to use threading even if the support is compiled in. In such a case, mutexes are not necessary and left uninitialized. But the code incorrectly tried to take and release the read_sha1_mutex unconditionally. Signed-off-by: Junio C Hamano <gitster@pobox.com> Acked-by: Fredrik Kuivinen <frekui@gmail.com>

Junio C Hamano committed Feb 15, 2010 at 18:34 UTC 5f02d31597df35ca98e9080e12bfbb18dadadcbe
1 file changed +16 -9
builtin-grep.c
+16 -9
@@ -408,15 +408,25 @@ static int pathspec_matches(const char **paths, const char *name, int max_depth)
408 return 0;
409 }
410
411 +static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
412 +{
413 + void *data;
414 +
415 + if (use_threads) {
416 + read_sha1_lock();
417 + data = read_sha1_file(sha1, type, size);
418 + read_sha1_unlock();
419 + } else {
420 + data = read_sha1_file(sha1, type, size);
421 + }
422 + return data;
423 +}
424 +
425 static void *load_sha1(const unsigned char *sha1, unsigned long *size,
426 const char *name)
427 {
428 enum object_type type;
415 - char *data;
416 -
417 - read_sha1_lock();
418 - data = read_sha1_file(sha1, &type, size);
419 - read_sha1_unlock();
429 + void *data = lock_and_read_sha1_file(sha1, &type, size);
430
431 if (!data)
432 error("'%s': unable to read %s", name, sha1_to_hex(sha1));
@@ -605,10 +615,7 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
615 void *data;
616 unsigned long size;
617
608 - read_sha1_lock();
609 - data = read_sha1_file(entry.sha1, &type, &size);
610 - read_sha1_unlock();
611 -
618 + data = lock_and_read_sha1_file(entry.sha1, &type, &size);
619 if (!data)
620 die("unable to read tree (%s)",
621 sha1_to_hex(entry.sha1));