LLVM 20.0.0git
TaskDispatch.cpp
Go to the documentation of this file.
1//===------------ TaskDispatch.cpp - ORC task dispatch utils --------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_THREADS
12
13namespace llvm {
14namespace orc {
15
16char Task::ID = 0;
18const char *GenericNamedTask::DefaultDescription = "Generic Task";
19
20void Task::anchor() {}
22
23void InPlaceTaskDispatcher::dispatch(std::unique_ptr<Task> T) { T->run(); }
24
26
27#if LLVM_ENABLE_THREADS
28void DynamicThreadPoolTaskDispatcher::dispatch(std::unique_ptr<Task> T) {
29 bool IsMaterializationTask = isa<MaterializationTask>(*T);
30
31 {
32 std::lock_guard<std::mutex> Lock(DispatchMutex);
33
34 if (IsMaterializationTask) {
35
36 // If this is a materialization task and there are too many running
37 // already then queue this one up and return early.
38 if (MaxMaterializationThreads &&
39 NumMaterializationThreads == *MaxMaterializationThreads) {
40 MaterializationTaskQueue.push_back(std::move(T));
41 return;
42 }
43
44 // Otherwise record that we have a materialization task running.
45 ++NumMaterializationThreads;
46 }
47
48 ++Outstanding;
49 }
50
51 std::thread([this, T = std::move(T), IsMaterializationTask]() mutable {
52 while (true) {
53
54 // Run the task.
55 T->run();
56
57 std::lock_guard<std::mutex> Lock(DispatchMutex);
58 if (!MaterializationTaskQueue.empty()) {
59 // If there are any materialization tasks running then steal that work.
60 T = std::move(MaterializationTaskQueue.front());
61 MaterializationTaskQueue.pop_front();
62 if (!IsMaterializationTask) {
63 ++NumMaterializationThreads;
64 IsMaterializationTask = true;
65 }
66 } else {
67 // Otherwise decrement work counters.
68 if (IsMaterializationTask)
69 --NumMaterializationThreads;
70 --Outstanding;
71 OutstandingCV.notify_all();
72 return;
73 }
74 }
75 }).detach();
76}
77
78void DynamicThreadPoolTaskDispatcher::shutdown() {
79 std::unique_lock<std::mutex> Lock(DispatchMutex);
80 Running = false;
81 OutstandingCV.wait(Lock, [this]() { return Outstanding == 0; });
82}
83#endif
84
85} // namespace orc
86} // namespace llvm
static const char * DefaultDescription
Definition: TaskDispatch.h:55
void shutdown() override
Called by ExecutionSession. Waits until all tasks have completed.
void dispatch(std::unique_ptr< Task > T) override
Run the given task.
static char ID
Definition: TaskDispatch.h:37
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18