Home | History | Annotate | Line # | Download | only in gdbsupport
      1 /* Task group
      2 
      3    Copyright (C) 2023-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #ifndef GDBSUPPORT_TASK_GROUP_H
     21 #define GDBSUPPORT_TASK_GROUP_H
     22 
     23 #include <memory>
     24 
     25 namespace gdb
     26 {
     27 
     28 /* A task group is a collection of tasks.  Each task in the group is
     29    submitted to the thread pool.  When all the tasks in the group have
     30    finished, a final action is run.  */
     31 
     32 class task_group
     33 {
     34 public:
     35 
     36   explicit task_group (std::function<void ()> &&done);
     37   DISABLE_COPY_AND_ASSIGN (task_group);
     38 
     39   /* Add a task to the task group.  All tasks must be added before the
     40      group is started.  Note that a task may not throw an
     41      exception.  */
     42   void add_task (std::function<void ()> &&task);
     43 
     44   /* Start this task group.  A task group may only be started once.
     45      This will submit all the tasks to the global thread pool.  */
     46   void start ();
     47 
     48 private:
     49 
     50   class impl;
     51 
     52   /* A task group is just a facade around an impl.  This is done
     53      because the impl object must live as long as its longest-lived
     54      task, so it is heap-allocated and destroyed when the last task
     55      completes.  */
     56   std::shared_ptr<impl> m_task;
     57 };
     58 
     59 } /* namespace gdb */
     60 
     61 #endif /* GDBSUPPORT_TASK_GROUP_H */
     62