15490a39dSopenharmony_ci/* 25490a39dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 35490a39dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45490a39dSopenharmony_ci * you may not use this file except in compliance with the License. 55490a39dSopenharmony_ci * You may obtain a copy of the License at 65490a39dSopenharmony_ci * 75490a39dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85490a39dSopenharmony_ci * 95490a39dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105490a39dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115490a39dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125490a39dSopenharmony_ci * See the License for the specific language governing permissions and 135490a39dSopenharmony_ci * limitations under the License. 145490a39dSopenharmony_ci */ 155490a39dSopenharmony_ci#include "message_queue.h" 165490a39dSopenharmony_ci#include "intell_voice_log.h" 175490a39dSopenharmony_ci 185490a39dSopenharmony_ci#define LOG_TAG "MesaageQueue" 195490a39dSopenharmony_ci 205490a39dSopenharmony_ciusing namespace std; 215490a39dSopenharmony_ci 225490a39dSopenharmony_cinamespace OHOS { 235490a39dSopenharmony_cinamespace IntellVoiceUtils { 245490a39dSopenharmony_ciMessage::Message(uint32_t what) : what_(what), arg1_(0), arg2_(0) 255490a39dSopenharmony_ci{ 265490a39dSopenharmony_ci} 275490a39dSopenharmony_ci 285490a39dSopenharmony_ciMessage::Message(uint32_t what, int32_t arg1) : what_(what), arg1_(arg1) 295490a39dSopenharmony_ci{ 305490a39dSopenharmony_ci} 315490a39dSopenharmony_ci 325490a39dSopenharmony_ciMessage::Message(uint32_t what, int32_t arg1, int32_t arg2, float arg3) 335490a39dSopenharmony_ci : what_(what), arg1_(arg1), arg2_(arg2), arg3_(arg3) 345490a39dSopenharmony_ci{ 355490a39dSopenharmony_ci} 365490a39dSopenharmony_ci 375490a39dSopenharmony_ciMessage::Message(uint32_t what, int32_t arg1, int32_t arg2, const std::string &obj) 385490a39dSopenharmony_ci : what_(what), arg1_(arg1), arg2_(arg2), obj_(obj) 395490a39dSopenharmony_ci{ 405490a39dSopenharmony_ci} 415490a39dSopenharmony_ci 425490a39dSopenharmony_ciMessage::Message(uint32_t what, int32_t arg1, int32_t arg2, float arg3, const std::string &obj) 435490a39dSopenharmony_ci : what_(what), arg1_(arg1), arg2_(arg2), arg3_(arg3), obj_(obj) 445490a39dSopenharmony_ci{ 455490a39dSopenharmony_ci} 465490a39dSopenharmony_ci 475490a39dSopenharmony_ciMessage::Message(uint32_t what, std::shared_ptr<void> obj2) : what_(what), obj2_(obj2) 485490a39dSopenharmony_ci{ 495490a39dSopenharmony_ci} 505490a39dSopenharmony_ci 515490a39dSopenharmony_ciMessage::Message(uint32_t what, std::shared_ptr<void> obj2, std::shared_ptr<void> obj3) 525490a39dSopenharmony_ci : what_(what), obj2_(obj2), obj3_(obj3) 535490a39dSopenharmony_ci{ 545490a39dSopenharmony_ci} 555490a39dSopenharmony_ci 565490a39dSopenharmony_ciMessage::Message(uint32_t what, void* voidPtr) : what_(what), voidPtr_(voidPtr) 575490a39dSopenharmony_ci{ 585490a39dSopenharmony_ci} 595490a39dSopenharmony_ci 605490a39dSopenharmony_ciMessage::Message(uint32_t what, int32_t arg1, void* voidPtr) : what_(what), arg1_(arg1), voidPtr_(voidPtr) 615490a39dSopenharmony_ci{ 625490a39dSopenharmony_ci} 635490a39dSopenharmony_ci 645490a39dSopenharmony_ciMessage::~Message() 655490a39dSopenharmony_ci{ 665490a39dSopenharmony_ci voidPtr_ = nullptr; 675490a39dSopenharmony_ci} 685490a39dSopenharmony_ci 695490a39dSopenharmony_ciMessageQueue::MessageQueue(uint32_t size) : size_(size), lock_(PTHREAD_MUTEX_INITIALIZER) 705490a39dSopenharmony_ci{ 715490a39dSopenharmony_ci pthread_condattr_t attr; 725490a39dSopenharmony_ci int result = pthread_condattr_init(&attr); 735490a39dSopenharmony_ci result += pthread_cond_init(&cond_, &attr); 745490a39dSopenharmony_ci result += pthread_condattr_destroy(&attr); 755490a39dSopenharmony_ci if (result != 0) { 765490a39dSopenharmony_ci INTELL_VOICE_LOG_ERROR("message queue construct init cond failed"); 775490a39dSopenharmony_ci } 785490a39dSopenharmony_ci} 795490a39dSopenharmony_ci 805490a39dSopenharmony_ciMessageQueue::~MessageQueue() 815490a39dSopenharmony_ci{ 825490a39dSopenharmony_ci int result = pthread_mutex_destroy(&lock_); 835490a39dSopenharmony_ci result += pthread_cond_destroy(&cond_); 845490a39dSopenharmony_ci if (result != 0) { 855490a39dSopenharmony_ci INTELL_VOICE_LOG_ERROR("message queue deconstruct destroy cond failed"); 865490a39dSopenharmony_ci } 875490a39dSopenharmony_ci} 885490a39dSopenharmony_ci 895490a39dSopenharmony_cishared_ptr<Message> MessageQueue::ReceiveMsg() 905490a39dSopenharmony_ci{ 915490a39dSopenharmony_ci shared_ptr<Message> msg = nullptr; 925490a39dSopenharmony_ci pthread_mutex_lock(&lock_); 935490a39dSopenharmony_ci while (queue_.empty()) { 945490a39dSopenharmony_ci pthread_cond_wait(&cond_, &lock_); 955490a39dSopenharmony_ci } 965490a39dSopenharmony_ci 975490a39dSopenharmony_ci msg = queue_.front(); 985490a39dSopenharmony_ci queue_.pop(); 995490a39dSopenharmony_ci pthread_mutex_unlock(&lock_); 1005490a39dSopenharmony_ci 1015490a39dSopenharmony_ci return msg; 1025490a39dSopenharmony_ci} 1035490a39dSopenharmony_ci 1045490a39dSopenharmony_cibool MessageQueue::SendMsg(shared_ptr<Message> msg) 1055490a39dSopenharmony_ci{ 1065490a39dSopenharmony_ci pthread_mutex_lock(&lock_); 1075490a39dSopenharmony_ci if (static_cast<uint32_t>(queue_.size()) >= size_ || msg == nullptr) { 1085490a39dSopenharmony_ci INTELL_VOICE_LOG_WARN("send message failed, msg queue full(%{public}d)", static_cast<int>(queue_.size())); 1095490a39dSopenharmony_ci pthread_mutex_unlock(&lock_); 1105490a39dSopenharmony_ci return false; 1115490a39dSopenharmony_ci } 1125490a39dSopenharmony_ci 1135490a39dSopenharmony_ci try { 1145490a39dSopenharmony_ci queue_.push(msg); 1155490a39dSopenharmony_ci } catch (const std::length_error& err) { 1165490a39dSopenharmony_ci INTELL_VOICE_LOG_ERROR("messagequeue push, length error"); 1175490a39dSopenharmony_ci pthread_mutex_unlock(&lock_); 1185490a39dSopenharmony_ci return false; 1195490a39dSopenharmony_ci } 1205490a39dSopenharmony_ci 1215490a39dSopenharmony_ci pthread_cond_signal(&cond_); 1225490a39dSopenharmony_ci pthread_mutex_unlock(&lock_); 1235490a39dSopenharmony_ci 1245490a39dSopenharmony_ci return true; 1255490a39dSopenharmony_ci} 1265490a39dSopenharmony_ci 1275490a39dSopenharmony_civoid MessageQueue::Clear() 1285490a39dSopenharmony_ci{ 1295490a39dSopenharmony_ci pthread_mutex_lock(&lock_); 1305490a39dSopenharmony_ci while (!queue_.empty()) { 1315490a39dSopenharmony_ci queue_.pop(); 1325490a39dSopenharmony_ci } 1335490a39dSopenharmony_ci pthread_mutex_unlock(&lock_); 1345490a39dSopenharmony_ci} 1355490a39dSopenharmony_ci} 1365490a39dSopenharmony_ci} 137