grep: move sha1-reading mutex into low-level code
The multi-threaded git-grep code needs to serialize access to the thread-unsafe read_sha1_file call. It does this with a mutex that is local to builtin/grep.c. Let's instead push this down into grep.c, where it can be used by both builtin/grep.c and grep.c. This will let us safely teach the low-level grep.c code tricks that involve reading from the object db. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Feb 2, 2012 at 03:18 UTC
b3aeb285d0ac1dcb4d578a61a68e08646f96501f
3 files changed
+29
-23
builtin/grep.c
+6
-23
@@ -85,21 +85,6 @@ static inline void grep_unlock(void)
85
pthread_mutex_unlock(&grep_mutex);
86
}
87
88
-/* Used to serialize calls to read_sha1_file. */
89
-static pthread_mutex_t read_sha1_mutex;
90
-
91
-static inline void read_sha1_lock(void)
92
-{
93
- if (use_threads)
94
- pthread_mutex_lock(&read_sha1_mutex);
95
-}
96
-
97
-static inline void read_sha1_unlock(void)
98
-{
99
- if (use_threads)
100
- pthread_mutex_unlock(&read_sha1_mutex);
101
-}
102
-
88
/* Signalled when a new work_item is added to todo. */
89
static pthread_cond_t cond_add;
90
@@ -254,7 +239,7 @@ static void start_threads(struct grep_opt *opt)
239
int i;
240
241
pthread_mutex_init(&grep_mutex, NULL);
257
- pthread_mutex_init(&read_sha1_mutex, NULL);
242
+ pthread_mutex_init(&grep_read_mutex, NULL);
243
pthread_mutex_init(&grep_attr_mutex, NULL);
244
pthread_cond_init(&cond_add, NULL);
245
pthread_cond_init(&cond_write, NULL);
@@ -303,7 +288,7 @@ static int wait_all(void)
288
}
289
290
pthread_mutex_destroy(&grep_mutex);
306
- pthread_mutex_destroy(&read_sha1_mutex);
291
+ pthread_mutex_destroy(&grep_read_mutex);
292
pthread_mutex_destroy(&grep_attr_mutex);
293
pthread_cond_destroy(&cond_add);
294
pthread_cond_destroy(&cond_write);
@@ -313,8 +298,6 @@ static int wait_all(void)
298
return hit;
299
}
300
#else /* !NO_PTHREADS */
316
-#define read_sha1_lock()
317
-#define read_sha1_unlock()
301
302
static int wait_all(void)
303
{
@@ -376,9 +359,9 @@ static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type
359
{
360
void *data;
361
379
- read_sha1_lock();
362
+ grep_read_lock();
363
data = read_sha1_file(sha1, type, size);
381
- read_sha1_unlock();
364
+ grep_read_unlock();
365
return data;
366
}
367
@@ -617,10 +600,10 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
600
struct strbuf base;
601
int hit, len;
602
620
- read_sha1_lock();
603
+ grep_read_lock();
604
data = read_object_with_reference(obj->sha1, tree_type,
605
&size, NULL);
623
- read_sha1_unlock();
606
+ grep_read_unlock();
607
608
if (!data)
609
die(_("unable to read tree (%s)"), sha1_to_hex(obj->sha1));
grep.c
+6
@@ -826,6 +826,12 @@ static inline void grep_attr_unlock(void)
826
if (grep_use_locks)
827
pthread_mutex_unlock(&grep_attr_mutex);
828
}
829
+
830
+/*
831
+ * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
832
+ */
833
+pthread_mutex_t grep_read_mutex;
834
+
835
#else
836
#define grep_attr_lock()
837
#define grep_attr_unlock()
grep.h
+17
@@ -139,6 +139,23 @@ extern int grep_threads_ok(const struct grep_opt *opt);
139
*/
140
extern int grep_use_locks;
141
extern pthread_mutex_t grep_attr_mutex;
142
+extern pthread_mutex_t grep_read_mutex;
143
+
144
+static inline void grep_read_lock(void)
145
+{
146
+ if (grep_use_locks)
147
+ pthread_mutex_lock(&grep_read_mutex);
148
+}
149
+
150
+static inline void grep_read_unlock(void)
151
+{
152
+ if (grep_use_locks)
153
+ pthread_mutex_unlock(&grep_read_mutex);
154
+}
155
+
156
+#else
157
+#define grep_read_lock()
158
+#define grep_read_unlock()
159
#endif
160
161
#endif