yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
wasm_loading_manager.h
Go to the documentation of this file.
1#ifndef YAZE_APP_PLATFORM_WASM_WASM_LOADING_MANAGER_H_
2#define YAZE_APP_PLATFORM_WASM_WASM_LOADING_MANAGER_H_
3
4#ifdef __EMSCRIPTEN__
5
6#include <atomic>
7#include <memory>
8#include <mutex>
9#include <string>
10#include <unordered_map>
11
12#include "absl/status/status.h"
13#include "absl/strings/str_format.h"
14
15namespace yaze {
16namespace app {
17namespace platform {
18
51class WasmLoadingManager {
52 public:
60 using LoadingHandle = uint64_t;
61
65 static constexpr LoadingHandle kInvalidHandle = 0;
66
72 static uint32_t GetJsId(LoadingHandle handle) {
73 return static_cast<uint32_t>(handle & 0xFFFFFFFF);
74 }
75
81 static LoadingHandle BeginLoading(const std::string& task_name);
82
88 static void UpdateProgress(LoadingHandle handle, float progress);
89
95 static void UpdateMessage(LoadingHandle handle, const std::string& message);
96
102 static bool IsCancelled(LoadingHandle handle);
103
108 static void EndLoading(LoadingHandle handle);
109
121 static bool ReportArenaProgress(int current, int total,
122 const std::string& item_name);
123
132 static void SetArenaHandle(LoadingHandle handle);
133
137 static void ClearArenaHandle();
138
139 private:
143 struct LoadingOperation {
144 std::string task_name;
145 float progress = 0.0f;
146 std::string message;
147 std::atomic<bool> cancelled{false};
148 bool active = true;
149 uint32_t generation = 0; // Generation counter for validation
150 };
151
155 static WasmLoadingManager& GetInstance();
156
160 WasmLoadingManager();
161
165 ~WasmLoadingManager();
166
167 // Disable copy and move
168 WasmLoadingManager(const WasmLoadingManager&) = delete;
169 WasmLoadingManager& operator=(const WasmLoadingManager&) = delete;
170 WasmLoadingManager(WasmLoadingManager&&) = delete;
171 WasmLoadingManager& operator=(WasmLoadingManager&&) = delete;
172
176 static LoadingHandle MakeHandle(uint32_t js_id, uint32_t generation) {
177 return (static_cast<uint64_t>(generation) << 32) | js_id;
178 }
179
183 static uint32_t GetGeneration(LoadingHandle handle) {
184 return static_cast<uint32_t>(handle >> 32);
185 }
186
193 std::atomic<uint32_t> next_js_id_{1};
194
201 std::atomic<uint32_t> generation_counter_{1};
202
206 std::unordered_map<LoadingHandle, std::unique_ptr<LoadingOperation>>
207 operations_;
208
212 std::mutex mutex_;
213
220 LoadingHandle arena_handle_ = kInvalidHandle;
221};
222
223} // namespace platform
224} // namespace app
225} // namespace yaze
226
227#endif // __EMSCRIPTEN__
228
229#endif // YAZE_APP_PLATFORM_WASM_WASM_LOADING_MANAGER_H_