diff options
author | Tom Tromey <tromey@adacore.com> | 2022-12-13 12:03:34 -0700 |
---|---|---|
committer | Tom Tromey <tromey@adacore.com> | 2023-01-17 07:06:56 -0700 |
commit | 6107546876df070688d75d8cd87d7c7a5f246091 (patch) | |
tree | 6092d20796d031fa4583af8c3f69dd4531626b8e /gdb/unittests | |
parent | 2ec9694617bc3a84ef89588d849eaef8893c0ec4 (diff) | |
download | gdb-6107546876df070688d75d8cd87d7c7a5f246091.zip gdb-6107546876df070688d75d8cd87d7c7a5f246091.tar.gz gdb-6107546876df070688d75d8cd87d7c7a5f246091.tar.bz2 |
Avoid submitting empty tasks in parallel_for_each
I found that parallel_for_each would submit empty tasks to the thread
pool. For example, this can happen if the number of tasks is smaller
than the number of available threads. In the DWARF reader, this
resulted in the cooked index containing empty sub-indices. This patch
arranges to instead shrink the result vector and process the trailing
entries in the calling thread.
(cherry picked from commit 63078a04984b73e1fdeb4571a63605ee5c13f929)
Diffstat (limited to 'gdb/unittests')
-rw-r--r-- | gdb/unittests/parallel-for-selftests.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/gdb/unittests/parallel-for-selftests.c b/gdb/unittests/parallel-for-selftests.c index 3162db1..15a095a 100644 --- a/gdb/unittests/parallel-for-selftests.c +++ b/gdb/unittests/parallel-for-selftests.c @@ -149,6 +149,45 @@ TEST (int n_threads) SELF_CHECK (counter == NUMBER); #undef NUMBER + + /* Check that if there are fewer tasks than threads, then we won't + end up with a null result. */ + std::vector<std::unique_ptr<int>> intresults; + std::atomic<bool> any_empty_tasks (false); + + FOR_EACH (1, 0, 1, + [&] (int start, int end) + { + if (start == end) + any_empty_tasks = true; + return std::unique_ptr<int> (new int (end - start)); + }); + SELF_CHECK (!any_empty_tasks); + SELF_CHECK (std::all_of (intresults.begin (), + intresults.end (), + [] (const std::unique_ptr<int> &entry) + { + return entry != nullptr; + })); + + /* The same but using the task size parameter. */ + intresults.clear (); + any_empty_tasks = false; + FOR_EACH (1, 0, 1, + [&] (int start, int end) + { + if (start == end) + any_empty_tasks = true; + return std::unique_ptr<int> (new int (end - start)); + }, + task_size_one); + SELF_CHECK (!any_empty_tasks); + SELF_CHECK (std::all_of (intresults.begin (), + intresults.end (), + [] (const std::unique_ptr<int> &entry) + { + return entry != nullptr; + })); } #endif /* FOR_EACH */ |