1 /*
2  * Copyright (c) 2024 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 
25 BASE_BEGIN_NAMESPACE()
26 template<class T>
27 class array_view;
28 BASE_END_NAMESPACE()
29 
CORE_BEGIN_NAMESPACE()30 CORE_BEGIN_NAMESPACE()
31 // Non-thread safe sequential task queue, executes tasks sequentially one-by-one.
32 // This queue type is not thread safe and should be only used from one thread.
33 class SequentialTaskQueue final : public TaskQueue {
34 public:
35     /** Constructor for the sequential task queue.
36         @param threads Optional thread pool, if support for threading is desired.
37     */
38     explicit SequentialTaskQueue(const IThreadPool::Ptr& threadPool);
39     SequentialTaskQueue(const SequentialTaskQueue& other) = delete;
40     ~SequentialTaskQueue() override;
41 
42     /** Submit task to execution queue, to be run after another task.
43         @param afterIdentifier Identifier of the task that is run prior the submitted task.
44         @param taskIdentifier Identifier of the task, must be unique.
45         @param task Task to execute.
46     */
47     void SubmitAfter(uint64_t afterIdentifier, uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task);
48 
49     void SubmitAfter(
50         BASE_NS::array_view<const uint64_t> afterIdentifiers, uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task);
51 
52     /** Submit task to execution queue, to be run before another task.
53         @param beforeIdentifier Identifier of the task that is run after the submitted task.
54         @param taskIdentifier Identifier of the task, must be unique.
55         @param task Task to execute.
56     */
57     void SubmitBefore(uint64_t beforeIdentifier, uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task);
58 
59     void Submit(uint64_t taskIdentifier, IThreadPool::ITask::Ptr&& task) override;
60     void Remove(uint64_t taskIdentifier) override;
61 
62     void Clear() override;
63 
64     void Execute() override;
65 
66 private:
67     BASE_NS::vector<TaskQueue::Entry> tasks_;
68 };
69 CORE_END_NAMESPACE()
70 
71 #endif // CORE_THREADING_SEQUENTIAL_TASK_QUEUE_H
72