Grok 20.3.2
TileFutureManager.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2026 Grok Image Compression Inc.
3 *
4 * This source code is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License, version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This source code is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#pragma once
19
20#include <stdexcept>
21#include <unordered_map>
22#include <mutex>
23#include <vector>
24#include <cassert>
25
26#include "grk_taskflow.h"
27
29{
30public:
31 TileFutureManager() = default;
32
33 // Prevent copying or moving
36
37 // Add a future to the map for a given tile ID
38 void add(uint16_t tile_id, tf::Future<void> future)
39 {
40 std::unique_lock lock(mutex_);
41 tileFutures_.insert_or_assign(tile_id, std::move(future));
42 }
43
44 // Wait for all futures to complete
45 void wait()
46 {
47 std::vector<std::reference_wrapper<tf::Future<void>>> futures;
48 {
49 std::unique_lock lock(mutex_);
50 futures.reserve(tileFutures_.size());
51 for(auto& [id, future] : tileFutures_)
52 {
53 futures.emplace_back(future);
54 }
55 // Lock is released here as scope ends
56 }
57
58 for(auto& future_ref : futures)
59 {
60 future_ref.get().wait();
61 }
62 }
63
64 // Wait for a specific future by tile ID, return true if found and waited
65 bool wait(uint16_t tile_id)
66 {
67 std::unique_lock lock(mutex_);
68 auto it = tileFutures_.find(tile_id);
69 if(it == tileFutures_.end())
70 {
71 return false;
72 }
73 auto& future = it->second;
74 lock.unlock();
75 future.wait();
76 return true;
77 }
78
79 // Clear the map immediately (no waiting)
80 void clear()
81 {
82 std::unique_lock lock(mutex_);
83 tileFutures_.clear();
84 }
85
86 // Cancel all futures
87 void cancelAll()
88 {
89 std::unique_lock lock(mutex_);
90 for(auto& [id, future] : tileFutures_)
91 {
92 future.cancel();
93 }
94 }
95
96 // Wait for all futures to complete, then clear the map
98 {
99 wait();
100 std::unique_lock lock(mutex_);
101 tileFutures_.clear();
102 }
103
104 void waitAndClear(uint16_t tileIndex)
105 {
106 std::unique_lock lock(mutex_);
107 auto it = tileFutures_.find(tileIndex);
108 if(it != tileFutures_.end())
109 {
110 auto future = std::move(it->second);
111 tileFutures_.erase(it);
112 lock.unlock();
113 future.wait();
114 }
115 }
116
117 // Check if the map is empty
118 bool empty() const
119 {
120 std::unique_lock lock(mutex_);
121 return tileFutures_.empty();
122 }
123
124private:
125 mutable std::mutex mutex_;
126 std::unordered_map<uint16_t, tf::Future<void>> tileFutures_;
127};
std::mutex mutex_
Definition TileFutureManager.h:125
TileFutureManager & operator=(const TileFutureManager &)=delete
std::unordered_map< uint16_t, tf::Future< void > > tileFutures_
Definition TileFutureManager.h:126
void cancelAll()
Definition TileFutureManager.h:87
TileFutureManager()=default
void waitAndClear(uint16_t tileIndex)
Definition TileFutureManager.h:104
void wait()
Definition TileFutureManager.h:45
void clear()
Definition TileFutureManager.h:80
TileFutureManager(const TileFutureManager &)=delete
void add(uint16_t tile_id, tf::Future< void > future)
Definition TileFutureManager.h:38
void waitAndClear()
Definition TileFutureManager.h:97
bool wait(uint16_t tile_id)
Definition TileFutureManager.h:65
bool empty() const
Definition TileFutureManager.h:118