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 16#ifndef ECMASCRIPT_TOOLING_CLIENT_MANAGER_MESSAGE_MANAGER_H 17#define ECMASCRIPT_TOOLING_CLIENT_MANAGER_MESSAGE_MANAGER_H 18 19#include <iostream> 20 21namespace OHOS::ArkCompiler::Toolchain { 22class MessageManager final { 23public: 24 ~MessageManager() 25 { 26 uv_mutex_destroy(&messageMutex_); 27 } 28 29 static MessageManager& getInstance() 30 { 31 static MessageManager instance; 32 return instance; 33 } 34 35 void MessagePush(uint32_t sessionId, std::string& message) 36 { 37 uv_mutex_lock(&messageMutex_); 38 messageQue_.push(std::make_pair(sessionId, message)); 39 uv_mutex_unlock(&messageMutex_); 40 } 41 42 bool MessagePop(uint32_t& sessionId, std::string& message) 43 { 44 uv_mutex_lock(&messageMutex_); 45 if (messageQue_.empty()) { 46 uv_mutex_unlock(&messageMutex_); 47 return false; 48 } 49 50 sessionId = messageQue_.front().first; 51 message = messageQue_.front().second; 52 messageQue_.pop(); 53 54 uv_mutex_unlock(&messageMutex_); 55 return true; 56 } 57 58private: 59 MessageManager() 60 { 61 uv_mutex_init(&messageMutex_); 62 } 63 MessageManager(const MessageManager&) = delete; 64 MessageManager& operator=(const MessageManager&) = delete; 65 66 uv_mutex_t messageMutex_; 67 std::queue<std::pair<uint32_t, std::string>> messageQue_; 68}; 69} // OHOS::ArkCompiler::Toolchain 70#endif