| 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 | #pragma once |
| 22 | |
| 23 | #include <atomic> |
| 24 | #include <condition_variable> |
| 25 | #include <memory> |
| 26 | #include <mutex> |
| 27 | #include <thread> |
| 28 | |
| 29 | namespace MultiThreading |
| 30 | { |
| 31 | class AsyncManager |
| 32 | { |
| 33 | public: |
| 34 | AsyncManager() = default; |
| 35 | AsyncManager( const AsyncManager & ) = delete; |
| 36 | |
| 37 | virtual ~AsyncManager() = default; |
| 38 | |
| 39 | AsyncManager & operator=( const AsyncManager & ) = delete; |
| 40 | |
| 41 | // Create the worker thread if it doesn't exist yet. Both createWorker() and stopWorker() are not |
| 42 | // designed to be executed concurrently. |
| 43 | void createWorker(); |
| 44 | |
| 45 | // Stop and join the worker thread. This cannot be done in the destructor (directly or indirectly) due |
| 46 | // to the potential race on the vptr since this class has virtual methods that could be called from the |
| 47 | // worker thread. |
| 48 | void stopWorker(); |
| 49 | |
| 50 | protected: |
| 51 | std::mutex _mutex; |
| 52 | |
| 53 | // Notify the worker thread about a new task. The _mutex should be acquired while calling this method. |
| 54 | void notifyWorker(); |
| 55 | |
| 56 | // Prepare the next task. The _mutex will be acquired while calling this method. Returns true if more tasks are available. |
| 57 | virtual bool prepareTask() = 0; |
| 58 | |
| 59 | // Execute the next task. NOTE WELL: the _mutex will NOT be acquired while calling this method. |
| 60 | virtual void executeTask() = 0; |
| 61 | |
| 62 | private: |
| 63 | std::unique_ptr<std::thread> _worker; |
| 64 | |
| 65 | std::condition_variable _masterNotification; |
| 66 | std::condition_variable _workerNotification; |
| 67 | |
| 68 | std::atomic<bool> _exitFlag{ false }; |
| 69 | bool _runFlag{ false }; |
| 70 | |
| 71 | static void _workerThread( AsyncManager * manager ); |
| 72 | }; |
| 73 | } |