Open eliemichel opened 1 month ago
Thinking on this further, I think it's necessary to make this safe. Sure, we could say it's unsafe to WaitAny(f1)
during f1
's own callback inside another WaitAny(f1)
.
But what if you WaitAny(f1)
while f1
's callback is being called by ProcessEvents
? If that's unsafe too, it becomes hazardous to use WaitAny
on any AllowProcessEvents
-mode future in a multithreaded program.
WaitAny(f1)
while f1
's callback is being called spontaneously? You effectively can't WaitAny
on any Spontaneous
-mode future at all, even in a single-threaded application, because the implementation can be using background threads. (Also, the application can't just add its own locking in the callback - the critical section is larger than the whole callback.)If those things have to be safe then nested WaitAny may as well have to be re-entrancy-safe too (for both single-threaded nesting and for multithreading).
If those things have to be safe
OTOH they don't necessarily have to be safe. There just would be no way to do them safely.
I formalized the less safe version in #416. This might be too pessimistic though.
cc @lokokung
FWIW, I think WaitAny
inside a callback should work in our current implementation... (I haven't explicitly tried it, but I do remember ensuring that we release any Instance
related locks before calling callbacks which should in turn make it fine.)
However, we still have issues with re-entrancy for device level stuff because of the default global Device lock... If/when we have a separate thread for doing stuff, I think we can defer certain spontaneous callbacks, i.e. Device lost, and that might allow re-entrancy? Would need to think a little more about whether that could work though.
Ah, I didn't think about the fact that there can be things that are safe in multithreaded code but will cause deadlocks in nested code....
Question about
WGPUFuture
s: Is it legal to callwgpuInstanceWaitAny
from within a callback, and if so, what completion status is reported for theWGPUFuture
of the callback we are currently in?(EDIT: parent issue #199)
FTR, replies got on matrix: