Allow destructors to run in foreign threads. Fixes #69.

This change modifies destructors to anticipate that they can be called form a foreign thread if the GC is involved. The actual release of the reference will then happen deferred in the original thread.
This commit is contained in:
Sönke Ludwig 2018-03-16 18:06:53 +01:00
parent 14f4e06b8a
commit 2c63aa5c5c
6 changed files with 86 additions and 29 deletions

View file

@ -0,0 +1,17 @@
module vibe.core.internal.release;
import eventcore.core;
/// Release a handle in a thread-safe way
void releaseHandle(string subsys, H)(H handle, shared(NativeEventDriver) drv)
{
if (drv is (() @trusted => cast(shared)eventDriver)()) {
__traits(getMember, eventDriver, subsys).releaseRef(handle);
} else {
// in case the destructor was called from a foreign thread,
// perform the release in the owner thread
drv.core.runInOwnerThread((h) {
__traits(getMember, eventDriver, subsys).releaseRef(cast(H)h);
}, cast(size_t)handle);
}
}