@@ -29,25 +29,12 @@ static int use_threads = 1;
29
29
#define THREADS 8
30
30
static pthread_t threads [THREADS ];
31
31
32
- static void * load_sha1 (const unsigned char * sha1 , unsigned long * size ,
33
- const char * name );
34
- static void * load_file (const char * filename , size_t * sz );
35
-
36
- enum work_type {WORK_SHA1 , WORK_FILE };
37
-
38
32
/* We use one producer thread and THREADS consumer
39
33
* threads. The producer adds struct work_items to 'todo' and the
40
34
* consumers pick work items from the same array.
41
35
*/
42
36
struct work_item {
43
- enum work_type type ;
44
- char * name ;
45
-
46
- /* if type == WORK_SHA1, then 'identifier' is a SHA1,
47
- * otherwise type == WORK_FILE, and 'identifier' is a NUL
48
- * terminated filename.
49
- */
50
- void * identifier ;
37
+ struct grep_source source ;
51
38
char done ;
52
39
struct strbuf out ;
53
40
};
@@ -85,21 +72,6 @@ static inline void grep_unlock(void)
85
72
pthread_mutex_unlock (& grep_mutex );
86
73
}
87
74
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
-
103
75
/* Signalled when a new work_item is added to todo. */
104
76
static pthread_cond_t cond_add ;
105
77
@@ -113,17 +85,18 @@ static pthread_cond_t cond_result;
113
85
114
86
static int skip_first_line ;
115
87
116
- static void add_work (enum work_type type , char * name , void * id )
88
+ static void add_work (struct grep_opt * opt , enum grep_source_type type ,
89
+ const char * name , const void * id )
117
90
{
118
91
grep_lock ();
119
92
120
93
while ((todo_end + 1 ) % ARRAY_SIZE (todo ) == todo_done ) {
121
94
pthread_cond_wait (& cond_write , & grep_mutex );
122
95
}
123
96
124
- todo [todo_end ].type = type ;
125
- todo [ todo_end ]. name = name ;
126
- todo [todo_end ].identifier = id ;
97
+ grep_source_init ( & todo [todo_end ].source , type , name , id ) ;
98
+ if ( opt -> binary != GREP_BINARY_TEXT )
99
+ grep_source_load_driver ( & todo [todo_end ].source ) ;
127
100
todo [todo_end ].done = 0 ;
128
101
strbuf_reset (& todo [todo_end ].out );
129
102
todo_end = (todo_end + 1 ) % ARRAY_SIZE (todo );
@@ -151,21 +124,6 @@ static struct work_item *get_work(void)
151
124
return ret ;
152
125
}
153
126
154
- static void grep_sha1_async (struct grep_opt * opt , char * name ,
155
- const unsigned char * sha1 )
156
- {
157
- unsigned char * s ;
158
- s = xmalloc (20 );
159
- memcpy (s , sha1 , 20 );
160
- add_work (WORK_SHA1 , name , s );
161
- }
162
-
163
- static void grep_file_async (struct grep_opt * opt , char * name ,
164
- const char * filename )
165
- {
166
- add_work (WORK_FILE , name , xstrdup (filename ));
167
- }
168
-
169
127
static void work_done (struct work_item * w )
170
128
{
171
129
int old_done ;
@@ -192,8 +150,7 @@ static void work_done(struct work_item *w)
192
150
193
151
write_or_die (1 , p , len );
194
152
}
195
- free (w -> name );
196
- free (w -> identifier );
153
+ grep_source_clear (& w -> source );
197
154
}
198
155
199
156
if (old_done != todo_done )
@@ -216,25 +173,8 @@ static void *run(void *arg)
216
173
break ;
217
174
218
175
opt -> output_priv = w ;
219
- if (w -> type == WORK_SHA1 ) {
220
- unsigned long sz ;
221
- void * data = load_sha1 (w -> identifier , & sz , w -> name );
222
-
223
- if (data ) {
224
- hit |= grep_buffer (opt , w -> name , data , sz );
225
- free (data );
226
- }
227
- } else if (w -> type == WORK_FILE ) {
228
- size_t sz ;
229
- void * data = load_file (w -> identifier , & sz );
230
- if (data ) {
231
- hit |= grep_buffer (opt , w -> name , data , sz );
232
- free (data );
233
- }
234
- } else {
235
- assert (0 );
236
- }
237
-
176
+ hit |= grep_source (opt , & w -> source );
177
+ grep_source_clear_data (& w -> source );
238
178
work_done (w );
239
179
}
240
180
free_grep_patterns (arg );
@@ -254,11 +194,12 @@ static void start_threads(struct grep_opt *opt)
254
194
int i ;
255
195
256
196
pthread_mutex_init (& grep_mutex , NULL );
257
- pthread_mutex_init (& read_sha1_mutex , NULL );
197
+ pthread_mutex_init (& grep_read_mutex , NULL );
258
198
pthread_mutex_init (& grep_attr_mutex , NULL );
259
199
pthread_cond_init (& cond_add , NULL );
260
200
pthread_cond_init (& cond_write , NULL );
261
201
pthread_cond_init (& cond_result , NULL );
202
+ grep_use_locks = 1 ;
262
203
263
204
for (i = 0 ; i < ARRAY_SIZE (todo ); i ++ ) {
264
205
strbuf_init (& todo [i ].out , 0 );
@@ -302,17 +243,16 @@ static int wait_all(void)
302
243
}
303
244
304
245
pthread_mutex_destroy (& grep_mutex );
305
- pthread_mutex_destroy (& read_sha1_mutex );
246
+ pthread_mutex_destroy (& grep_read_mutex );
306
247
pthread_mutex_destroy (& grep_attr_mutex );
307
248
pthread_cond_destroy (& cond_add );
308
249
pthread_cond_destroy (& cond_write );
309
250
pthread_cond_destroy (& cond_result );
251
+ grep_use_locks = 0 ;
310
252
311
253
return hit ;
312
254
}
313
255
#else /* !NO_PTHREADS */
314
- #define read_sha1_lock ()
315
- #define read_sha1_unlock ()
316
256
317
257
static int wait_all (void )
318
258
{
@@ -371,29 +311,16 @@ static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type
371
311
{
372
312
void * data ;
373
313
374
- read_sha1_lock ();
314
+ grep_read_lock ();
375
315
data = read_sha1_file (sha1 , type , size );
376
- read_sha1_unlock ();
377
- return data ;
378
- }
379
-
380
- static void * load_sha1 (const unsigned char * sha1 , unsigned long * size ,
381
- const char * name )
382
- {
383
- enum object_type type ;
384
- void * data = lock_and_read_sha1_file (sha1 , & type , size );
385
-
386
- if (!data )
387
- error (_ ("'%s': unable to read %s" ), name , sha1_to_hex (sha1 ));
388
-
316
+ grep_read_unlock ();
389
317
return data ;
390
318
}
391
319
392
320
static int grep_sha1 (struct grep_opt * opt , const unsigned char * sha1 ,
393
321
const char * filename , int tree_name_len )
394
322
{
395
323
struct strbuf pathbuf = STRBUF_INIT ;
396
- char * name ;
397
324
398
325
if (opt -> relative && opt -> prefix_length ) {
399
326
quote_path_relative (filename + tree_name_len , -1 , & pathbuf ,
@@ -403,87 +330,51 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
403
330
strbuf_addstr (& pathbuf , filename );
404
331
}
405
332
406
- name = strbuf_detach (& pathbuf , NULL );
407
-
408
333
#ifndef NO_PTHREADS
409
334
if (use_threads ) {
410
- grep_sha1_async (opt , name , sha1 );
335
+ add_work (opt , GREP_SOURCE_SHA1 , pathbuf .buf , sha1 );
336
+ strbuf_release (& pathbuf );
411
337
return 0 ;
412
338
} else
413
339
#endif
414
340
{
341
+ struct grep_source gs ;
415
342
int hit ;
416
- unsigned long sz ;
417
- void * data = load_sha1 (sha1 , & sz , name );
418
- if (!data )
419
- hit = 0 ;
420
- else
421
- hit = grep_buffer (opt , name , data , sz );
422
-
423
- free (data );
424
- free (name );
425
- return hit ;
426
- }
427
- }
428
343
429
- static void * load_file (const char * filename , size_t * sz )
430
- {
431
- struct stat st ;
432
- char * data ;
433
- int i ;
344
+ grep_source_init (& gs , GREP_SOURCE_SHA1 , pathbuf .buf , sha1 );
345
+ strbuf_release (& pathbuf );
346
+ hit = grep_source (opt , & gs );
434
347
435
- if (lstat (filename , & st ) < 0 ) {
436
- err_ret :
437
- if (errno != ENOENT )
438
- error (_ ("'%s': %s" ), filename , strerror (errno ));
439
- return NULL ;
440
- }
441
- if (!S_ISREG (st .st_mode ))
442
- return NULL ;
443
- * sz = xsize_t (st .st_size );
444
- i = open (filename , O_RDONLY );
445
- if (i < 0 )
446
- goto err_ret ;
447
- data = xmalloc (* sz + 1 );
448
- if (st .st_size != read_in_full (i , data , * sz )) {
449
- error (_ ("'%s': short read %s" ), filename , strerror (errno ));
450
- close (i );
451
- free (data );
452
- return NULL ;
348
+ grep_source_clear (& gs );
349
+ return hit ;
453
350
}
454
- close (i );
455
- data [* sz ] = 0 ;
456
- return data ;
457
351
}
458
352
459
353
static int grep_file (struct grep_opt * opt , const char * filename )
460
354
{
461
355
struct strbuf buf = STRBUF_INIT ;
462
- char * name ;
463
356
464
357
if (opt -> relative && opt -> prefix_length )
465
358
quote_path_relative (filename , -1 , & buf , opt -> prefix );
466
359
else
467
360
strbuf_addstr (& buf , filename );
468
- name = strbuf_detach (& buf , NULL );
469
361
470
362
#ifndef NO_PTHREADS
471
363
if (use_threads ) {
472
- grep_file_async (opt , name , filename );
364
+ add_work (opt , GREP_SOURCE_FILE , buf .buf , filename );
365
+ strbuf_release (& buf );
473
366
return 0 ;
474
367
} else
475
368
#endif
476
369
{
370
+ struct grep_source gs ;
477
371
int hit ;
478
- size_t sz ;
479
- void * data = load_file (filename , & sz );
480
- if (!data )
481
- hit = 0 ;
482
- else
483
- hit = grep_buffer (opt , name , data , sz );
484
372
485
- free (data );
486
- free (name );
373
+ grep_source_init (& gs , GREP_SOURCE_FILE , buf .buf , filename );
374
+ strbuf_release (& buf );
375
+ hit = grep_source (opt , & gs );
376
+
377
+ grep_source_clear (& gs );
487
378
return hit ;
488
379
}
489
380
}
@@ -612,10 +503,10 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
612
503
struct strbuf base ;
613
504
int hit , len ;
614
505
615
- read_sha1_lock ();
506
+ grep_read_lock ();
616
507
data = read_object_with_reference (obj -> sha1 , tree_type ,
617
508
& size , NULL );
618
- read_sha1_unlock ();
509
+ grep_read_unlock ();
619
510
620
511
if (!data )
621
512
die (_ ("unable to read tree (%s)" ), sha1_to_hex (obj -> sha1 ));
@@ -1027,8 +918,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1027
918
use_threads = 0 ;
1028
919
#endif
1029
920
1030
- opt .use_threads = use_threads ;
1031
-
1032
921
#ifndef NO_PTHREADS
1033
922
if (use_threads ) {
1034
923
if (!(opt .name_only || opt .unmatch_name_only || opt .count )
0 commit comments