1 //===--------- TaskDispatch.h - ORC task dispatch utils ---------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Task and TaskDispatch classes.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_ORC_TASKDISPATCH_H
14 #define LLVM_EXECUTIONENGINE_ORC_TASKDISPATCH_H
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/ExtensibleRTTI.h"
19 #include "llvm/Support/raw_ostream.h"
24 #if LLVM_ENABLE_THREADS
25 #include <condition_variable>
33 /// Represents an abstract task for ORC to run.
34 class Task : public RTTIExtends<Task, RTTIRoot> {
38 virtual ~Task() = default;
40 /// Description of the task to be performed. Used for logging.
41 virtual void printDescription(raw_ostream &OS) = 0;
44 virtual void run() = 0;
47 void anchor() override;
50 /// Base class for generic tasks.
51 class GenericNamedTask : public RTTIExtends<GenericNamedTask, Task> {
54 static const char *DefaultDescription;
57 /// Generic task implementation.
58 template <typename FnT> class GenericNamedTaskImpl : public GenericNamedTask {
60 GenericNamedTaskImpl(FnT &&Fn, std::string DescBuffer)
61 : Fn(std::forward<FnT>(Fn)), Desc(DescBuffer.c_str()),
62 DescBuffer(std::move(DescBuffer)) {}
63 GenericNamedTaskImpl(FnT &&Fn, const char *Desc)
64 : Fn(std::forward<FnT>(Fn)), Desc(Desc) {
65 assert(Desc && "Description cannot be null");
67 void printDescription(raw_ostream &OS) override { OS << Desc; }
68 void run() override { Fn(); }
73 std::string DescBuffer;
76 /// Create a generic named task from a std::string description.
77 template <typename FnT>
78 std::unique_ptr<GenericNamedTask> makeGenericNamedTask(FnT &&Fn,
80 return std::make_unique<GenericNamedTaskImpl<FnT>>(std::forward<FnT>(Fn),
84 /// Create a generic named task from a const char * description.
85 template <typename FnT>
86 std::unique_ptr<GenericNamedTask>
87 makeGenericNamedTask(FnT &&Fn, const char *Desc = nullptr) {
89 Desc = GenericNamedTask::DefaultDescription;
90 return std::make_unique<GenericNamedTaskImpl<FnT>>(std::forward<FnT>(Fn),
94 /// Abstract base for classes that dispatch ORC Tasks.
95 class TaskDispatcher {
97 virtual ~TaskDispatcher();
99 /// Run the given task.
100 virtual void dispatch(std::unique_ptr<Task> T) = 0;
102 /// Called by ExecutionSession. Waits until all tasks have completed.
103 virtual void shutdown() = 0;
106 /// Runs all tasks on the current thread.
107 class InPlaceTaskDispatcher : public TaskDispatcher {
109 void dispatch(std::unique_ptr<Task> T) override;
110 void shutdown() override;
113 #if LLVM_ENABLE_THREADS
115 class DynamicThreadPoolTaskDispatcher : public TaskDispatcher {
117 void dispatch(std::unique_ptr<Task> T) override;
118 void shutdown() override;
120 std::mutex DispatchMutex;
122 size_t Outstanding = 0;
123 std::condition_variable OutstandingCV;
126 #endif // LLVM_ENABLE_THREADS
128 } // End namespace orc
129 } // End namespace llvm
131 #endif // LLVM_EXECUTIONENGINE_ORC_TASKDISPATCH_H