| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2022 - 2025 * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "thread.h" |
| 22 | |
| 23 | #include <cassert> |
| 24 | #include <memory> |
| 25 | |
| 26 | #if defined( __EMSCRIPTEN__ ) && !defined( __EMSCRIPTEN_PTHREADS__ ) |
| 27 | namespace |
| 28 | { |
| 29 | class MutexUnlocker |
| 30 | { |
| 31 | public: |
| 32 | explicit MutexUnlocker( std::mutex & mutex ) |
| 33 | : _mutex( mutex ) |
| 34 | { |
| 35 | _mutex.unlock(); |
| 36 | } |
| 37 | |
| 38 | MutexUnlocker( const MutexUnlocker & ) = delete; |
| 39 | |
| 40 | ~MutexUnlocker() |
| 41 | { |
| 42 | _mutex.lock(); |
| 43 | } |
| 44 | |
| 45 | MutexUnlocker & operator=( const MutexUnlocker & ) = delete; |
| 46 | |
| 47 | private: |
| 48 | std::mutex & _mutex; |
| 49 | }; |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | namespace MultiThreading |
| 54 | { |
| 55 | void AsyncManager::createWorker() |
| 56 | { |
| 57 | #if !defined( __EMSCRIPTEN__ ) || defined( __EMSCRIPTEN_PTHREADS__ ) |
| 58 | if ( !_worker ) { |
| 59 | _runFlag = true; |
| 60 | _worker = std::make_unique<std::thread>( AsyncManager::_workerThread, this ); |
| 61 | |
| 62 | { |
| 63 | std::unique_lock<std::mutex> lock( _mutex ); |
| 64 | |
| 65 | _masterNotification.wait( lock, [this] { return !_runFlag; } ); |
| 66 | } |
| 67 | } |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | void AsyncManager::stopWorker() |
| 72 | { |
| 73 | #if defined( __EMSCRIPTEN__ ) && !defined( __EMSCRIPTEN_PTHREADS__ ) |
| 74 | assert( !_worker ); |
| 75 | #else |
| 76 | if ( _worker ) { |
| 77 | { |
| 78 | const std::scoped_lock<std::mutex> lock( _mutex ); |
| 79 | |
| 80 | _exitFlag = true; |
| 81 | _runFlag = true; |
| 82 | } |
| 83 | |
| 84 | _workerNotification.notify_all(); |
| 85 | |
| 86 | _worker->join(); |
| 87 | _worker.reset(); |
| 88 | } |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | void AsyncManager::notifyWorker() |
| 93 | { |
| 94 | _runFlag = true; |
| 95 | |
| 96 | #if defined( __EMSCRIPTEN__ ) && !defined( __EMSCRIPTEN_PTHREADS__ ) |
| 97 | assert( !_exitFlag ); |
| 98 | |
| 99 | while ( _runFlag ) { |
| 100 | const bool moreTasks = prepareTask(); |
| 101 | if ( !moreTasks ) { |
| 102 | _runFlag = false; |
| 103 | } |
| 104 | |
| 105 | { |
| 106 | // In accordance with the contract, the _mutex should NOT be acquired while |
| 107 | // calling the executeTask() - even if its acquisition is in fact a snake oil. |
| 108 | MutexUnlocker unlocker( _mutex ); |
| 109 | |
| 110 | executeTask(); |
| 111 | } |
| 112 | } |
| 113 | #else |
| 114 | _workerNotification.notify_all(); |
| 115 | #endif |
| 116 | } |
| 117 | |
| 118 | void AsyncManager::_workerThread( AsyncManager * manager ) |
| 119 | { |
| 120 | assert( manager != nullptr ); |
| 121 | |
| 122 | { |
| 123 | const std::scoped_lock<std::mutex> lock( manager->_mutex ); |
| 124 | |
| 125 | manager->_runFlag = false; |
| 126 | } |
| 127 | |
| 128 | manager->_masterNotification.notify_one(); |
| 129 | |
| 130 | while ( !manager->_exitFlag ) { |
| 131 | { |
| 132 | std::unique_lock<std::mutex> lock( manager->_mutex ); |
| 133 | |
| 134 | manager->_workerNotification.wait( lock, [manager] { return manager->_runFlag; } ); |
| 135 | } |
| 136 | |
| 137 | if ( manager->_exitFlag ) { |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | { |
| 142 | const std::scoped_lock<std::mutex> lock( manager->_mutex ); |
| 143 | |
| 144 | const bool moreTasks = manager->prepareTask(); |
| 145 | if ( !moreTasks ) { |
| 146 | manager->_runFlag = false; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | manager->executeTask(); |
| 151 | } |
| 152 | } |
| 153 | } |