Skip to content

Commit b3aeb28

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 78db6ea commit b3aeb28

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

builtin/grep.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,6 @@ static inline void grep_unlock(void)
8585
pthread_mutex_unlock(&grep_mutex);
8686
}
8787

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-
10388
/* Signalled when a new work_item is added to todo. */
10489
static pthread_cond_t cond_add;
10590

@@ -254,7 +239,7 @@ static void start_threads(struct grep_opt *opt)
254239
int i;
255240

256241
pthread_mutex_init(&grep_mutex, NULL);
257-
pthread_mutex_init(&read_sha1_mutex, NULL);
242+
pthread_mutex_init(&grep_read_mutex, NULL);
258243
pthread_mutex_init(&grep_attr_mutex, NULL);
259244
pthread_cond_init(&cond_add, NULL);
260245
pthread_cond_init(&cond_write, NULL);
@@ -303,7 +288,7 @@ static int wait_all(void)
303288
}
304289

305290
pthread_mutex_destroy(&grep_mutex);
306-
pthread_mutex_destroy(&read_sha1_mutex);
291+
pthread_mutex_destroy(&grep_read_mutex);
307292
pthread_mutex_destroy(&grep_attr_mutex);
308293
pthread_cond_destroy(&cond_add);
309294
pthread_cond_destroy(&cond_write);
@@ -313,8 +298,6 @@ static int wait_all(void)
313298
return hit;
314299
}
315300
#else /* !NO_PTHREADS */
316-
#define read_sha1_lock()
317-
#define read_sha1_unlock()
318301

319302
static int wait_all(void)
320303
{
@@ -376,9 +359,9 @@ static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type
376359
{
377360
void *data;
378361

379-
read_sha1_lock();
362+
grep_read_lock();
380363
data = read_sha1_file(sha1, type, size);
381-
read_sha1_unlock();
364+
grep_read_unlock();
382365
return data;
383366
}
384367

@@ -617,10 +600,10 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
617600
struct strbuf base;
618601
int hit, len;
619602

620-
read_sha1_lock();
603+
grep_read_lock();
621604
data = read_object_with_reference(obj->sha1, tree_type,
622605
&size, NULL);
623-
read_sha1_unlock();
606+
grep_read_unlock();
624607

625608
if (!data)
626609
die(_("unable to read tree (%s)"), sha1_to_hex(obj->sha1));

grep.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,12 @@ static inline void grep_attr_unlock(void)
826826
if (grep_use_locks)
827827
pthread_mutex_unlock(&grep_attr_mutex);
828828
}
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+
829835
#else
830836
#define grep_attr_lock()
831837
#define grep_attr_unlock()

grep.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ extern int grep_threads_ok(const struct grep_opt *opt);
139139
*/
140140
extern int grep_use_locks;
141141
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()
142159
#endif
143160

144161
#endif

0 commit comments

Comments
 (0)