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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 2, 2012 at 03:18 UTC 78db6ea9dc1a872f9d07a36fe7aec700a5c963b9
3 files changed +13 -11
builtin/grep.c
+2 -2
@@ -259,6 +259,7 @@ static void start_threads(struct grep_opt *opt)
259 pthread_cond_init(&cond_add, NULL);
260 pthread_cond_init(&cond_write, NULL);
261 pthread_cond_init(&cond_result, NULL);
262 + grep_use_locks = 1;
263
264 for (i = 0; i < ARRAY_SIZE(todo); i++) {
265 strbuf_init(&todo[i].out, 0);
@@ -307,6 +308,7 @@ static int wait_all(void)
308 pthread_cond_destroy(&cond_add);
309 pthread_cond_destroy(&cond_write);
310 pthread_cond_destroy(&cond_result);
311 + grep_use_locks = 0;
312
313 return hit;
314 }
@@ -1030,8 +1032,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1032 use_threads = 0;
1033 #endif
1034
1033 - opt.use_threads = use_threads;
1034 -
1035 #ifndef NO_PTHREADS
1036 if (use_threads) {
1037 if (opt.pre_context || opt.post_context || opt.file_break ||
grep.c
+10 -8
@@ -807,26 +807,28 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
807 }
808
809 #ifndef NO_PTHREADS
810 +int grep_use_locks;
811 +
812 /*
813 * This lock protects access to the gitattributes machinery, which is
814 * not thread-safe.
815 */
816 pthread_mutex_t grep_attr_mutex;
817
816 -static inline void grep_attr_lock(struct grep_opt *opt)
818 +static inline void grep_attr_lock(void)
819 {
818 - if (opt->use_threads)
820 + if (grep_use_locks)
821 pthread_mutex_lock(&grep_attr_mutex);
822 }
823
822 -static inline void grep_attr_unlock(struct grep_opt *opt)
824 +static inline void grep_attr_unlock(void)
825 {
824 - if (opt->use_threads)
826 + if (grep_use_locks)
827 pthread_mutex_unlock(&grep_attr_mutex);
828 }
829 #else
828 -#define grep_attr_lock(opt)
829 -#define grep_attr_unlock(opt)
830 +#define grep_attr_lock()
831 +#define grep_attr_unlock()
832 #endif
833
834 static int match_funcname(struct grep_opt *opt, const char *name, char *bol, char *eol)
@@ -834,9 +836,9 @@ static int match_funcname(struct grep_opt *opt, const char *name, char *bol, cha
836 xdemitconf_t *xecfg = opt->priv;
837 if (xecfg && !xecfg->find_func) {
838 struct userdiff_driver *drv;
837 - grep_attr_lock(opt);
839 + grep_attr_lock();
840 drv = userdiff_find_by_path(name);
839 - grep_attr_unlock(opt);
841 + grep_attr_unlock();
842 if (drv && drv->funcname.pattern) {
843 const struct userdiff_funcname *pe = &drv->funcname;
844 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
grep.h
+1 -1
@@ -116,7 +116,6 @@ struct grep_opt {
116 int show_hunk_mark;
117 int file_break;
118 int heading;
119 - int use_threads;
119 void *priv;
120
121 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);
137 * Mutex used around access to the attributes machinery if
138 * opt->use_threads. Must be initialized/destroyed by callers!
139 */
140 +extern int grep_use_locks;
141 extern pthread_mutex_t grep_attr_mutex;
142 #endif
143