1 /*
2 * Copyright (c) 2021 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 #include "event_dispatch_queue.h"
16
17 #include <algorithm>
18 #include <memory>
19
20 #include "file_util.h"
21 #include "logger.h"
22 #include "plugin.h"
23 #include "thread_util.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 DEFINE_LOG_TAG("HiView-EventDispatchQueue");
EventDispatchQueue(const std::string & name,Event::ManageType type,HiviewContext * context)28 EventDispatchQueue::EventDispatchQueue(const std::string& name, Event::ManageType type, HiviewContext* context)
29 : stop_(false), isRunning_(false), threadName_(name), type_(type), context_(context)
30 {}
31
~EventDispatchQueue()32 EventDispatchQueue::~EventDispatchQueue()
33 {
34 Stop();
35 }
36
Run()37 void EventDispatchQueue::Run()
38 {
39 const int threadNameLen = 15;
40 Thread::SetThreadDescription(threadName_.substr(0, threadNameLen));
41 isRunning_ = true;
42 while (true) {
43 std::shared_ptr<Event> event = nullptr;
44 {
45 std::unique_lock<std::mutex> lock(mutexLock_);
46 while (pendingEvents_.empty()) {
47 condition_.wait(lock);
48 if (stop_) {
49 return;
50 }
51 }
52 event = pendingEvents_.front();
53 pendingEvents_.pop_front();
54 }
55
56 if (event == nullptr) {
57 continue;
58 }
59
60 if (type_ == Event::ManageType::UNORDERED) {
61 ProcessUnorderedEvent(*(event.get()));
62 }
63
64 if (stop_) {
65 break;
66 }
67 }
68 }
69
ProcessUnorderedEvent(const Event & event)70 void EventDispatchQueue::ProcessUnorderedEvent(const Event& event)
71 {
72 auto eventName = event.domain_ + "_" + event.eventName_;
73 auto listeners = context_->GetListenerInfo(event.messageType_, eventName, event.eventId_);
74 for (auto& tmp : listeners) {
75 auto listener = tmp.lock();
76 if (listener == nullptr) {
77 continue;
78 }
79 if (listener->isPlugin) {
80 auto ptr = listener->plugin.lock();
81 if (ptr != nullptr) {
82 ptr->OnEventListeningCallback(event);
83 }
84 } else {
85 auto ptr = listener->listener.lock();
86 if (ptr != nullptr) {
87 ptr->OnUnorderedEvent(event);
88 }
89 }
90 }
91 }
92
Stop()93 void EventDispatchQueue::Stop()
94 {
95 stop_ = true;
96 condition_.notify_all();
97 if (thread_ != nullptr && thread_->joinable()) {
98 thread_->join();
99 }
100 isRunning_ = false;
101 }
102
Start()103 void EventDispatchQueue::Start()
104 {
105 std::unique_lock<std::mutex> lock(mutexLock_);
106 if (!IsRunning()) {
107 thread_ = std::make_unique<std::thread>(&EventDispatchQueue::Run, this);
108 }
109 }
110
Enqueue(std::shared_ptr<Event> event)111 void EventDispatchQueue::Enqueue(std::shared_ptr<Event> event)
112 {
113 HIVIEW_LOGD("EventDispatchQueue Enqueue");
114 std::unique_lock<std::mutex> lock(mutexLock_);
115 pendingEvents_.push_back(std::move(event));
116 condition_.notify_one();
117 }
118
GetWaitQueueSize() const119 int EventDispatchQueue::GetWaitQueueSize() const
120 {
121 return pendingEvents_.size();
122 }
123 } // namespace HiviewDFX
124 } // namespace OHOS