TaskDispatch.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--------- TaskDispatch.h - ORC task dispatch utils ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Task and TaskDispatch classes.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_TASKDISPATCH_H
  18. #define LLVM_EXECUTIONENGINE_ORC_TASKDISPATCH_H
  19. #include "llvm/Config/llvm-config.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/ExtensibleRTTI.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include <cassert>
  24. #include <string>
  25. #if LLVM_ENABLE_THREADS
  26. #include <condition_variable>
  27. #include <mutex>
  28. #include <thread>
  29. #endif
  30. namespace llvm {
  31. namespace orc {
  32. /// Represents an abstract task for ORC to run.
  33. class Task : public RTTIExtends<Task, RTTIRoot> {
  34. public:
  35. static char ID;
  36. virtual ~Task() = default;
  37. /// Description of the task to be performed. Used for logging.
  38. virtual void printDescription(raw_ostream &OS) = 0;
  39. /// Run the task.
  40. virtual void run() = 0;
  41. private:
  42. void anchor() override;
  43. };
  44. /// Base class for generic tasks.
  45. class GenericNamedTask : public RTTIExtends<GenericNamedTask, Task> {
  46. public:
  47. static char ID;
  48. static const char *DefaultDescription;
  49. };
  50. /// Generic task implementation.
  51. template <typename FnT> class GenericNamedTaskImpl : public GenericNamedTask {
  52. public:
  53. GenericNamedTaskImpl(FnT &&Fn, std::string DescBuffer)
  54. : Fn(std::forward<FnT>(Fn)), Desc(DescBuffer.c_str()),
  55. DescBuffer(std::move(DescBuffer)) {}
  56. GenericNamedTaskImpl(FnT &&Fn, const char *Desc)
  57. : Fn(std::forward<FnT>(Fn)), Desc(Desc) {
  58. assert(Desc && "Description cannot be null");
  59. }
  60. void printDescription(raw_ostream &OS) override { OS << Desc; }
  61. void run() override { Fn(); }
  62. private:
  63. FnT Fn;
  64. const char *Desc;
  65. std::string DescBuffer;
  66. };
  67. /// Create a generic named task from a std::string description.
  68. template <typename FnT>
  69. std::unique_ptr<GenericNamedTask> makeGenericNamedTask(FnT &&Fn,
  70. std::string Desc) {
  71. return std::make_unique<GenericNamedTaskImpl<FnT>>(std::forward<FnT>(Fn),
  72. std::move(Desc));
  73. }
  74. /// Create a generic named task from a const char * description.
  75. template <typename FnT>
  76. std::unique_ptr<GenericNamedTask>
  77. makeGenericNamedTask(FnT &&Fn, const char *Desc = nullptr) {
  78. if (!Desc)
  79. Desc = GenericNamedTask::DefaultDescription;
  80. return std::make_unique<GenericNamedTaskImpl<FnT>>(std::forward<FnT>(Fn),
  81. Desc);
  82. }
  83. /// Abstract base for classes that dispatch ORC Tasks.
  84. class TaskDispatcher {
  85. public:
  86. virtual ~TaskDispatcher();
  87. /// Run the given task.
  88. virtual void dispatch(std::unique_ptr<Task> T) = 0;
  89. /// Called by ExecutionSession. Waits until all tasks have completed.
  90. virtual void shutdown() = 0;
  91. };
  92. /// Runs all tasks on the current thread.
  93. class InPlaceTaskDispatcher : public TaskDispatcher {
  94. public:
  95. void dispatch(std::unique_ptr<Task> T) override;
  96. void shutdown() override;
  97. };
  98. #if LLVM_ENABLE_THREADS
  99. class DynamicThreadPoolTaskDispatcher : public TaskDispatcher {
  100. public:
  101. void dispatch(std::unique_ptr<Task> T) override;
  102. void shutdown() override;
  103. private:
  104. std::mutex DispatchMutex;
  105. bool Running = true;
  106. size_t Outstanding = 0;
  107. std::condition_variable OutstandingCV;
  108. };
  109. #endif // LLVM_ENABLE_THREADS
  110. } // End namespace orc
  111. } // End namespace llvm
  112. #endif // LLVM_EXECUTIONENGINE_ORC_TASKDISPATCH_H
  113. #ifdef __GNUC__
  114. #pragma GCC diagnostic pop
  115. #endif