Line data Source code
1 : // 2 : // Copyright 2024 OpenModelViewer Authors 3 : // 4 : // Licensed under the Apache License, Version 2.0 (the "License"); 5 : // you may not use this file except in compliance with the License. 6 : // You may obtain a copy of the License at 7 : // 8 : // http://www.apache.org/licenses/LICENSE-2.0 9 : // 10 : // Unless required by applicable law or agreed to in writing, software 11 : // distributed under the License is distributed on an "AS IS" BASIS, 12 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 : // See the License for the specific language governing permissions and 14 : // limitations under the License. 15 : // 16 : 17 : #include "openmodelviewer/core/async/task_scheduler.hpp" 18 : 19 : namespace openmodelviewer::core::async 20 : { 21 58 : TaskScheduler::TaskScheduler(size_t reserved) 22 : { 23 58 : size_t optimalPoolSize = ThreadPool::computeOptimalPoolSize(reserved); 24 58 : m_pool = std::make_unique<ThreadPool>(optimalPoolSize); 25 : 26 58 : m_running = false; 27 58 : } 28 : 29 66 : TaskScheduler::~TaskScheduler() 30 : { 31 58 : this->stop(); 32 66 : } 33 : 34 45 : bool TaskScheduler::start() 35 : { 36 45 : if (m_running.load()) 37 : { 38 0 : return false; 39 : } 40 : 41 45 : if (!m_pool->start()) 42 : { 43 0 : return false; 44 : } 45 : 46 45 : m_running = true; 47 45 : return true; 48 : } 49 : 50 101 : void TaskScheduler::stop() 51 : { 52 101 : if (!m_running.exchange(false)) 53 : { 54 56 : return; 55 : } 56 : 57 45 : m_pool->stop(); 58 : } 59 : 60 0 : bool TaskScheduler::isRunning() const noexcept 61 : { 62 0 : return m_running.load(); 63 : } 64 : 65 75 : std::shared_ptr<ITaskData> TaskScheduler::getTaskData(const TaskHandle& handle) const 66 : { 67 75 : std::lock_guard lock(m_tasksMutex); 68 : 69 75 : auto it = m_tasks.find(handle); 70 75 : if (it != m_tasks.end()) 71 : { 72 75 : return it->second; 73 : } 74 : 75 0 : return nullptr; 76 75 : } 77 : 78 3 : bool TaskScheduler::isAvailable(const TaskHandle& handle) const 79 : { 80 3 : std::lock_guard lock(m_tasksMutex); 81 : 82 6 : return m_tasks.find(handle) != m_tasks.end(); 83 3 : } 84 : }