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 
9 #include "llvm/ExecutionEngine/Orc/TaskDispatch.h"
10 
11 namespace llvm {
12 namespace orc {
13 
14 char Task::ID = 0;
15 char GenericNamedTask::ID = 0;
16 const char *GenericNamedTask::DefaultDescription = "Generic Task";
17 
18 void Task::anchor() {}
19 TaskDispatcher::~TaskDispatcher() {}
20 
21 void InPlaceTaskDispatcher::dispatch(std::unique_ptr<Task> T) { T->run(); }
22 
23 void InPlaceTaskDispatcher::shutdown() {}
24 
25 #if LLVM_ENABLE_THREADS
26 void DynamicThreadPoolTaskDispatcher::dispatch(std::unique_ptr<Task> T) {
27   {
28     std::lock_guard<std::mutex> Lock(DispatchMutex);
29     ++Outstanding;
30   }
31 
32   std::thread([this, T = std::move(T)]() mutable {
33     T->run();
34     std::lock_guard<std::mutex> Lock(DispatchMutex);
35     --Outstanding;
36     OutstandingCV.notify_all();
37   }).detach();
38 }
39 
40 void DynamicThreadPoolTaskDispatcher::shutdown() {
41   std::unique_lock<std::mutex> Lock(DispatchMutex);
42   Running = false;
43   OutstandingCV.wait(Lock, [this]() { return Outstanding == 0; });
44 }
45 #endif
46 
47 } // namespace orc
48 } // namespace llvm
49