1 /*
2 * Copyright (c) 2023 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 "message_queue.h"
16 #include "intell_voice_log.h"
17
18 #define LOG_TAG "MesaageQueue"
19
20 using namespace std;
21
22 namespace OHOS {
23 namespace IntellVoiceUtils {
Message(uint32_t what)24 Message::Message(uint32_t what)
25 {
26 mWhat = what;
27 arg1 = 0;
28 arg2 = 0;
29 }
30
Message(uint32_t what,int32_t arg1)31 Message::Message(uint32_t what, int32_t arg1)
32 {
33 mWhat = what;
34 this->arg1 = arg1;
35 }
36
Message(uint32_t what,int32_t arg1,int32_t arg2,float arg3)37 Message::Message(uint32_t what, int32_t arg1, int32_t arg2, float arg3)
38 {
39 mWhat = what;
40 this->arg1 = arg1;
41 this->arg2 = arg2;
42 this->arg3 = arg3;
43 }
44
Message(uint32_t what,int32_t arg1,int32_t arg2,const std::string & obj)45 Message::Message(uint32_t what, int32_t arg1, int32_t arg2, const std::string &obj)
46 {
47 mWhat = what;
48 this->arg1 = arg1;
49 this->arg2 = arg2;
50 this->obj = obj;
51 }
52
Message(uint32_t what,int32_t arg1,int32_t arg2,float arg3,const std::string & obj)53 Message::Message(uint32_t what, int32_t arg1, int32_t arg2, float arg3, const std::string &obj)
54 {
55 mWhat = what;
56 this->arg1 = arg1;
57 this->arg2 = arg2;
58 this->arg3 = arg3;
59 this->obj = obj;
60 }
61
Message(uint32_t what,std::shared_ptr<void> obj2)62 Message::Message(uint32_t what, std::shared_ptr<void> obj2)
63 {
64 mWhat = what;
65 this->obj2 = obj2;
66 }
67
Message(uint32_t what,std::shared_ptr<void> obj2,std::shared_ptr<void> obj3)68 Message::Message(uint32_t what, std::shared_ptr<void> obj2, std::shared_ptr<void> obj3)
69 {
70 mWhat = what;
71 this->obj2 = obj2;
72 this->obj3 = obj3;
73 }
74
Message(uint32_t what,void * voidPtr)75 Message::Message(uint32_t what, void* voidPtr)
76 {
77 mWhat = what;
78 this->voidPtr = voidPtr;
79 }
80
Message(uint32_t what,void * voidPtr,int32_t arg1)81 Message::Message(uint32_t what, void* voidPtr, int32_t arg1)
82 {
83 mWhat = what;
84 this->voidPtr = voidPtr;
85 this->arg1 = arg1;
86 }
87
~Message()88 Message::~Message()
89 {
90 voidPtr = nullptr;
91 }
92
MessageQueue(uint32_t size)93 MessageQueue::MessageQueue(uint32_t size) : mSize(size), mLock(PTHREAD_MUTEX_INITIALIZER)
94 {
95 pthread_condattr_t attr;
96 int result = pthread_condattr_init(&attr);
97 result += pthread_cond_init(&mCond, &attr);
98 result += pthread_condattr_destroy(&attr);
99 if (result != 0) {
100 INTELL_VOICE_LOG_ERROR("message queue construct init cond failed");
101 }
102 }
103
~MessageQueue()104 MessageQueue::~MessageQueue()
105 {
106 int result = pthread_mutex_destroy(&mLock);
107 result += pthread_cond_destroy(&mCond);
108 if (result != 0) {
109 INTELL_VOICE_LOG_ERROR("message queue deconstruct destroy cond failed");
110 }
111 }
112
ReceiveMsg()113 shared_ptr<Message> MessageQueue::ReceiveMsg()
114 {
115 shared_ptr<Message> msg = nullptr;
116 pthread_mutex_lock(&mLock);
117 while (mQueue.empty()) {
118 pthread_cond_wait(&mCond, &mLock);
119 }
120
121 msg = mQueue.front();
122 mQueue.pop();
123 pthread_mutex_unlock(&mLock);
124
125 return msg;
126 }
127
SendMsg(shared_ptr<Message> msg)128 bool MessageQueue::SendMsg(shared_ptr<Message> msg)
129 {
130 pthread_mutex_lock(&mLock);
131 if (static_cast<uint32_t>(mQueue.size()) >= mSize || msg == nullptr) {
132 INTELL_VOICE_LOG_WARN("send message failed, msg queue full(qsize %{public}d)", static_cast<int>(mQueue.size()));
133 pthread_mutex_unlock(&mLock);
134 return false;
135 }
136
137 try {
138 mQueue.push(msg);
139 } catch (const std::length_error& err) {
140 INTELL_VOICE_LOG_ERROR("messagequeue push, length error");
141 pthread_mutex_unlock(&mLock);
142 return false;
143 }
144
145 pthread_cond_signal(&mCond);
146 pthread_mutex_unlock(&mLock);
147
148 return true;
149 }
150
Clear()151 void MessageQueue::Clear()
152 {
153 pthread_mutex_lock(&mLock);
154 while (!mQueue.empty()) {
155 mQueue.pop();
156 }
157 pthread_mutex_unlock(&mLock);
158 }
159 }
160 }
161