1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef CORE_THREADING_SEQUENTIAL_TASK_QUEUE_H
17 #define CORE_THREADING_SEQUENTIAL_TASK_QUEUE_H
18
19 #include <base/containers/vector.h>
20 #include <core/namespace.h>
21 #include <core/threading/intf_thread_pool.h>
22
23 #include "threading/task_queue.h"
24
CORE_BEGIN_NAMESPACE()25 CORE_BEGIN_NAMESPACE()
26 // Non-thread safe sequential task queue, executes tasks sequentially one-by-one.
27 // This queue type is not thread safe and should be only used from one thread.
28 class SequentialTaskQueue final : public TaskQueue {
29 public:
30 /** Constructor for the sequential task queue.
31 @param threads Optional thread pool, if support for threading is desired.
32 */
33 explicit SequentialTaskQueue(const IThreadPool::Ptr& threadPool);
34 SequentialTaskQueue(const SequentialTaskQueue& other) = delete;
35 ~SequentialTaskQueue() override;
36
37 /** Submit task to execution queue, to be run after another task.
38 @param afterIdentifier Identifier of the task that is run prior the submitted task.
39 @param taskIdentifier Identifier of the task, must be unique.
40 @param task Task to execute.
41 */
42 void SubmitAfter(uint64_t afterIdentifier, uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task);
43
44 /** Submit task to execution queue, to be run before another task.
45 @param beforeIdentifier Identifier of the task that is run after the submitted task.
46 @param taskIdentifier Identifier of the task, must be unique.
47 @param task Task to execute.
48 */
49 void SubmitBefore(uint64_t beforeIdentifier, uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task);
50
51 void Submit(uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task) override;
52 void Remove(uint64_t taskIdentifier) override;
53
54 void Clear() override;
55
56 void Execute() override;
57
58 private:
59 BASE_NS::vector<TaskQueue::Entry> tasks_;
60 };
61 CORE_END_NAMESPACE()
62
63 #endif // CORE_THREADING_SEQUENTIAL_TASK_QUEUE_H
64