• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 PANDA_RUNTIME_THREAD_POOL_QUEUE_H_
17 #define PANDA_RUNTIME_THREAD_POOL_QUEUE_H_
18 
19 #include "libpandabase/macros.h"
20 
21 static constexpr size_t QUEUE_SIZE_MAX_SIZE = 1000;
22 
23 namespace ark {
24 
25 class TaskInterface {
26 public:
27     bool IsEmpty();
28 };
29 
30 template <typename Task>
31 class TaskQueueInterface {
32 public:
33     // All methods (except for Finalize) require an acquired lock from a thread pool.
queueMaxSize_(queueMaxSize)34     explicit TaskQueueInterface(size_t queueMaxSize = QUEUE_SIZE_MAX_SIZE) : queueMaxSize_(queueMaxSize) {}
35     virtual ~TaskQueueInterface() = default;
36 
37     NO_COPY_SEMANTIC(TaskQueueInterface);
38     NO_MOVE_SEMANTIC(TaskQueueInterface);
39 
40     virtual Task GetTask() = 0;
41 
42     // NOLINTNEXTLINE(google-default-arguments)
43     virtual void AddTask(Task &&task, size_t priority = 0) = 0;
44     virtual void Finalize() = 0;
45 
46     bool TryAddTask(Task &&task, size_t priority = 0)
47     {
48         if (IsFull()) {
49             return false;
50         }
51         AddTask(std::move(task), priority);
52         return true;
53     }
54 
IsEmpty()55     bool IsEmpty()
56     {
57         return GetQueueSize() == 0;
58     }
59 
IsFull()60     bool IsFull()
61     {
62         return GetQueueSize() >= queueMaxSize_;
63     }
64 
65 protected:
66     virtual size_t GetQueueSize() = 0;
67 
68 private:
69     const size_t queueMaxSize_;
70 };
71 
72 }  // namespace ark
73 
74 #endif  // PANDA_RUNTIME_THREAD_POOL_QUEUE_H_
75