Skip to content

Commit 78db6ea

Browse files
peffgitster
authored andcommitted
grep: make locking flag global
The low-level grep code traditionally didn't care about threading, as it doesn't do any threading itself and didn't call out to other non-thread-safe code. That changed with 0579f91 (grep: enable threading with -p and -W using lazy attribute lookup, 2011-12-12), which pushed the lookup of funcname attributes (which is not thread-safe) into the low-level grep code. As a result, the low-level code learned about a new global "grep_attr_mutex" to serialize access to the attribute code. A multi-threaded caller (e.g., builtin/grep.c) is expected to initialize the mutex and set "use_threads" in the grep_opt structure. The low-level code only uses the lock if use_threads is set. However, putting the use_threads flag into the grep_opt struct is not the most logical place. Whether threading is in use is not something that matters for each call to grep_buffer, but is instead global to the whole program (i.e., if any thread is doing multi-threaded grep, every other thread, even if it thinks it is doing its own single-threaded grep, would need to use the locking). In practice, this distinction isn't a problem for us, because the only user of multi-threaded grep is "git-grep", which does nothing except call grep. This patch turns the opt->use_threads flag into a global flag. More important than the nit-picking semantic argument above is that this means that the locking functions don't need to actually have access to a grep_opt to know whether to lock. Which in turn can make adding new locks simpler, as we don't need to pass around a grep_opt. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 828ea97 commit 78db6ea

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

builtin/grep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ static void start_threads(struct grep_opt *opt)
259259
pthread_cond_init(&cond_add, NULL);
260260
pthread_cond_init(&cond_write, NULL);
261261
pthread_cond_init(&cond_result, NULL);
262+
grep_use_locks = 1;
262263

263264
for (i = 0; i < ARRAY_SIZE(todo); i++) {
264265
strbuf_init(&todo[i].out, 0);
@@ -307,6 +308,7 @@ static int wait_all(void)
307308
pthread_cond_destroy(&cond_add);
308309
pthread_cond_destroy(&cond_write);
309310
pthread_cond_destroy(&cond_result);
311+
grep_use_locks = 0;
310312

311313
return hit;
312314
}
@@ -1030,8 +1032,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10301032
use_threads = 0;
10311033
#endif
10321034

1033-
opt.use_threads = use_threads;
1034-
10351035
#ifndef NO_PTHREADS
10361036
if (use_threads) {
10371037
if (opt.pre_context || opt.post_context || opt.file_break ||

grep.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -807,36 +807,38 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
807807
}
808808

809809
#ifndef NO_PTHREADS
810+
int grep_use_locks;
811+
810812
/*
811813
* This lock protects access to the gitattributes machinery, which is
812814
* not thread-safe.
813815
*/
814816
pthread_mutex_t grep_attr_mutex;
815817

816-
static inline void grep_attr_lock(struct grep_opt *opt)
818+
static inline void grep_attr_lock(void)
817819
{
818-
if (opt->use_threads)
820+
if (grep_use_locks)
819821
pthread_mutex_lock(&grep_attr_mutex);
820822
}
821823

822-
static inline void grep_attr_unlock(struct grep_opt *opt)
824+
static inline void grep_attr_unlock(void)
823825
{
824-
if (opt->use_threads)
826+
if (grep_use_locks)
825827
pthread_mutex_unlock(&grep_attr_mutex);
826828
}
827829
#else
828-
#define grep_attr_lock(opt)
829-
#define grep_attr_unlock(opt)
830+
#define grep_attr_lock()
831+
#define grep_attr_unlock()
830832
#endif
831833

832834
static int match_funcname(struct grep_opt *opt, const char *name, char *bol, char *eol)
833835
{
834836
xdemitconf_t *xecfg = opt->priv;
835837
if (xecfg && !xecfg->find_func) {
836838
struct userdiff_driver *drv;
837-
grep_attr_lock(opt);
839+
grep_attr_lock();
838840
drv = userdiff_find_by_path(name);
839-
grep_attr_unlock(opt);
841+
grep_attr_unlock();
840842
if (drv && drv->funcname.pattern) {
841843
const struct userdiff_funcname *pe = &drv->funcname;
842844
xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);

grep.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ struct grep_opt {
116116
int show_hunk_mark;
117117
int file_break;
118118
int heading;
119-
int use_threads;
120119
void *priv;
121120

122121
void (*output)(struct grep_opt *opt, const void *data, size_t size);
@@ -138,6 +137,7 @@ extern int grep_threads_ok(const struct grep_opt *opt);
138137
* Mutex used around access to the attributes machinery if
139138
* opt->use_threads. Must be initialized/destroyed by callers!
140139
*/
140+
extern int grep_use_locks;
141141
extern pthread_mutex_t grep_attr_mutex;
142142
#endif
143143

0 commit comments

Comments
 (0)