From 70a75dc88aad2beb9c0007eba4af5154ebe58c8d Mon Sep 17 00:00:00 2001 From: Jan Kanis Date: Wed, 23 Jan 2013 00:19:29 +0100 Subject: [PATCH] implement reader_cancel_thread using __thread thread-local storage --- reader.cpp | 13 ++++++++++++- reader.h | 8 ++++++++ wildcard.cpp | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/reader.cpp b/reader.cpp index 07d58a31d..8fe3a66d9 100644 --- a/reader.cpp +++ b/reader.cpp @@ -181,7 +181,10 @@ commence. #define SEARCH_FORWARD 1 /* Any time the contents of a buffer changes, we update the generation count. This allows for our background highlighting thread to notice it and skip doing work that it would otherwise have to do. */ -static unsigned int s_generation_count; +static volatile unsigned int s_generation_count; + +/* This threadlocal 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 unsigned int thread_generation_count; /* A color is an int */ typedef int color_t; @@ -667,6 +670,12 @@ int reader_reading_interrupted() return res; } +bool reader_cancel_thread() +{ + ASSERT_IS_BACKGROUND_THREAD(); + return s_generation_count != thread_generation_count; +} + void reader_write_title() { const wchar_t *title; @@ -1225,6 +1234,8 @@ struct autosuggestion_context_t return 0; } + thread_generation_count = generation_count; + /* Let's make sure we aren't using the empty string */ if (search_string.empty()) { diff --git a/reader.h b/reader.h index 0942bad08..f26f27405 100644 --- a/reader.h +++ b/reader.h @@ -132,6 +132,14 @@ void reader_reset_interrupted(); */ 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: currently only valid for autocompletion threads! Other threads don't + set the threadlocal generation count when they start up. +*/ +bool reader_cancel_thread(); + /** Read one line of input. Before calling this function, reader_push() must have been called in order to set up a valid reader diff --git a/wildcard.cpp b/wildcard.cpp index 1241e5e73..b3d6e57aa 100644 --- a/wildcard.cpp +++ b/wildcard.cpp @@ -728,7 +728,7 @@ static int wildcard_expand_internal(const wchar_t *wc, // debug( 3, L"WILDCARD_EXPAND %ls in %ls", wc, base_dir ); - if (is_main_thread() && reader_interrupted()) + if (is_main_thread() ? reader_interrupted() : reader_cancel_thread()) { return -1; }