From 1978ac87a1ccdcec8581d03eda8c54d182e5a7e2 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Thu, 16 Jan 2020 15:11:24 -0800 Subject: [PATCH] Remove reader_test_should_cancel Use cancel_checker more pervasively. --- src/builtin_history.cpp | 3 ++- src/complete.cpp | 2 +- src/history.cpp | 18 +++++++++--------- src/history.h | 3 ++- src/reader.cpp | 13 ------------- src/reader.h | 5 ----- 6 files changed, 14 insertions(+), 30 deletions(-) diff --git a/src/builtin_history.cpp b/src/builtin_history.cpp index 4df5e30f4..6facf2bbb 100644 --- a/src/builtin_history.cpp +++ b/src/builtin_history.cpp @@ -249,7 +249,8 @@ int builtin_history(parser_t &parser, io_streams_t &streams, wchar_t **argv) { switch (opts.hist_cmd) { case HIST_SEARCH: { if (!history->search(opts.search_type, args, opts.show_time_format, opts.max_items, - opts.case_sensitive, opts.null_terminate, opts.reverse, streams)) { + opts.case_sensitive, opts.null_terminate, opts.reverse, + parser.cancel_checker(), streams)) { status = STATUS_CMD_ERROR; } break; diff --git a/src/complete.cpp b/src/complete.cpp index d4058368f..6133a15ff 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -1347,7 +1347,7 @@ static void walk_wrap_chain_recursive(const wcstring &command_line, source_range wrap_chain_visited_set_t *visited, size_t depth) { // Limit our recursion depth. This prevents cycles in the wrap chain graph from overflowing. if (depth > 24) return; - if (reader_test_should_cancel()) return; + if (cancel_checker()) return; // Extract command from the command line and invoke the receiver with it. wcstring command(command_line, command_range.start, command_range.length); diff --git a/src/history.cpp b/src/history.cpp index 822b0316d..9c53de01a 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -605,10 +605,6 @@ bool history_search_t::go_backwards() { size_t index = current_index_; while (++index < max_index) { - if (reader_test_should_cancel()) { - return false; - } - history_item_t item = history_->item_at_index(index); // We're done if it's empty or we cancelled. @@ -1344,10 +1340,11 @@ void history_t::save() { impl()->save(); } /// \p func returns true, continue the search; else stop it. static void do_1_history_search(history_t &hist, history_search_type_t search_type, const wcstring &search_string, bool case_sensitive, - const std::function &func) { + const std::function &func, + const cancel_checker_t &cancel_check) { history_search_t searcher = history_search_t(hist, search_string, search_type, case_sensitive ? 0 : history_search_ignore_case); - while (searcher.go_backwards()) { + while (!cancel_check() && searcher.go_backwards()) { if (!func(searcher.current_item())) { break; } @@ -1357,7 +1354,8 @@ static void do_1_history_search(history_t &hist, history_search_type_t search_ty // Searches history. bool history_t::search(history_search_type_t search_type, const wcstring_list_t &search_args, const wchar_t *show_time_format, size_t max_items, bool case_sensitive, - bool null_terminate, bool reverse, io_streams_t &streams) { + bool null_terminate, bool reverse, const cancel_checker_t &cancel_check, + io_streams_t &streams) { wcstring_list_t collected; wcstring formatted_record; size_t remaining = max_items; @@ -1379,14 +1377,16 @@ bool history_t::search(history_search_type_t search_type, const wcstring_list_t if (search_args.empty()) { // The user had no search terms; just append everything. - do_1_history_search(*this, history_search_type_t::match_everything, {}, false, func); + do_1_history_search(*this, history_search_type_t::match_everything, {}, false, func, + cancel_check); } else { for (const wcstring &search_string : search_args) { if (search_string.empty()) { streams.err.append_format(L"Searching for the empty string isn't allowed"); return false; } - do_1_history_search(*this, search_type, search_string, case_sensitive, func); + do_1_history_search(*this, search_type, search_string, case_sensitive, func, + cancel_check); } } diff --git a/src/history.h b/src/history.h index 6f7e30590..66aa52aea 100644 --- a/src/history.h +++ b/src/history.h @@ -172,7 +172,8 @@ class history_t { // Searches history. bool search(history_search_type_t search_type, const wcstring_list_t &search_args, const wchar_t *show_time_format, size_t max_items, bool case_sensitive, - bool null_terminate, bool reverse, io_streams_t &streams); + bool null_terminate, bool reverse, const cancel_checker_t &cancel_check, + io_streams_t &streams); // Irreversibly clears history. void clear(); diff --git a/src/reader.cpp b/src/reader.cpp index 86b98ccec..62c7d4938 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -127,10 +127,6 @@ enum class jump_precision_t { till, to }; /// background threads to notice it and skip doing work that they would otherwise have to do. static std::atomic s_generation; -/// This pthreads generation count is set when an autosuggestion background thread starts up, so it -/// can easily check if the work it is doing is no longer useful. -static thread_local unsigned s_thread_generation; - /// Helper to get the generation count static inline unsigned read_generation_count() { return s_generation.load(std::memory_order_relaxed); @@ -853,14 +849,6 @@ void reader_data_t::repaint_if_needed() { void reader_reset_interrupted() { interrupted = 0; } -bool reader_test_should_cancel() { - if (is_main_thread()) { - return interrupted; - } else { - return read_generation_count() != s_thread_generation; - } -} - int reader_test_and_clear_interrupted() { int res = interrupted; if (res) { @@ -1300,7 +1288,6 @@ static std::function get_autosuggestion_performer if (ctx.check_cancel()) { return nothing; } - s_thread_generation = generation_count; // Let's make sure we aren't using the empty string. if (search_string.empty()) { diff --git a/src/reader.h b/src/reader.h index b09e9a1fb..5f3127d56 100644 --- a/src/reader.h +++ b/src/reader.h @@ -117,11 +117,6 @@ size_t reader_get_cursor_pos(); /// selection, true otherwise. bool reader_get_selection(size_t *start, size_t *len); -/// Return whether we have been interrupted and should cancel the current operation. -/// This may be because we received a sigint, or because we are in a background thread -/// and the job is now stale. -bool reader_test_should_cancel(); - /// Return the value of the interrupted flag, which is set by the sigint handler, and clear it if it /// was set. In practice this will return 0 or SIGINT. int reader_test_and_clear_interrupted();