1 /* 2 * Copyright 2022 Google LLC. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrDawnAsyncWait_DEFINED 9 #define GrDawnAsyncWait_DEFINED 10 11 #include "webgpu/webgpu_cpp.h" 12 13 #include <atomic> 14 #include <functional> 15 16 // Utility for monitoring the execution of an asynchronous Dawn-API event. 17 class GrDawnAsyncWait final { 18 public: 19 explicit GrDawnAsyncWait(const wgpu::Device& device); 20 21 // Returns true if the wait has been signaled and false otherwise. This function yields 22 // execution to the event loop where Dawn's asynchronous tasks get scheduled and returns 23 // as soon as the loop yields the execution back to the caller. 24 bool yieldAndCheck() const; 25 26 // Busy-waits until this wait has been signaled. 27 // TODO(armansito): This could benefit from a timeout in the case the wait never gets signaled. 28 void busyWait() const; 29 30 // Marks this wait as resolved. Once called, all calls to `yieldAndCheck` and `busyWait` will 31 // return true immediately. signal()32 void signal() { fSignaled.store(true); } 33 34 // Resets this object into its unsignaled state. reset()35 void reset() { fSignaled.store(false); } 36 37 private: 38 wgpu::Device fDevice; 39 std::atomic_bool fSignaled; 40 }; 41 42 #endif // GrDawnAsyncWait_DEFINED 43