From patchwork Sun Oct 29 17:35:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 78681 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 598B03861856 for ; Sun, 29 Oct 2023 17:39:14 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from omta36.uswest2.a.cloudfilter.net (omta36.uswest2.a.cloudfilter.net [35.89.44.35]) by sourceware.org (Postfix) with ESMTPS id E029B3861826 for ; Sun, 29 Oct 2023 17:38:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E029B3861826 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E029B3861826 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=35.89.44.35 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698601112; cv=none; b=AwjGwKonlM6AaUEpN56HDb1i7ghH39IF8dK1Vlzi7WqxcyBA6rAor6vZdLOGEIlWOOy0T8c73dAV9IWmfnc0WsFf91ROta8fYEtd2J4fCDzhnF6/yd/ACrNgnbpoDrU863uXbSxk0im28EzvU/LWKnfiMrFYaKmM+1saQzbOgrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698601112; c=relaxed/simple; bh=KES95wGivCEnbmkLqiH90T0j3/0WLBnZ9rVg2AUKRfc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QMqxGIKfiinYZX+FUUYEiSmiAA1BqleNtYa+HXawnA4Z2bekfhV9DgkG9VfDkZlsJzu2HPbs56L2dCq9uThbWzICEJyGoejqM+ZuCAUyLIVyjB8w0cs4AVQHGIImK3enNTuVgkxgi0mt9yzWeChOFdXTZJ538TujRdFmtYpqyzc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from eig-obgw-6001a.ext.cloudfilter.net ([10.0.30.140]) by cmsmtp with ESMTPS id x0cOq0KDBhqFdx9k2qYvB4; Sun, 29 Oct 2023 17:38:26 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id x9k1qFRYcNUwRx9k1q6Rim; Sun, 29 Oct 2023 17:38:25 +0000 X-Authority-Analysis: v=2.4 cv=I852+fsg c=1 sm=1 tr=0 ts=653e9891 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=OWjo9vPv0XrRhIrVQ50Ab3nP57M=:19 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=bhdUkHdE2iEA:10 a=Qbun_eYptAEA:10 a=mDV3o1hIAAAA:8 a=ccfE1abgpALcRL0arBIA:9 a=_FVE-zBwftR9WsbkzFJk:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=ityhtAPlv5eeyzVSRgZLvhv5qhVvUmdG5cwlUzD+hzs=; b=JdZEp8G0uiSy+J77bCsPTTYHfT vqRiBvWCQEMxFrv6E/DZLW60Ar1arzE1Dhx7LlQKWzIKoLd1Q2BfHJ5WLCiyVrLM1K/OSj2f8u64h ijFT4cv3nTANueAwRQuKciwnr; Received: from 97-122-77-73.hlrn.qwest.net ([97.122.77.73]:47464 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1qx9k0-000nk4-2Y; Sun, 29 Oct 2023 11:38:24 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 04/15] Add gdb::task_group Date: Sun, 29 Oct 2023 11:35:23 -0600 Message-ID: <20231029173839.471514-5-tom@tromey.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231029173839.471514-1-tom@tromey.com> References: <20231029173839.471514-1-tom@tromey.com> MIME-Version: 1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.77.73 X-Source-L: No X-Exim-ID: 1qx9k0-000nk4-2Y X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-77-73.hlrn.qwest.net (localhost.localdomain) [97.122.77.73]:47464 X-Source-Auth: tom+tromey.com X-Email-Count: 5 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfJM5OjYHndAANPAC/a654Zm4f03R84RQqTetEzL6mXHXzkY72hWzyScOvEDh193utqe/vPnKMEWRAT67zGeFYqkzy2qkoUSaQFH2SSExcYYP57w4D5AT XSm3DxXmGBs+Q4QnyOxB9j+r2gDC/TkhC5gLY9i/R4JmiU8WzzRcJWRf41WQIW2ft00rLcb7DYu3+TBhJQVnE/FRBY/U4lVYxDc= X-Spam-Status: No, score=-3024.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_SHORT, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org 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(-) create mode 100644 gdbsupport/task-group.cc create mode 100644 gdbsupport/task-group.h diff --git a/gdbsupport/Makefile.am b/gdbsupport/Makefile.am index 00524e9a566..1871b96f49b 100644 --- a/gdbsupport/Makefile.am +++ b/gdbsupport/Makefile.am @@ -74,6 +74,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 50a5a55e19d..bbbbbd57282 100644 --- a/gdbsupport/Makefile.in +++ b/gdbsupport/Makefile.in @@ -160,8 +160,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@) @@ -401,6 +401,7 @@ libgdbsupport_a_SOURCES = \ search.cc \ signals.cc \ signals-state-save-restore.cc \ + task-group.cc \ tdesc.cc \ thread-pool.cc \ xml-utils.cc \ @@ -511,6 +512,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 . */ + +#include "common-defs.h" +#include "task-group.h" +#include "thread-pool.h" + +namespace gdb +{ + +class task_group::impl : public std::enable_shared_from_this +{ +public: + + explicit impl (std::function &&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 &&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> m_tasks; + /* The number of tasks that have completed. */ + std::atomic m_completed; + /* The 'done' function. */ + std::function m_done; +}; + +void +task_group::impl::start () +{ + std::shared_ptr 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 &&done) + : m_task (new impl (std::move (done))) +{ +} + +void +task_group::add_task (std::function &&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 . */ + +#ifndef GDBSUPPORT_TASK_GROUP_H +#define GDBSUPPORT_TASK_GROUP_H + +#include + +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 &&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 &&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 m_task; +}; + +} /* namespace gdb */ + +#endif /* GDBSUPPORT_TASK_GROUP_H */