Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/TaskDispatch.cpp
35266 views
//===------------ TaskDispatch.cpp - ORC task dispatch utils --------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/ExecutionEngine/Orc/TaskDispatch.h"9#include "llvm/ExecutionEngine/Orc/Core.h"1011namespace llvm {12namespace orc {1314char Task::ID = 0;15char GenericNamedTask::ID = 0;16const char *GenericNamedTask::DefaultDescription = "Generic Task";1718void Task::anchor() {}19TaskDispatcher::~TaskDispatcher() = default;2021void InPlaceTaskDispatcher::dispatch(std::unique_ptr<Task> T) { T->run(); }2223void InPlaceTaskDispatcher::shutdown() {}2425#if LLVM_ENABLE_THREADS26void DynamicThreadPoolTaskDispatcher::dispatch(std::unique_ptr<Task> T) {27bool IsMaterializationTask = isa<MaterializationTask>(*T);2829{30std::lock_guard<std::mutex> Lock(DispatchMutex);3132if (IsMaterializationTask) {3334// If this is a materialization task and there are too many running35// already then queue this one up and return early.36if (MaxMaterializationThreads &&37NumMaterializationThreads == *MaxMaterializationThreads) {38MaterializationTaskQueue.push_back(std::move(T));39return;40}4142// Otherwise record that we have a materialization task running.43++NumMaterializationThreads;44}4546++Outstanding;47}4849std::thread([this, T = std::move(T), IsMaterializationTask]() mutable {50while (true) {5152// Run the task.53T->run();5455std::lock_guard<std::mutex> Lock(DispatchMutex);56if (!MaterializationTaskQueue.empty()) {57// If there are any materialization tasks running then steal that work.58T = std::move(MaterializationTaskQueue.front());59MaterializationTaskQueue.pop_front();60if (!IsMaterializationTask) {61++NumMaterializationThreads;62IsMaterializationTask = true;63}64} else {65// Otherwise decrement work counters.66if (IsMaterializationTask)67--NumMaterializationThreads;68--Outstanding;69OutstandingCV.notify_all();70return;71}72}73}).detach();74}7576void DynamicThreadPoolTaskDispatcher::shutdown() {77std::unique_lock<std::mutex> Lock(DispatchMutex);78Running = false;79OutstandingCV.wait(Lock, [this]() { return Outstanding == 0; });80}81#endif8283} // namespace orc84} // namespace llvm858687