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 #include "runner_pool.h" 17 18 #include "string_utils.h" 19 #include "telephony_log_wrapper.h" 20 #include "telephony_types.h" 21 22 namespace OHOS { 23 namespace Telephony { 24 using namespace std; 25 RunnerPool RunnerPool::runnerPool_; 26 GetInstance()27RunnerPool &RunnerPool::GetInstance() 28 { 29 return runnerPool_; 30 } 31 Init()32void RunnerPool::Init() 33 { 34 if (isInit_) { 35 TELEPHONY_LOGI("RunnerPool in sms_mms has init"); 36 return; 37 } 38 smsCommonRunner_ = CreateRunner("smsCommonRunner"); 39 if (smsCommonRunner_ == nullptr) { 40 return; 41 } 42 for (int32_t slotId = 0; slotId < SIM_SLOT_COUNT; ++slotId) { 43 auto runnerTmp = CreateRunner("smsSendReceiveRunner_" + to_string(slotId)); 44 if (runnerTmp == nullptr) { 45 return; 46 } 47 smsSendReceiveRunnerMap_.insert(make_pair(slotId, runnerTmp)); 48 } 49 isInit_ = true; 50 TELEPHONY_LOGI("RunnerPool in sms_mms init success"); 51 } 52 CreateRunner(const std::string & name)53std::shared_ptr<AppExecFwk::EventRunner> RunnerPool::CreateRunner(const std::string &name) 54 { 55 auto runner = AppExecFwk::EventRunner::Create(name); 56 if (runner == nullptr) { 57 TELEPHONY_LOGE("%{public}s runner create thread fail!", name.c_str()); 58 return nullptr; 59 } 60 runner->Run(); 61 return runner; 62 } 63 GetSmsCommonRunner()64std::shared_ptr<AppExecFwk::EventRunner> RunnerPool::GetSmsCommonRunner() 65 { 66 return smsCommonRunner_; 67 } 68 GetSmsSendReceiveRunnerBySlotId(const int32_t slotId)69std::shared_ptr<AppExecFwk::EventRunner> RunnerPool::GetSmsSendReceiveRunnerBySlotId(const int32_t slotId) 70 { 71 std::lock_guard<std::mutex> lock(mutex_); 72 std::map<uint32_t, std::shared_ptr<AppExecFwk::EventRunner>>::iterator iter = smsSendReceiveRunnerMap_.find(slotId); 73 if (iter != smsSendReceiveRunnerMap_.end()) { 74 return iter->second; 75 } 76 return nullptr; 77 } 78 } // namespace Telephony 79 } // namespace OHOS 80