Skip to content

Commit 07b838f

Browse files
committed
Merge branch 'rs/threaded-grep-context'
* rs/threaded-grep-context: grep: enable threading for context line printing Conflicts: grep.c
2 parents d718dd0 + 431d6e7 commit 07b838f

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

builtin/grep.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ static pthread_cond_t cond_write;
9696
/* Signalled when we are finished with everything. */
9797
static pthread_cond_t cond_result;
9898

99+
static int print_hunk_marks_between_files;
100+
static int printed_something;
101+
99102
static void add_work(enum work_type type, char *name, void *id)
100103
{
101104
grep_lock();
@@ -159,7 +162,12 @@ static void work_done(struct work_item *w)
159162
for(; todo[todo_done].done && todo_done != todo_start;
160163
todo_done = (todo_done+1) % ARRAY_SIZE(todo)) {
161164
w = &todo[todo_done];
162-
write_or_die(1, w->out.buf, w->out.len);
165+
if (w->out.len) {
166+
if (print_hunk_marks_between_files && printed_something)
167+
write_or_die(1, "--\n", 3);
168+
write_or_die(1, w->out.buf, w->out.len);
169+
printed_something = 1;
170+
}
163171
free(w->name);
164172
free(w->identifier);
165173
}
@@ -946,8 +954,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
946954
if (online_cpus() == 1 || !grep_threads_ok(&opt))
947955
use_threads = 0;
948956

949-
if (use_threads)
957+
if (use_threads) {
958+
if (opt.pre_context || opt.post_context)
959+
print_hunk_marks_between_files = 1;
950960
start_threads(&opt);
961+
}
951962
#else
952963
use_threads = 0;
953964
#endif

grep.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,7 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
570570
if (opt->show_hunk_mark) {
571571
output_color(opt, "--", 2, opt->color_sep);
572572
opt->output(opt, "\n", 1);
573-
} else
574-
opt->show_hunk_mark = 1;
573+
}
575574
} else if (lno > opt->last_shown + 1) {
576575
output_color(opt, "--", 2, opt->color_sep);
577576
opt->output(opt, "\n", 1);
@@ -772,14 +771,6 @@ int grep_threads_ok(const struct grep_opt *opt)
772771
!opt->name_only)
773772
return 0;
774773

775-
/* If we are showing hunk marks, we should not do it for the
776-
* first match. The synchronization problem we get for this
777-
* constraint is not yet solved, so we disable threading in
778-
* this case.
779-
*/
780-
if (opt->pre_context || opt->post_context)
781-
return 0;
782-
783774
return 1;
784775
}
785776

@@ -801,11 +792,14 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
801792
enum grep_context ctx = GREP_CONTEXT_HEAD;
802793
xdemitconf_t xecfg;
803794

804-
opt->last_shown = 0;
805-
806795
if (!opt->output)
807796
opt->output = std_output;
808797

798+
if (opt->last_shown && (opt->pre_context || opt->post_context) &&
799+
opt->output == std_output)
800+
opt->show_hunk_mark = 1;
801+
opt->last_shown = 0;
802+
809803
if (buffer_is_binary(buf, size)) {
810804
switch (opt->binary) {
811805
case GREP_BINARY_DEFAULT:

0 commit comments

Comments
 (0)