From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Subject: [PATCH v4 08/19] Add gdb::task_group
Date: Sun, 10 Dec 2023 14:41:16 -0700 [thread overview]
Message-ID: <20231210-t-bg-dwarf-reading-v4-8-b978c32fd12f@tromey.com> (raw)
In-Reply-To: <20231210-t-bg-dwarf-reading-v4-0-b978c32fd12f@tromey.com>
This adds gdb::task_group, a convenient way to group background tasks
and then call a function when all the tasks have completed.
---
gdbsupport/Makefile.am | 1 +
gdbsupport/Makefile.in | 6 ++-
gdbsupport/task-group.cc | 97 ++++++++++++++++++++++++++++++++++++++++++++++++
gdbsupport/task-group.h | 64 ++++++++++++++++++++++++++++++++
4 files changed, 166 insertions(+), 2 deletions(-)
diff --git a/gdbsupport/Makefile.am b/gdbsupport/Makefile.am
index f1a641308fe..fdca034ce5e 100644
--- a/gdbsupport/Makefile.am
+++ b/gdbsupport/Makefile.am
@@ -79,6 +79,7 @@ libgdbsupport_a_SOURCES = \
search.cc \
signals.cc \
signals-state-save-restore.cc \
+ task-group.cc \
tdesc.cc \
thread-pool.cc \
xml-utils.cc \
diff --git a/gdbsupport/Makefile.in b/gdbsupport/Makefile.in
index 9fdc23c39a9..070e36a6aff 100644
--- a/gdbsupport/Makefile.in
+++ b/gdbsupport/Makefile.in
@@ -166,8 +166,8 @@ am_libgdbsupport_a_OBJECTS = agent.$(OBJEXT) btrace-common.$(OBJEXT) \
ptid.$(OBJEXT) rsp-low.$(OBJEXT) run-time-clock.$(OBJEXT) \
safe-strerror.$(OBJEXT) scoped_mmap.$(OBJEXT) search.$(OBJEXT) \
signals.$(OBJEXT) signals-state-save-restore.$(OBJEXT) \
- tdesc.$(OBJEXT) thread-pool.$(OBJEXT) xml-utils.$(OBJEXT) \
- $(am__objects_1) $(am__objects_2)
+ task-group.$(OBJEXT) tdesc.$(OBJEXT) thread-pool.$(OBJEXT) \
+ xml-utils.$(OBJEXT) $(am__objects_1) $(am__objects_2)
libgdbsupport_a_OBJECTS = $(am_libgdbsupport_a_OBJECTS)
AM_V_P = $(am__v_P_@AM_V@)
am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
@@ -436,6 +436,7 @@ libgdbsupport_a_SOURCES = \
search.cc \
signals.cc \
signals-state-save-restore.cc \
+ task-group.cc \
tdesc.cc \
thread-pool.cc \
xml-utils.cc \
@@ -546,6 +547,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/selftest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/signals-state-save-restore.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/signals.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/task-group.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tdesc.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/thread-pool.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xml-utils.Po@am__quote@
diff --git a/gdbsupport/task-group.cc b/gdbsupport/task-group.cc
new file mode 100644
index 00000000000..e6c8a6f2e31
--- /dev/null
+++ b/gdbsupport/task-group.cc
@@ -0,0 +1,97 @@
+/* Task group
+
+ Copyright (C) 2023 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "common-defs.h"
+#include "task-group.h"
+#include "thread-pool.h"
+
+namespace gdb
+{
+
+class task_group::impl : public std::enable_shared_from_this<task_group::impl>
+{
+public:
+
+ explicit impl (std::function<void ()> &&done)
+ : m_completed (0),
+ m_done (std::move (done))
+ { }
+ DISABLE_COPY_AND_ASSIGN (impl);
+
+ ~impl ()
+ {
+ if (m_started)
+ m_done ();
+ }
+
+ /* Add a task to the task group. */
+ void add_task (std::function<void ()> &&task)
+ {
+ m_tasks.push_back (std::move (task));
+ };
+
+ /* Start this task group. */
+ void start ();
+
+ /* True if started. */
+ bool m_started = false;
+ /* The tasks. */
+ std::vector<std::function<void ()>> m_tasks;
+ /* The number of tasks that have completed. */
+ std::atomic<int> m_completed;
+ /* The 'done' function. */
+ std::function<void ()> m_done;
+};
+
+void
+task_group::impl::start ()
+{
+ std::shared_ptr<impl> shared_this = shared_from_this ();
+ m_started = true;
+ for (size_t i = 0; i < m_tasks.size (); ++i)
+ {
+ gdb::thread_pool::g_thread_pool->post_task ([=] ()
+ {
+ /* Be sure to capture a shared reference here. */
+ shared_this->m_tasks[i] ();
+ });
+ }
+}
+
+task_group::task_group (std::function<void ()> &&done)
+ : m_task (new impl (std::move (done)))
+{
+}
+
+void
+task_group::add_task (std::function<void ()> &&task)
+{
+ gdb_assert (m_task != nullptr);
+ m_task->add_task (std::move (task));
+}
+
+void
+task_group::start ()
+{
+ gdb_assert (m_task != nullptr);
+ m_task->start ();
+ m_task.reset ();
+}
+
+} /* namespace gdb */
diff --git a/gdbsupport/task-group.h b/gdbsupport/task-group.h
new file mode 100644
index 00000000000..c11b5ccf158
--- /dev/null
+++ b/gdbsupport/task-group.h
@@ -0,0 +1,64 @@
+/* Task group
+
+ Copyright (C) 2023 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef GDBSUPPORT_TASK_GROUP_H
+#define GDBSUPPORT_TASK_GROUP_H
+
+#include <memory>
+
+namespace gdb
+{
+
+/* A task group is a collection of tasks. Each task in the group is
+ submitted to the thread pool. When all the tasks in the group have
+ finished, a final action is run. */
+
+class task_group
+{
+public:
+
+ explicit task_group (std::function<void ()> &&done);
+ DISABLE_COPY_AND_ASSIGN (task_group);
+
+ /* Add a task to the task group. All tasks must be added before the
+ group is started. Note that a task may not throw an
+ exception. */
+ void add_task (std::function<void ()> &&task);
+
+ /* Start this task group. A task group may only be started once.
+ This will submit all the tasks to the global thread pool. */
+ void start ();
+
+private:
+
+ class impl;
+
+ /* A task group is just a facade around an impl. This is done
+ because the impl object must live as long as its longest-lived
+ task, so it is heap-allocated and destroyed when the last task
+ completes. Before 'start' is called, the impl is owned by this
+ task_group, but after 'start' it is owned jointly by the running
+ tasks, and m_task is cleared. */
+ // FIXME
+ std::shared_ptr<impl> m_task;
+};
+
+} /* namespace gdb */
+
+#endif /* GDBSUPPORT_TASK_GROUP_H */
--
2.43.0
next prev parent reply other threads:[~2023-12-10 21:41 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-10 21:41 [PATCH v4 00/19] Index DWARF in the background Tom Tromey
2023-12-10 21:41 ` [PATCH v4 01/19] Don't use objfile::intern in DWO code Tom Tromey
2024-01-09 18:44 ` Simon Marchi
2024-01-09 19:12 ` Tom Tromey
2024-01-09 19:51 ` Tom Tromey
2023-12-10 21:41 ` [PATCH v4 02/19] Pre-read DWZ section data Tom Tromey
2023-12-10 21:41 ` [PATCH v4 03/19] Add a couple of bfd_cache_close calls Tom Tromey
2023-12-10 21:41 ` [PATCH v4 04/19] Add thread-safety to gdb's BFD wrappers Tom Tromey
2023-12-10 21:41 ` [PATCH v4 05/19] Refactor complaint thread-safety approach Tom Tromey
2023-12-10 21:41 ` [PATCH v4 06/19] Add deferred_warnings parameter to read_addrmap_from_aranges Tom Tromey
2023-12-10 21:41 ` [PATCH v4 07/19] Add quick_symbol_functions::compute_main_name Tom Tromey
2023-12-10 21:41 ` Tom Tromey [this message]
2023-12-18 14:55 ` [PATCH v4 08/19] Add gdb::task_group Tom Tromey
2023-12-10 21:41 ` [PATCH v4 09/19] Move cooked_index_storage to cooked-index.h Tom Tromey
2023-12-10 21:41 ` [PATCH v4 10/19] Add "maint set dwarf synchronous" Tom Tromey
2023-12-10 21:41 ` [PATCH v4 11/19] Change how cooked index waits for threads Tom Tromey
2023-12-10 21:41 ` [PATCH v4 12/19] Do more DWARF reading in the background Tom Tromey
2023-12-10 21:41 ` [PATCH v4 13/19] Simplify the public DWARF API Tom Tromey
2023-12-10 21:41 ` [PATCH v4 14/19] Remove two quick_symbol_functions methods Tom Tromey
2023-12-10 21:41 ` [PATCH v4 15/19] Change current_language to be a macro Tom Tromey
2023-12-10 21:41 ` [PATCH v4 16/19] Lazy language setting Tom Tromey
2023-12-10 21:41 ` [PATCH v4 17/19] Optimize lookup_minimal_symbol_text Tom Tromey
2023-12-10 21:41 ` [PATCH v4 18/19] Avoid language-based lookups in startup path Tom Tromey
2023-12-10 21:41 ` [PATCH v4 19/19] Back out some parallel_for_each features Tom Tromey
2024-01-09 1:08 ` [PATCH v4 00/19] Index DWARF in the background Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231210-t-bg-dwarf-reading-v4-8-b978c32fd12f@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).