1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parallel/event.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/atomic.hpp"
12#include "duckdb/common/common.hpp"
13#include "duckdb/common/vector.hpp"
14
15namespace duckdb {
16class Executor;
17class Task;
18
19class Event : public std::enable_shared_from_this<Event> {
20public:
21 Event(Executor &executor);
22 virtual ~Event() = default;
23
24public:
25 virtual void Schedule() = 0;
26 //! Called right after the event is finished
27 virtual void FinishEvent() {
28 }
29 //! Called after the event is entirely finished
30 virtual void FinalizeFinish() {
31 }
32
33 void FinishTask();
34 void Finish();
35
36 void AddDependency(Event &event);
37 bool HasDependencies() const {
38 return total_dependencies != 0;
39 }
40 const vector<Event *> &GetParentsVerification() const;
41
42 void CompleteDependency();
43
44 void SetTasks(vector<shared_ptr<Task>> tasks);
45
46 void InsertEvent(shared_ptr<Event> replacement_event);
47
48 bool IsFinished() const {
49 return finished;
50 }
51
52 virtual void PrintPipeline() {
53 }
54
55protected:
56 Executor &executor;
57 //! The current threads working on the event
58 atomic<idx_t> finished_tasks;
59 //! The maximum amount of threads that can work on the event
60 atomic<idx_t> total_tasks;
61
62 //! The amount of completed dependencies
63 //! The event can only be started after the dependencies have finished executing
64 atomic<idx_t> finished_dependencies;
65 //! The total amount of dependencies
66 idx_t total_dependencies;
67
68 //! The events that depend on this event to run
69 vector<weak_ptr<Event>> parents;
70 //! Raw pointers to the parents (used for verification only)
71 vector<Event *> parents_raw;
72
73 //! Whether or not the event is finished executing
74 atomic<bool> finished;
75};
76
77} // namespace duckdb
78