• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-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 #include "hks_ha_event_queue.h"
17 #include "hks_log.h"
18 #include "hks_template.h"
19 
Enqueue(uint32_t eventId,struct HksParamSet * paramSet)20 bool HksEventQueue::Enqueue(uint32_t eventId, struct HksParamSet *paramSet)
21 {
22     std::unique_lock<std::mutex> lock(queueMutex_);
23     HKS_IF_NULL_LOGE_RETURN(paramSet, false,
24         "HksParamSet is nullptr, cannot enqueue eventId: %" LOG_PUBLIC "u", eventId)
25 
26     // 1. Check if already stopped
27     HKS_IF_TRUE_LOGI_RETURN(stopped_, false, "Enqueue stopped")
28 
29     // 2. Check if the queue is full
30     HKS_IF_TRUE_LOGI_RETURN(queueItem_.size() >= queueCapacity_, false,
31         "Queue is full, cannot enqueue eventId: %" LOG_PUBLIC "u", eventId)
32 
33     // 3. Enqueue
34     queueItem_.emplace(HksEventQueueItem{eventId, paramSet});
35     notEmpty.notify_one();
36     return true;
37 }
38 
Dequeue(HksEventQueueItem & item)39 bool HksEventQueue::Dequeue(HksEventQueueItem& item)
40 {
41     std::unique_lock<std::mutex> lock(queueMutex_);
42 
43     // Wait until the queue is not empty or stopped
44     notEmpty.wait(lock, [this]() {
45         return (!queueItem_.empty()) || stopped_;
46     });
47 
48     HKS_IF_TRUE_LOGI_RETURN(stopped_ && queueItem_.empty(), false, "Dequeue stopped")
49 
50     item = std::move(queueItem_.front());
51     queueItem_.pop();
52 
53     return true;
54 }
55 
Stop()56 void HksEventQueue::Stop()
57 {
58     std::lock_guard<std::mutex> lock(queueMutex_);
59     stopped_ = true;
60     notEmpty.notify_all();
61 }
62 
Size() const63 uint32_t HksEventQueue::Size() const
64 {
65     return queueItem_.size();
66 }
67 
IsEmpty() const68 bool HksEventQueue::IsEmpty() const
69 {
70     return queueItem_.empty();
71 }