1*5ba1f45fSchristos /* Task group 2*5ba1f45fSchristos 3*5ba1f45fSchristos Copyright (C) 2023-2024 Free Software Foundation, Inc. 4*5ba1f45fSchristos 5*5ba1f45fSchristos This file is part of GDB. 6*5ba1f45fSchristos 7*5ba1f45fSchristos This program is free software; you can redistribute it and/or modify 8*5ba1f45fSchristos it under the terms of the GNU General Public License as published by 9*5ba1f45fSchristos the Free Software Foundation; either version 3 of the License, or 10*5ba1f45fSchristos (at your option) any later version. 11*5ba1f45fSchristos 12*5ba1f45fSchristos This program is distributed in the hope that it will be useful, 13*5ba1f45fSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 14*5ba1f45fSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*5ba1f45fSchristos GNU General Public License for more details. 16*5ba1f45fSchristos 17*5ba1f45fSchristos You should have received a copy of the GNU General Public License 18*5ba1f45fSchristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19*5ba1f45fSchristos 20*5ba1f45fSchristos #include "task-group.h" 21*5ba1f45fSchristos #include "thread-pool.h" 22*5ba1f45fSchristos 23*5ba1f45fSchristos namespace gdb 24*5ba1f45fSchristos { 25*5ba1f45fSchristos 26*5ba1f45fSchristos class task_group::impl : public std::enable_shared_from_this<task_group::impl> 27*5ba1f45fSchristos { 28*5ba1f45fSchristos public: 29*5ba1f45fSchristos 30*5ba1f45fSchristos explicit impl (std::function<void ()> &&done) 31*5ba1f45fSchristos : m_done (std::move (done)) 32*5ba1f45fSchristos { } 33*5ba1f45fSchristos DISABLE_COPY_AND_ASSIGN (impl); 34*5ba1f45fSchristos 35*5ba1f45fSchristos ~impl () 36*5ba1f45fSchristos { 37*5ba1f45fSchristos if (m_started) 38*5ba1f45fSchristos m_done (); 39*5ba1f45fSchristos } 40*5ba1f45fSchristos 41*5ba1f45fSchristos /* Add a task to the task group. */ 42*5ba1f45fSchristos void add_task (std::function<void ()> &&task) 43*5ba1f45fSchristos { 44*5ba1f45fSchristos m_tasks.push_back (std::move (task)); 45*5ba1f45fSchristos }; 46*5ba1f45fSchristos 47*5ba1f45fSchristos /* Start this task group. */ 48*5ba1f45fSchristos void start (); 49*5ba1f45fSchristos 50*5ba1f45fSchristos /* True if started. */ 51*5ba1f45fSchristos bool m_started = false; 52*5ba1f45fSchristos /* The tasks. */ 53*5ba1f45fSchristos std::vector<std::function<void ()>> m_tasks; 54*5ba1f45fSchristos /* The 'done' function. */ 55*5ba1f45fSchristos std::function<void ()> m_done; 56*5ba1f45fSchristos }; 57*5ba1f45fSchristos 58*5ba1f45fSchristos void 59*5ba1f45fSchristos task_group::impl::start () 60*5ba1f45fSchristos { 61*5ba1f45fSchristos std::shared_ptr<impl> shared_this = shared_from_this (); 62*5ba1f45fSchristos m_started = true; 63*5ba1f45fSchristos for (size_t i = 0; i < m_tasks.size (); ++i) 64*5ba1f45fSchristos { 65*5ba1f45fSchristos gdb::thread_pool::g_thread_pool->post_task ([=] () 66*5ba1f45fSchristos { 67*5ba1f45fSchristos /* Be sure to capture a shared reference here. */ 68*5ba1f45fSchristos shared_this->m_tasks[i] (); 69*5ba1f45fSchristos }); 70*5ba1f45fSchristos } 71*5ba1f45fSchristos } 72*5ba1f45fSchristos 73*5ba1f45fSchristos task_group::task_group (std::function<void ()> &&done) 74*5ba1f45fSchristos : m_task (new impl (std::move (done))) 75*5ba1f45fSchristos { 76*5ba1f45fSchristos } 77*5ba1f45fSchristos 78*5ba1f45fSchristos void 79*5ba1f45fSchristos task_group::add_task (std::function<void ()> &&task) 80*5ba1f45fSchristos { 81*5ba1f45fSchristos gdb_assert (m_task != nullptr); 82*5ba1f45fSchristos m_task->add_task (std::move (task)); 83*5ba1f45fSchristos } 84*5ba1f45fSchristos 85*5ba1f45fSchristos void 86*5ba1f45fSchristos task_group::start () 87*5ba1f45fSchristos { 88*5ba1f45fSchristos gdb_assert (m_task != nullptr); 89*5ba1f45fSchristos m_task->start (); 90*5ba1f45fSchristos m_task.reset (); 91*5ba1f45fSchristos } 92*5ba1f45fSchristos 93*5ba1f45fSchristos } /* namespace gdb */ 94