1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parallel/task.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12
13namespace duckdb {
14class ClientContext;
15class Executor;
16class Task;
17class DatabaseInstance;
18struct ProducerToken;
19
20enum class TaskExecutionMode : uint8_t { PROCESS_ALL, PROCESS_PARTIAL };
21
22enum class TaskExecutionResult : uint8_t { TASK_FINISHED, TASK_NOT_FINISHED, TASK_ERROR, TASK_BLOCKED };
23
24//! Generic parallel task
25class Task : public std::enable_shared_from_this<Task> {
26public:
27 virtual ~Task() {
28 }
29
30 //! Execute the task in the specified execution mode
31 //! If mode is PROCESS_ALL, Execute should always finish processing and return TASK_FINISHED
32 //! If mode is PROCESS_PARTIAL, Execute can return TASK_NOT_FINISHED, in which case Execute will be called again
33 //! In case of an error, TASK_ERROR is returned
34 //! In case the task has interrupted, BLOCKED is returned.
35 virtual TaskExecutionResult Execute(TaskExecutionMode mode) = 0;
36
37 //! Descheduling a task ensures the task is not executed, but remains available for rescheduling as long as
38 //! required, generally until some code in an operator calls the InterruptState::Callback() method of a state of the
39 //! InterruptMode::TASK mode.
40 virtual void Deschedule() {
41 throw InternalException("Cannot deschedule task of base Task class");
42 };
43
44 //! Ensures a task is rescheduled to the correct queue
45 virtual void Reschedule() {
46 throw InternalException("Cannot reschedule task of base Task class");
47 }
48};
49
50//! Execute a task within an executor, including exception handling
51//! This should be used within queries
52class ExecutorTask : public Task {
53public:
54 ExecutorTask(Executor &executor);
55 ExecutorTask(ClientContext &context);
56 virtual ~ExecutorTask();
57
58 void Deschedule() override;
59 void Reschedule() override;
60
61 Executor &executor;
62
63public:
64 virtual TaskExecutionResult ExecuteTask(TaskExecutionMode mode) = 0;
65 TaskExecutionResult Execute(TaskExecutionMode mode) override;
66};
67
68} // namespace duckdb
69