@@ -24,11 +24,11 @@ static char const * const grep_usage[] = {
24
24
NULL
25
25
};
26
26
27
- static int use_threads = 1 ;
27
+ #define GREP_NUM_THREADS_DEFAULT 8
28
+ static int num_threads ;
28
29
29
30
#ifndef NO_PTHREADS
30
- #define THREADS 8
31
- static pthread_t threads [THREADS ];
31
+ static pthread_t * threads ;
32
32
33
33
/* We use one producer thread and THREADS consumer
34
34
* threads. The producer adds struct work_items to 'todo' and the
@@ -63,13 +63,13 @@ static pthread_mutex_t grep_mutex;
63
63
64
64
static inline void grep_lock (void )
65
65
{
66
- if (use_threads )
66
+ if (num_threads )
67
67
pthread_mutex_lock (& grep_mutex );
68
68
}
69
69
70
70
static inline void grep_unlock (void )
71
71
{
72
- if (use_threads )
72
+ if (num_threads )
73
73
pthread_mutex_unlock (& grep_mutex );
74
74
}
75
75
@@ -206,7 +206,8 @@ static void start_threads(struct grep_opt *opt)
206
206
strbuf_init (& todo [i ].out , 0 );
207
207
}
208
208
209
- for (i = 0 ; i < ARRAY_SIZE (threads ); i ++ ) {
209
+ threads = xcalloc (num_threads , sizeof (* threads ));
210
+ for (i = 0 ; i < num_threads ; i ++ ) {
210
211
int err ;
211
212
struct grep_opt * o = grep_opt_dup (opt );
212
213
o -> output = strbuf_out ;
@@ -238,12 +239,14 @@ static int wait_all(void)
238
239
pthread_cond_broadcast (& cond_add );
239
240
grep_unlock ();
240
241
241
- for (i = 0 ; i < ARRAY_SIZE ( threads ) ; i ++ ) {
242
+ for (i = 0 ; i < num_threads ; i ++ ) {
242
243
void * h ;
243
244
pthread_join (threads [i ], & h );
244
245
hit |= (int ) (intptr_t ) h ;
245
246
}
246
247
248
+ free (threads );
249
+
247
250
pthread_mutex_destroy (& grep_mutex );
248
251
pthread_mutex_destroy (& grep_read_mutex );
249
252
pthread_mutex_destroy (& grep_attr_mutex );
@@ -267,6 +270,14 @@ static int grep_cmd_config(const char *var, const char *value, void *cb)
267
270
int st = grep_config (var , value , cb );
268
271
if (git_color_default_config (var , value , cb ) < 0 )
269
272
st = -1 ;
273
+
274
+ if (!strcmp (var , "grep.threads" )) {
275
+ num_threads = git_config_int (var , value );
276
+ if (num_threads < 0 )
277
+ die (_ ("invalid number of threads specified (%d) for %s" ),
278
+ num_threads , var );
279
+ }
280
+
270
281
return st ;
271
282
}
272
283
@@ -294,7 +305,7 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
294
305
}
295
306
296
307
#ifndef NO_PTHREADS
297
- if (use_threads ) {
308
+ if (num_threads ) {
298
309
add_work (opt , GREP_SOURCE_SHA1 , pathbuf .buf , path , sha1 );
299
310
strbuf_release (& pathbuf );
300
311
return 0 ;
@@ -323,7 +334,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
323
334
strbuf_addstr (& buf , filename );
324
335
325
336
#ifndef NO_PTHREADS
326
- if (use_threads ) {
337
+ if (num_threads ) {
327
338
add_work (opt , GREP_SOURCE_FILE , buf .buf , filename , filename );
328
339
strbuf_release (& buf );
329
340
return 0 ;
@@ -702,6 +713,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
702
713
N_ ("show <n> context lines before matches" )),
703
714
OPT_INTEGER ('A' , "after-context" , & opt .post_context ,
704
715
N_ ("show <n> context lines after matches" )),
716
+ OPT_INTEGER (0 , "threads" , & num_threads ,
717
+ N_ ("use <n> worker threads" )),
705
718
OPT_NUMBER_CALLBACK (& opt , N_ ("shortcut for -C NUM" ),
706
719
context_callback ),
707
720
OPT_BOOL ('p' , "show-function" , & opt .funcname ,
@@ -832,13 +845,17 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
832
845
833
846
#ifndef NO_PTHREADS
834
847
if (list .nr || cached || show_in_pager )
835
- use_threads = 0 ;
848
+ num_threads = 0 ;
849
+ else if (num_threads == 0 )
850
+ num_threads = GREP_NUM_THREADS_DEFAULT ;
851
+ else if (num_threads < 0 )
852
+ die (_ ("invalid number of threads specified (%d)" ), num_threads );
836
853
#else
837
- use_threads = 0 ;
854
+ num_threads = 0 ;
838
855
#endif
839
856
840
857
#ifndef NO_PTHREADS
841
- if (use_threads ) {
858
+ if (num_threads ) {
842
859
if (!(opt .name_only || opt .unmatch_name_only || opt .count )
843
860
&& (opt .pre_context || opt .post_context ||
844
861
opt .file_break || opt .funcbody ))
@@ -908,7 +925,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
908
925
hit = grep_objects (& opt , & pathspec , & list );
909
926
}
910
927
911
- if (use_threads )
928
+ if (num_threads )
912
929
hit |= wait_all ();
913
930
if (hit && show_in_pager )
914
931
run_pager (& opt , prefix );
0 commit comments