yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
wasm_async_guard.h
Go to the documentation of this file.
1#ifndef YAZE_APP_PLATFORM_WASM_WASM_ASYNC_GUARD_H_
2#define YAZE_APP_PLATFORM_WASM_WASM_ASYNC_GUARD_H_
3
4#ifdef __EMSCRIPTEN__
5
6#include <atomic>
7
8#include <emscripten.h>
9
10namespace yaze {
11namespace platform {
12
29class WasmAsyncGuard {
30 public:
35 static bool TryAcquire() {
36 bool expected = false;
37 return in_async_op_.compare_exchange_strong(expected, true);
38 }
39
43 static void Release() { in_async_op_.store(false); }
44
49 static bool IsInProgress() { return in_async_op_.load(); }
50
58 class ScopedGuard {
59 public:
60 ScopedGuard() : acquired_(TryAcquire()) {
61 if (!acquired_) {
62 emscripten_log(
63 EM_LOG_WARN,
64 "[WasmAsyncGuard] Async operation already in progress, "
65 "request will be queued by JS async queue");
66 }
67 }
68
69 ~ScopedGuard() {
70 if (acquired_) {
71 Release();
72 }
73 }
74
75 // Non-copyable
76 ScopedGuard(const ScopedGuard&) = delete;
77 ScopedGuard& operator=(const ScopedGuard&) = delete;
78
83 bool acquired() const { return acquired_; }
84
85 private:
86 bool acquired_;
87 };
88
89 private:
90 static std::atomic<bool> in_async_op_;
91};
92
93} // namespace platform
94} // namespace yaze
95
96#endif // __EMSCRIPTEN__
97
98#endif // YAZE_APP_PLATFORM_WASM_WASM_ASYNC_GUARD_H_