* @brief A simple counter-based manager for parallel task execution. * * The task processing execution consists of: * * * A single-threaded init stage. * * A multi-threaded processing stage. * * A condition variable so threads can wait for processing completion. * * The init stage will be executed by the first thread to arrive in the critical section, there is * no main thread
| 95 | * manager->term(<lambda>) |
| 96 | */ |
| 97 | class ParallelManager |
| 98 | { |
| 99 | private: |
| 100 | /** @brief Lock used for critical section and condition synchronization. */ |
| 101 | std::mutex m_lock; |
| 102 | |
| 103 | /** @brief True if the current operation is cancelled. */ |
| 104 | std::atomic<bool> m_is_cancelled; |
| 105 | |
| 106 | /** @brief True if the stage init() step has been executed. */ |
| 107 | bool m_init_done; |
| 108 | |
| 109 | /** @brief True if the stage term() step has been executed. */ |
| 110 | bool m_term_done; |
| 111 | |
| 112 | /** @brief Condition variable for tracking stage processing completion. */ |
| 113 | std::condition_variable m_complete; |
| 114 | |
| 115 | /** @brief Number of tasks started, but not necessarily finished. */ |
| 116 | std::atomic<size_t> m_start_count; |
| 117 | |
| 118 | /** @brief Number of tasks finished. */ |
| 119 | size_t m_done_count; |
| 120 | |
| 121 | /** @brief Number of tasks that need to be processed. */ |
| 122 | size_t m_task_count; |
| 123 | |
| 124 | /** @brief Progress callback (optional). */ |
| 125 | astcenc_progress_callback m_callback; |
| 126 | |
| 127 | /** @brief Lock used for callback synchronization. */ |
| 128 | std::mutex m_callback_lock; |
| 129 | |
| 130 | /** @brief Minimum progress before making a callback. */ |
| 131 | float m_callback_min_diff; |
| 132 | |
| 133 | /** @brief Last progress callback value. */ |
| 134 | float m_callback_last_value; |
| 135 | |
| 136 | public: |
| 137 | /** @brief Create a new ParallelManager. */ |
| 138 | ParallelManager() |
| 139 | { |
| 140 | reset(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @brief Reset the tracker for a new processing batch. |
| 145 | * |
| 146 | * This must be called from single-threaded code before starting the multi-threaded processing |
| 147 | * operations. |
| 148 | */ |
| 149 | void reset() |
| 150 | { |
| 151 | m_init_done = false; |
| 152 | m_term_done = false; |
| 153 | m_is_cancelled = false; |
| 154 | m_start_count = 0; |
nothing calls this directly
no outgoing calls
no test coverage detected