15ccb8f90Sopenharmony_ci/* 25ccb8f90Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License. 55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at 65ccb8f90Sopenharmony_ci * 75ccb8f90Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85ccb8f90Sopenharmony_ci * 95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and 135ccb8f90Sopenharmony_ci * limitations under the License. 145ccb8f90Sopenharmony_ci */ 155ccb8f90Sopenharmony_ci 165ccb8f90Sopenharmony_ci#include "ffrt_utils.h" 175ccb8f90Sopenharmony_ci#include "power_log.h" 185ccb8f90Sopenharmony_ci 195ccb8f90Sopenharmony_cinamespace OHOS { 205ccb8f90Sopenharmony_cinamespace PowerMgr { 215ccb8f90Sopenharmony_civoid FFRTUtils::SubmitTask(const FFRTTask& task) 225ccb8f90Sopenharmony_ci{ 235ccb8f90Sopenharmony_ci ffrt::submit(task); 245ccb8f90Sopenharmony_ci} 255ccb8f90Sopenharmony_ci 265ccb8f90Sopenharmony_civoid FFRTUtils::SubmitTaskSync(const FFRTTask& task) 275ccb8f90Sopenharmony_ci{ 285ccb8f90Sopenharmony_ci ffrt::submit(task); 295ccb8f90Sopenharmony_ci ffrt::wait(); 305ccb8f90Sopenharmony_ci} 315ccb8f90Sopenharmony_ci 325ccb8f90Sopenharmony_civoid FFRTUtils::SubmitQueueTasks(const std::vector<FFRTTask>& tasks, FFRTQueue& queue) 335ccb8f90Sopenharmony_ci{ 345ccb8f90Sopenharmony_ci if (tasks.empty()) { 355ccb8f90Sopenharmony_ci return; 365ccb8f90Sopenharmony_ci } 375ccb8f90Sopenharmony_ci for (auto task : tasks) { 385ccb8f90Sopenharmony_ci queue.submit(task); 395ccb8f90Sopenharmony_ci } 405ccb8f90Sopenharmony_ci} 415ccb8f90Sopenharmony_ci 425ccb8f90Sopenharmony_ciFFRTHandle FFRTUtils::SubmitDelayTask(FFRTTask& task, uint32_t delayMs, FFRTQueue& queue) 435ccb8f90Sopenharmony_ci{ 445ccb8f90Sopenharmony_ci using namespace std::chrono; 455ccb8f90Sopenharmony_ci milliseconds ms(delayMs); 465ccb8f90Sopenharmony_ci microseconds us = duration_cast<microseconds>(ms); 475ccb8f90Sopenharmony_ci return queue.submit_h(task, ffrt::task_attr().delay(us.count())); 485ccb8f90Sopenharmony_ci} 495ccb8f90Sopenharmony_ci 505ccb8f90Sopenharmony_ciFFRTHandle FFRTUtils::SubmitDelayTask(FFRTTask& task, uint32_t delayMs, std::shared_ptr<FFRTQueue> queue) 515ccb8f90Sopenharmony_ci{ 525ccb8f90Sopenharmony_ci using namespace std::chrono; 535ccb8f90Sopenharmony_ci milliseconds ms(delayMs); 545ccb8f90Sopenharmony_ci microseconds us = duration_cast<microseconds>(ms); 555ccb8f90Sopenharmony_ci return queue->submit_h(task, ffrt::task_attr().delay(us.count())); 565ccb8f90Sopenharmony_ci} 575ccb8f90Sopenharmony_ci 585ccb8f90Sopenharmony_cibool FFRTUtils::SubmitTimeoutTask(const FFRTTask& task, uint32_t timeoutMs) 595ccb8f90Sopenharmony_ci{ 605ccb8f90Sopenharmony_ci ffrt::future<void> future = ffrt::async(task); 615ccb8f90Sopenharmony_ci auto status = future.wait_for(std::chrono::milliseconds(timeoutMs)); 625ccb8f90Sopenharmony_ci return status == ffrt::future_status::ready; 635ccb8f90Sopenharmony_ci} 645ccb8f90Sopenharmony_ci 655ccb8f90Sopenharmony_ciint FFRTUtils::CancelTask(FFRTHandle& handle, FFRTQueue& queue) 665ccb8f90Sopenharmony_ci{ 675ccb8f90Sopenharmony_ci return queue.cancel(handle); 685ccb8f90Sopenharmony_ci} 695ccb8f90Sopenharmony_ci 705ccb8f90Sopenharmony_ciint FFRTUtils::CancelTask(FFRTHandle& handle, std::shared_ptr<FFRTQueue> queue) 715ccb8f90Sopenharmony_ci{ 725ccb8f90Sopenharmony_ci return queue->cancel(handle); 735ccb8f90Sopenharmony_ci} 745ccb8f90Sopenharmony_ci 755ccb8f90Sopenharmony_civoid FFRTMutexMap::Lock(uint32_t mutexId) 765ccb8f90Sopenharmony_ci{ 775ccb8f90Sopenharmony_ci mutexMap_[mutexId].lock(); 785ccb8f90Sopenharmony_ci} 795ccb8f90Sopenharmony_ci 805ccb8f90Sopenharmony_civoid FFRTMutexMap::Unlock(uint32_t mutexId) 815ccb8f90Sopenharmony_ci{ 825ccb8f90Sopenharmony_ci mutexMap_[mutexId].unlock(); 835ccb8f90Sopenharmony_ci} 845ccb8f90Sopenharmony_ci 855ccb8f90Sopenharmony_ciFFRTTimer::FFRTTimer(): queue_("ffrt_timer") 865ccb8f90Sopenharmony_ci{ 875ccb8f90Sopenharmony_ci} 885ccb8f90Sopenharmony_ci 895ccb8f90Sopenharmony_ciFFRTTimer::FFRTTimer(const char *timer_name): queue_(timer_name) 905ccb8f90Sopenharmony_ci{ 915ccb8f90Sopenharmony_ci} 925ccb8f90Sopenharmony_ci 935ccb8f90Sopenharmony_ciFFRTTimer::~FFRTTimer() 945ccb8f90Sopenharmony_ci{ 955ccb8f90Sopenharmony_ci Clear(); 965ccb8f90Sopenharmony_ci // queue_ will be destructed thereafter. 975ccb8f90Sopenharmony_ci} 985ccb8f90Sopenharmony_ci 995ccb8f90Sopenharmony_civoid FFRTTimer::Clear() 1005ccb8f90Sopenharmony_ci{ 1015ccb8f90Sopenharmony_ci mutex_.lock(); 1025ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_UTIL, "FFRT Timer Clear"); 1035ccb8f90Sopenharmony_ci CancelAllTimerInner(); 1045ccb8f90Sopenharmony_ci handleMap_.clear(); 1055ccb8f90Sopenharmony_ci taskId_.clear(); 1065ccb8f90Sopenharmony_ci mutex_.unlock(); 1075ccb8f90Sopenharmony_ci} 1085ccb8f90Sopenharmony_ci 1095ccb8f90Sopenharmony_civoid FFRTTimer::CancelAllTimer() 1105ccb8f90Sopenharmony_ci{ 1115ccb8f90Sopenharmony_ci mutex_.lock(); 1125ccb8f90Sopenharmony_ci CancelAllTimerInner(); 1135ccb8f90Sopenharmony_ci mutex_.unlock(); 1145ccb8f90Sopenharmony_ci} 1155ccb8f90Sopenharmony_ci 1165ccb8f90Sopenharmony_civoid FFRTTimer::CancelTimer(uint32_t timerId) 1175ccb8f90Sopenharmony_ci{ 1185ccb8f90Sopenharmony_ci mutex_.lock(); 1195ccb8f90Sopenharmony_ci CancelTimerInner(timerId); 1205ccb8f90Sopenharmony_ci mutex_.unlock(); 1215ccb8f90Sopenharmony_ci} 1225ccb8f90Sopenharmony_ci 1235ccb8f90Sopenharmony_civoid FFRTTimer::SetTimer(uint32_t timerId, FFRTTask& task, uint32_t delayMs) 1245ccb8f90Sopenharmony_ci{ 1255ccb8f90Sopenharmony_ci mutex_.lock(); 1265ccb8f90Sopenharmony_ci CancelTimerInner(timerId); 1275ccb8f90Sopenharmony_ci ++taskId_[timerId]; 1285ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_UTIL, "Timer[%{public}u] Add Task[%{public}u] with delay = %{public}u", 1295ccb8f90Sopenharmony_ci timerId, taskId_[timerId], delayMs); 1305ccb8f90Sopenharmony_ci handleMap_[timerId] = FFRTUtils::SubmitDelayTask(task, delayMs, queue_); 1315ccb8f90Sopenharmony_ci mutex_.unlock(); 1325ccb8f90Sopenharmony_ci} 1335ccb8f90Sopenharmony_ci 1345ccb8f90Sopenharmony_ciuint32_t FFRTTimer::GetTaskId(uint32_t timerId) 1355ccb8f90Sopenharmony_ci{ 1365ccb8f90Sopenharmony_ci mutex_.lock(); 1375ccb8f90Sopenharmony_ci uint32_t id = taskId_[timerId]; 1385ccb8f90Sopenharmony_ci mutex_.unlock(); 1395ccb8f90Sopenharmony_ci return id; 1405ccb8f90Sopenharmony_ci} 1415ccb8f90Sopenharmony_ci 1425ccb8f90Sopenharmony_ciconst void* FFRTTimer::GetTaskHandlePtr(uint32_t timerId) 1435ccb8f90Sopenharmony_ci{ 1445ccb8f90Sopenharmony_ci std::lock_guard lock(mutex_); 1455ccb8f90Sopenharmony_ci // conversion function to void* defined in task.h 1465ccb8f90Sopenharmony_ci return static_cast<void*>(handleMap_[timerId]); 1475ccb8f90Sopenharmony_ci} 1485ccb8f90Sopenharmony_ci 1495ccb8f90Sopenharmony_ci/* inner functions must be called when mutex_ is locked */ 1505ccb8f90Sopenharmony_civoid FFRTTimer::CancelAllTimerInner() 1515ccb8f90Sopenharmony_ci{ 1525ccb8f90Sopenharmony_ci for (auto &p : handleMap_) { 1535ccb8f90Sopenharmony_ci if (p.second != nullptr) { 1545ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_UTIL, "Timer[%{public}u] Cancel Task[%{public}u]", p.first, taskId_[p.first]); 1555ccb8f90Sopenharmony_ci FFRTUtils::CancelTask(p.second, queue_); 1565ccb8f90Sopenharmony_ci p.second = nullptr; 1575ccb8f90Sopenharmony_ci } 1585ccb8f90Sopenharmony_ci } 1595ccb8f90Sopenharmony_ci} 1605ccb8f90Sopenharmony_ci 1615ccb8f90Sopenharmony_civoid FFRTTimer::CancelTimerInner(uint32_t timerId) 1625ccb8f90Sopenharmony_ci{ 1635ccb8f90Sopenharmony_ci if (handleMap_[timerId] != nullptr) { 1645ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_UTIL, "Timer[%{public}u] Cancel Task[%{public}u]", timerId, taskId_[timerId]); 1655ccb8f90Sopenharmony_ci FFRTUtils::CancelTask(handleMap_[timerId], queue_); 1665ccb8f90Sopenharmony_ci handleMap_[timerId] = nullptr; 1675ccb8f90Sopenharmony_ci } 1685ccb8f90Sopenharmony_ci} 1695ccb8f90Sopenharmony_ci 1705ccb8f90Sopenharmony_ci} // namespace PowerMgr 1715ccb8f90Sopenharmony_ci} // namespace OHOS