xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/task-group.h (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
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 #ifndef GDBSUPPORT_TASK_GROUP_H
21*5ba1f45fSchristos #define GDBSUPPORT_TASK_GROUP_H
22*5ba1f45fSchristos 
23*5ba1f45fSchristos #include <memory>
24*5ba1f45fSchristos 
25*5ba1f45fSchristos namespace gdb
26*5ba1f45fSchristos {
27*5ba1f45fSchristos 
28*5ba1f45fSchristos /* A task group is a collection of tasks.  Each task in the group is
29*5ba1f45fSchristos    submitted to the thread pool.  When all the tasks in the group have
30*5ba1f45fSchristos    finished, a final action is run.  */
31*5ba1f45fSchristos 
32*5ba1f45fSchristos class task_group
33*5ba1f45fSchristos {
34*5ba1f45fSchristos public:
35*5ba1f45fSchristos 
36*5ba1f45fSchristos   explicit task_group (std::function<void ()> &&done);
37*5ba1f45fSchristos   DISABLE_COPY_AND_ASSIGN (task_group);
38*5ba1f45fSchristos 
39*5ba1f45fSchristos   /* Add a task to the task group.  All tasks must be added before the
40*5ba1f45fSchristos      group is started.  Note that a task may not throw an
41*5ba1f45fSchristos      exception.  */
42*5ba1f45fSchristos   void add_task (std::function<void ()> &&task);
43*5ba1f45fSchristos 
44*5ba1f45fSchristos   /* Start this task group.  A task group may only be started once.
45*5ba1f45fSchristos      This will submit all the tasks to the global thread pool.  */
46*5ba1f45fSchristos   void start ();
47*5ba1f45fSchristos 
48*5ba1f45fSchristos private:
49*5ba1f45fSchristos 
50*5ba1f45fSchristos   class impl;
51*5ba1f45fSchristos 
52*5ba1f45fSchristos   /* A task group is just a facade around an impl.  This is done
53*5ba1f45fSchristos      because the impl object must live as long as its longest-lived
54*5ba1f45fSchristos      task, so it is heap-allocated and destroyed when the last task
55*5ba1f45fSchristos      completes.  */
56*5ba1f45fSchristos   std::shared_ptr<impl> m_task;
57*5ba1f45fSchristos };
58*5ba1f45fSchristos 
59*5ba1f45fSchristos } /* namespace gdb */
60*5ba1f45fSchristos 
61*5ba1f45fSchristos #endif /* GDBSUPPORT_TASK_GROUP_H */
62