From 649f9f6dc6312f75c434a669a81b8bf2aaa929a4 Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Mon, 11 Apr 2005 18:29:52 +0000 Subject: [PATCH] make certain that com.dll remains resident for its daemon threads even if the user explicitly uloads it (from a labView DLL) --- src/libCom/osi/os/WIN32/osdThread.c | 71 +++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/libCom/osi/os/WIN32/osdThread.c b/src/libCom/osi/os/WIN32/osdThread.c index 21dd9c7e3..501e07db9 100644 --- a/src/libCom/osi/os/WIN32/osdThread.c +++ b/src/libCom/osi/os/WIN32/osdThread.c @@ -94,6 +94,76 @@ static const int osdRealtimePriorityList [osdRealtimePriorityStateCount] = 6 // allowed on >= W2k, but no #define supplied }; +#if !defined(EPICS_DLL_NO) +BOOL WINAPI DllMain ( + HANDLE hModule, DWORD dwReason, LPVOID lpReserved ) +{ + static DWORD dllHandleIndex; + HMODULE dllHandle; + BOOL success = TRUE; + + switch ( dwReason ) + { + case DLL_PROCESS_ATTACH: + dllHandleIndex = TlsAlloc (); + if ( dllHandleIndex == TLS_OUT_OF_INDEXES ) { + success = FALSE; + } + break; + + case DLL_PROCESS_DETACH: + success = TlsFree ( dllHandleIndex ); + break; + + case DLL_THREAD_ATTACH: + /* + * Dont allow user's explicitly calling FreeLibrary for Com.dll to yank + * the carpet out from under EPICS threads that are still using Com.dll + */ +#if _WIN32_WINNT >= 0x0501 + /* + * Only in WXP + * Thats a shame becaus ethis is probably much faster + */ + success = GetModuleHandleEx ( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, + ( LPCTSTR ) DllMain, & dllHandle ); +#else + { + char name[256]; + DWORD nChar = GetModuleFileName ( + hModule, name, sizeof ( name ) ); + if ( nChar && nChar < sizeof ( name ) ) { + dllHandle = LoadLibrary ( name ); + if ( ! dllHandle ) { + success = FALSE; + } + } + else { + success = FALSE; + } + } +#endif + if ( success ) { + success = TlsSetValue ( dllHandleIndex, dllHandle ); + } + break; + case DLL_THREAD_DETACH: + /* + * Thread is exiting, release Com.dll. I am assuming that windows is + * smart enough to postpone the unload until this function returns. + */ + dllHandle = TlsGetValue ( dllHandleIndex ); + if ( dllHandle ) { + success = FreeLibrary ( dllHandle ); + } + break; + } + + return success; +} +#endif + /* * fetchWin32ThreadGlobal () * Search for "Synchronization and Multiprocessor Issues" in ms doc @@ -520,6 +590,7 @@ epicsShareFunc epicsThreadId epicsShareAPI epicsThreadCreate (const char *pName, if ( ! pGbl ) { return NULL; } + pParmWIN32 = epicsThreadParmCreate ( pName ); if ( pParmWIN32 == 0 ) { return ( epicsThreadId ) pParmWIN32;