Creating a Casablanca lib and understanding the io_scheduler destructor

Dec 3, 2013 at 5:36 PM
We are currently exploring creating a lib we can link into our dll or executable (Windows) and came across the DllMain specialized behavior below.

DllMain sets a flag when the process is terminating
volatile long g_isProcessTerminating = 0;

BOOL WINAPI DllMain(HINSTANCE, DWORD fdwReason, LPVOID lpReserved)
{
    switch (fdwReason)
    {
    case DLL_PROCESS_DETACH:

        // A non null lpReserved parameter indicates that the process is terminating
        if (lpReserved != nullptr)
        {
            g_isProcessTerminating = 1;
        }

        ....
Later in ~io_scheduler I find the only use of the flag:
io_scheduler::~io_scheduler()
{
    if (g_isProcessTerminating != 1)
    {
        // Cancel pending callbacks and waits for the ones currently executing
        CloseThreadpoolCleanupGroupMembers(m_cleanupGroup, TRUE, NULL);

        // Release resources
        CloseThreadpoolCleanupGroup(m_cleanupGroup);
        DestroyThreadpoolEnvironment(&m_environ);
    }
}
What is the intent of this condition? Is this purely an optimization? It appears to not block/wait while cleaning up the threadpools if the process is terminating vs being unloaded via FreeLibrary.

Should we reproduce this functionality? Any other thoughts on creating a lib?

Thanks,
-Josh
Dec 4, 2013 at 5:15 PM
Hi Josh,

This, and a few other things we will need to work out before supporting static linking. The other potentially problematic thing I can think of off the top of my head is the global ambient scheduler, which is a singleton. With the static lib, initialization/deinitialization will have to be moved to a separate API, or done lazily.

Other than that, I cannot think of many technical hurdles.