Introduce reader_test_should_cancel

This centralizes the logic around cancellation due to either sigint or a
stale threaded operation.
This commit is contained in:
ridiculousfish
2019-09-21 13:07:19 -07:00
parent 95aea7839d
commit 3f2f44ce73
5 changed files with 17 additions and 27 deletions

View File

@@ -1278,9 +1278,7 @@ bool completer_t::try_complete_user(const wcstring &str) {
setpwent();
// cppcheck-suppress getpwentCalled
while (struct passwd *pw = getpwent()) {
bool interrupted =
is_main_thread() ? reader_test_and_clear_interrupted() : reader_thread_job_is_stale();
if (interrupted) {
if (reader_test_should_cancel()) {
break;
}
const wcstring pw_name_str = str2wcstring(pw->pw_name);
@@ -1670,9 +1668,7 @@ static void append_switch(wcstring &out, const wcstring opt, const wcstring arg)
if (arg.empty()) return;
append_format(out, L" --%ls %ls", opt.c_str(), escape_string(arg, ESCAPE_ALL).c_str());
}
static void append_switch(wcstring &out, wchar_t opt) {
append_format(out, L" -%lc", opt);
}
static void append_switch(wcstring &out, wchar_t opt) { append_format(out, L" -%lc", opt); }
static void append_switch(wcstring &out, const wcstring opt) {
append_format(out, L" --%ls", opt.c_str());
}

View File

@@ -579,11 +579,10 @@ bool history_search_t::go_backwards() {
const size_t max_index = (size_t)-1;
if (current_index_ == max_index) return false;
const bool main_thread = is_main_thread();
size_t index = current_index_;
while (++index < max_index) {
if (main_thread ? reader_test_and_clear_interrupted() : reader_thread_job_is_stale()) {
if (reader_test_should_cancel()) {
return false;
}

View File

@@ -836,7 +836,13 @@ void reader_data_t::repaint_if_needed() {
void reader_reset_interrupted() { interrupted = 0; }
bool reader_test_interrupted() { return interrupted != 0; }
bool reader_test_should_cancel() {
if (is_main_thread()) {
return interrupted;
} else {
return read_generation_count() != s_thread_generation;
}
}
bool reader_test_and_clear_interrupted() {
int res = interrupted;
@@ -846,11 +852,6 @@ bool reader_test_and_clear_interrupted() {
return res != 0;
}
bool reader_thread_job_is_stale() {
ASSERT_IS_BACKGROUND_THREAD();
return read_generation_count() != s_thread_generation;
}
void reader_write_title(const wcstring &cmd, parser_t &parser, bool reset_cursor_position) {
if (!term_supports_setting_title()) return;
@@ -1275,7 +1276,7 @@ static std::function<autosuggestion_result_t(void)> get_autosuggestion_performer
}
history_search_t searcher(*history, search_string, HISTORY_SEARCH_TYPE_PREFIX);
while (!reader_thread_job_is_stale() && searcher.go_backwards()) {
while (!reader_test_should_cancel() && searcher.go_backwards()) {
history_item_t item = searcher.current_item();
// Skip items with newlines because they make terrible autosuggestions.
@@ -1288,7 +1289,7 @@ static std::function<autosuggestion_result_t(void)> get_autosuggestion_performer
}
// Maybe cancel here.
if (reader_thread_job_is_stale()) return nothing;
if (reader_test_should_cancel()) return nothing;
// Here we do something a little funny. If the line ends with a space, and the cursor is not
// at the end, don't use completion autosuggestions. It ends up being pretty weird seeing

View File

@@ -115,8 +115,10 @@ size_t reader_get_cursor_pos();
/// selection, true otherwise.
bool reader_get_selection(size_t *start, size_t *len);
/// Return the value of the interrupted flag, which is set by the sigint handler.
bool reader_test_interrupted();
/// 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.
@@ -131,11 +133,6 @@ void reader_reset_interrupted();
/// was set. If the current reader is interruptible, call \c reader_exit().
int reader_reading_interrupted();
/// Returns true if the current reader generation count does not equal the generation count the
/// current thread was started with. Note 1: currently only valid for autocompletion threads! Other
/// threads don't set the threadlocal generation count when they start up.
bool reader_thread_job_is_stale();
/// Read one line of input. Before calling this function, reader_push() must have been called in
/// order to set up a valid reader environment. If nchars > 0, return after reading that many
/// characters even if a full line has not yet been read. Note: the returned value may be longer

View File

@@ -499,10 +499,7 @@ class wildcard_expander_t {
/// Indicate whether we should cancel wildcard expansion. This latches 'interrupt'.
bool interrupted() {
if (!did_interrupt) {
did_interrupt = (is_main_thread() ? reader_test_and_clear_interrupted()
: reader_thread_job_is_stale());
}
did_interrupt = did_interrupt || reader_test_should_cancel();
return did_interrupt;
}