Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async-std in Windows cdylib dlls (LoadLibrary/FreeLibrary startup/cleanup) #1059

Open
Trolldemorted opened this issue May 31, 2023 · 1 comment

Comments

@Trolldemorted
Copy link

Trolldemorted commented May 31, 2023

Is there a way to use async-std in Windows cdylibs that are being loaded/unloaded with LoadLibrary/FreeLibrary?

For this we'd have to ensure that before DllMain with PROCESS_DETACH returns, all threads that were spawned by async-std are no longer running. Unfortunately I do not have a clear overview over the intrinsic behaviour of async-std's thread pools, reactors (are they still a thing?) and whatnot, and the documentation was not that helpful in this particular matter.

What troubles me most are the special rules that apply to DllMain, such as "you cannot wait for threads to finish".

@fawdlstty
Copy link

fawdlstty commented Nov 29, 2023

You can add two exported functions to your DLL, and move initial/uninitial code from DllMain to new func, such as:

// cpp pseudo code
typedef void(*pFinishCb)();
__declspec(dllexport) void AsyncInitialize(pFinishCb cb) {
    std::thread([](){
        // do something...
        cb();
    }).detach();
}
__declspec(dllexport) void AsyncUninitialize(pFinishCb cb) {
    std::thread([](){
        // do something...
        cb();
    }).detach();
}

and wrap it to async rust func:

// rust pseudo code
async fn MyAsyncInitialize() {
    cpp::LoadLibrary();
    let (tx, rx) = mpsc::channel();
    cpp::AsyncInitialize(move ||{
        tx.send(());
    });
    rx.recv().await;
}
// and same to Unitialize

now you can async initialize/uninitialize your dll in async:

MyAsyncInitialize().await;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants