• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 LIBPANDABASE_TASKMANAGER_UTILS_TWO_LOCK_QUEUE
17 #define LIBPANDABASE_TASKMANAGER_UTILS_TWO_LOCK_QUEUE
18 
19 #include "libpandabase/taskmanager/utils/sp_sc_lock_free_queue.h"
20 #include "libpandabase/os/mutex.h"
21 
22 namespace ark::taskmanager::internal {
23 
24 template <class T, class Allocator>
25 class TwoLockQueue {
26 public:
Push(T && val)27     void Push(T &&val)
28     {
29         os::memory::LockHolder lh(pushLock_);
30         queue_.Push(std::move(val));
31     }
32 
Pop()33     T Pop()
34     {
35         os::memory::LockHolder lh(popLock_);
36         return queue_.Pop();
37     }
38 
TryPop(T * val)39     bool TryPop(T *val)
40     {
41         os::memory::LockHolder lh(popLock_);
42         return queue_.TryPop(val);
43     }
44 
IsEmpty()45     bool IsEmpty() const
46     {
47         return queue_.IsEmpty();
48     }
49 
Size()50     size_t Size() const
51     {
52         return queue_.Size();
53     }
54 
55 private:
56     using InternalTaskQueue = SPSCLockFreeQueue<T, Allocator>;
57 
58     mutable os::memory::Mutex pushLock_;
59     mutable os::memory::Mutex popLock_;
60     InternalTaskQueue queue_;
61 };
62 
63 }  // namespace ark::taskmanager::internal
64 
65 #endif  // LIBPANDABASE_TASKMANAGER_UTILS_TWO_LOCK_QUEUE
66