|
| 1 | +//===------------------ QueueingTaskDispatcher.h ----------------*- C++ -*-===// |
| 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 | +// QueueingTaskDispatcher class. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef ORC_RT_QUEUEINGTASKDISPATCHER_H |
| 14 | +#define ORC_RT_QUEUEINGTASKDISPATCHER_H |
| 15 | + |
| 16 | +#include "orc-rt/TaskDispatcher.h" |
| 17 | + |
| 18 | +#include <deque> |
| 19 | +#include <memory> |
| 20 | +#include <mutex> |
| 21 | + |
| 22 | +namespace orc_rt { |
| 23 | + |
| 24 | +/// A TaskDispatcher implementation that puts tasks in a queue to be run. |
| 25 | +/// QueueingTaskDispatcher provides direct access to the queue, allowing |
| 26 | +/// clients to decide how to run tasks. It is intended for use on systems |
| 27 | +/// where threads are not available, and for unit tests. |
| 28 | +/// For most uses of the ORC runtime, use of QueueingTaskDispatcher is strongly |
| 29 | +/// discouraged, and alternatives like ThreadPoolTaskDispatcher are preferred. |
| 30 | +class QueueingTaskDispatcher : public TaskDispatcher { |
| 31 | +public: |
| 32 | + void dispatch(std::unique_ptr<Task> T) override; |
| 33 | + void shutdown() override; |
| 34 | + |
| 35 | + /// Take a task from the back of the queue. If there are no tasks, returns |
| 36 | + /// nullptr. |
| 37 | + std::unique_ptr<Task> pop_back(); |
| 38 | + |
| 39 | + /// Take a task from the front of the queue. If there are no tasks, returns |
| 40 | + /// nullptr. |
| 41 | + std::unique_ptr<Task> pop_front(); |
| 42 | + |
| 43 | + /// Run tasks in last-in-first-out order until the queue is empty. |
| 44 | + void runLIFOUntilEmpty(); |
| 45 | + |
| 46 | + /// Run tasks in first-in-first-out order until the queue is empty. |
| 47 | + void runFIFOUntilEmpty(); |
| 48 | + |
| 49 | +private: |
| 50 | + std::mutex M; |
| 51 | + enum { Running, Shutdown } State = Running; |
| 52 | + std::deque<std::unique_ptr<Task>> Tasks; |
| 53 | +}; |
| 54 | + |
| 55 | +} // namespace orc_rt |
| 56 | + |
| 57 | +#endif // ORC_RT_QUEUEINGTASKDISPATCHER_H |
0 commit comments