@matheusbsbs / matheus-bagliano / commits / 431d6e7bc8
Fork 0

grep: enable threading for context line printing

If context lines are to be printed, grep separates them with hunk marks ("--\n"). These marks are printed between matches from different files, too. They are not printed before the first file, though. Threading was disabled when context line printing was enabled because avoiding to print the mark before the first line was an unsolved synchronisation problem. This patch separates the code for printing hunk marks for the threaded and the unthreaded case, allowing threading to be turned on together with the common -ABC options. ->show_hunk_mark, which controls printing of hunk marks between files in show_line(), is now set in grep_buffer_1(), but only if some results have already been printed and threading is disabled. The threaded case is handled in work_done(). Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Mar 15, 2010 at 17:21 UTC 431d6e7bc85f693fe49a04142a8ab4d2d72b0257
2 files changed +18 -14
builtin/grep.c
+13 -2
@@ -96,6 +96,9 @@ static pthread_cond_t cond_write;
96 /* Signalled when we are finished with everything. */
97 static pthread_cond_t cond_result;
98
99 +static int print_hunk_marks_between_files;
100 +static int printed_something;
101 +
102 static void add_work(enum work_type type, char *name, void *id)
103 {
104 grep_lock();
@@ -159,7 +162,12 @@ static void work_done(struct work_item *w)
162 for(; todo[todo_done].done && todo_done != todo_start;
163 todo_done = (todo_done+1) % ARRAY_SIZE(todo)) {
164 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 + }
171 free(w->name);
172 free(w->identifier);
173 }
@@ -926,8 +934,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
934 if (online_cpus() == 1 || !grep_threads_ok(&opt))
935 use_threads = 0;
936
929 - if (use_threads)
937 + if (use_threads) {
938 + if (opt.pre_context || opt.post_context)
939 + print_hunk_marks_between_files = 1;
940 start_threads(&opt);
941 + }
942 #else
943 use_threads = 0;
944 #endif
grep.c
+5 -12
@@ -551,8 +551,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
551 if (opt->last_shown == 0) {
552 if (opt->show_hunk_mark)
553 opt->output(opt, "--\n", 3);
554 - else
555 - opt->show_hunk_mark = 1;
554 } else if (lno > opt->last_shown + 1)
555 opt->output(opt, "--\n", 3);
556 }
@@ -750,14 +748,6 @@ int grep_threads_ok(const struct grep_opt *opt)
748 !opt->name_only)
749 return 0;
750
753 - /* If we are showing hunk marks, we should not do it for the
754 - * first match. The synchronization problem we get for this
755 - * constraint is not yet solved, so we disable threading in
756 - * this case.
757 - */
758 - if (opt->pre_context || opt->post_context)
759 - return 0;
760 -
751 return 1;
752 }
753
@@ -779,11 +769,14 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
769 enum grep_context ctx = GREP_CONTEXT_HEAD;
770 xdemitconf_t xecfg;
771
782 - opt->last_shown = 0;
783 -
772 if (!opt->output)
773 opt->output = std_output;
774
775 + if (opt->last_shown && (opt->pre_context || opt->post_context) &&
776 + opt->output == std_output)
777 + opt->show_hunk_mark = 1;
778 + opt->last_shown = 0;
779 +
780 if (buffer_is_binary(buf, size)) {
781 switch (opt->binary) {
782 case GREP_BINARY_DEFAULT: