AIStatefulTask ‐ Asynchronous, Stateful Task Scheduler library.

Threads-like task objects evolving through user-defined states.

RunningTasksTracker.h
1#pragma once
2
3#include "AIStatefulTask.h"
4#include "threadsafe/PointerStorage.h"
5#include <limits>
6#include "debug.h"
7
8namespace statefultask {
9
10// A class to keep track of tasks, providing a means of mass abortion.
11//
12// Task can add themselves from initialize_impl, and then must remove
13// themselves again in finish_impl.
14//
15// The call to abort_all will block tasks that call add(), make a copy
16// of all currently added tasks (keeping them alive with boost::intrusive_ptr),
17// unblock tasks that call add() (but now aborting them instead of adding
18// them) and then call abort() on all the copied tasks.
19//
21{
22 public:
23 using index_type = aithreadsafe::VoidPointerStorage::index_type;
24 static constexpr index_type s_aborted = std::numeric_limits<index_type>::max();
25
26 private:
27 aithreadsafe::PointerStorage<AIStatefulTask> m_tasks;
28 bool m_aborted{false};
29
30 public:
31 RunningTasksTracker(uint32_t initial_size) : m_tasks(initial_size) { }
32
33 index_type add(AIStatefulTask* task)
34 {
35 if (AI_UNLIKELY(m_aborted))
36 {
37 task->abort();
38 return s_aborted;
39 }
40 return m_tasks.insert(task);
41 }
42
43 void remove(index_type index)
44 {
45 // Is it avoidable to call this when the task was aborted before it was added?
46 ASSERT(index != s_aborted);
47 m_tasks.erase(index);
48 }
49
50 void abort_all();
51};
52
53} // namespace statefultask
Declaration of base class AIStatefulTask.
Definition: AIStatefulTask.h:96
Definition: RunningTasksTracker.h:21
void abort()
Definition: AIStatefulTask.cxx:1690
Tasks defined by the library project are put into this namespace.
Definition: AIStatefulTask.h:857