122736c2fSopenharmony_ci/*
222736c2fSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
322736c2fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
422736c2fSopenharmony_ci * you may not use this file except in compliance with the License.
522736c2fSopenharmony_ci * You may obtain a copy of the License at
622736c2fSopenharmony_ci *
722736c2fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
822736c2fSopenharmony_ci *
922736c2fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1022736c2fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1122736c2fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1222736c2fSopenharmony_ci * See the License for the specific language governing permissions and
1322736c2fSopenharmony_ci * limitations under the License.
1422736c2fSopenharmony_ci */
1522736c2fSopenharmony_ci
1622736c2fSopenharmony_ci#include "message_handler.h"
1722736c2fSopenharmony_ci
1822736c2fSopenharmony_cinamespace OHOS {
1922736c2fSopenharmony_cinamespace MiscServices {
2022736c2fSopenharmony_cistd::mutex MessageHandler::handlerMutex_;
2122736c2fSopenharmony_ci
2222736c2fSopenharmony_ciMessageHandler::MessageHandler()
2322736c2fSopenharmony_ci{
2422736c2fSopenharmony_ci}
2522736c2fSopenharmony_ci
2622736c2fSopenharmony_ciMessageHandler::~MessageHandler()
2722736c2fSopenharmony_ci{
2822736c2fSopenharmony_ci    std::unique_lock<std::mutex> lock(mMutex);
2922736c2fSopenharmony_ci    while (!mQueue.empty()) {
3022736c2fSopenharmony_ci        Message *msg = mQueue.front();
3122736c2fSopenharmony_ci        mQueue.pop();
3222736c2fSopenharmony_ci        delete msg;
3322736c2fSopenharmony_ci        msg = nullptr;
3422736c2fSopenharmony_ci    }
3522736c2fSopenharmony_ci}
3622736c2fSopenharmony_ci
3722736c2fSopenharmony_ci/*! Send a message
3822736c2fSopenharmony_ci * @param msg a message to be sent
3922736c2fSopenharmony_ci * @note the msg pointer should not be freed by the caller
4022736c2fSopenharmony_ci */
4122736c2fSopenharmony_civoid MessageHandler::SendMessage(Message *msg)
4222736c2fSopenharmony_ci{
4322736c2fSopenharmony_ci    std::unique_lock<std::mutex> lock(mMutex);
4422736c2fSopenharmony_ci    mQueue.push(msg);
4522736c2fSopenharmony_ci    mCV.notify_one();
4622736c2fSopenharmony_ci}
4722736c2fSopenharmony_ci
4822736c2fSopenharmony_ci/*! Get a message
4922736c2fSopenharmony_ci * @return a pointer referred to an object of message
5022736c2fSopenharmony_ci * @note the returned pointer should be freed by the caller.
5122736c2fSopenharmony_ci */
5222736c2fSopenharmony_ciMessage *MessageHandler::GetMessage()
5322736c2fSopenharmony_ci{
5422736c2fSopenharmony_ci    std::unique_lock<std::mutex> lock(mMutex);
5522736c2fSopenharmony_ci    mCV.wait(lock, [this] { return !this->mQueue.empty(); });
5622736c2fSopenharmony_ci    Message *msg = reinterpret_cast<Message *>(mQueue.front());
5722736c2fSopenharmony_ci    mQueue.pop();
5822736c2fSopenharmony_ci    return msg;
5922736c2fSopenharmony_ci}
6022736c2fSopenharmony_ci
6122736c2fSopenharmony_ci/*! The single instance of MessageHandler in the service
6222736c2fSopenharmony_ci * @return the pointer referred to an object.
6322736c2fSopenharmony_ci */
6422736c2fSopenharmony_ciMessageHandler *MessageHandler::Instance()
6522736c2fSopenharmony_ci{
6622736c2fSopenharmony_ci    static MessageHandler *handler = nullptr;
6722736c2fSopenharmony_ci    if (handler == nullptr) {
6822736c2fSopenharmony_ci        std::unique_lock<std::mutex> lock(handlerMutex_);
6922736c2fSopenharmony_ci        if (handler == nullptr) {
7022736c2fSopenharmony_ci            handler = new MessageHandler();
7122736c2fSopenharmony_ci            return handler;
7222736c2fSopenharmony_ci        }
7322736c2fSopenharmony_ci    }
7422736c2fSopenharmony_ci    return handler;
7522736c2fSopenharmony_ci}
7622736c2fSopenharmony_ci} // namespace MiscServices
7722736c2fSopenharmony_ci} // namespace OHOS
78