TaskDispatch.cpp 1.3 KB

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