summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2012-07-23 19:16:57 +0000
committerGeorge Hazan <george.hazan@gmail.com>2012-07-23 19:16:57 +0000
commitdcb6bcdd69443e229f1ead5ee4a6c2a4622890f5 (patch)
tree295ec22cdd4e216087c29d6fbab6ba8921f007b0 /src
parent5c794a21c8e0c943e69b08a9ba52064149d663a6 (diff)
more correct variant of tls
git-svn-id: http://svn.miranda-ng.org/main/trunk@1135 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'src')
-rw-r--r--src/mir_core/miranda.cpp5
-rw-r--r--src/mir_core/miranda.h5
-rw-r--r--src/mir_core/modules.cpp22
-rw-r--r--src/mir_core/threads.cpp8
4 files changed, 17 insertions, 23 deletions
diff --git a/src/mir_core/miranda.cpp b/src/mir_core/miranda.cpp
index 9a3719f845..57c126bdbe 100644
--- a/src/mir_core/miranda.cpp
+++ b/src/mir_core/miranda.cpp
@@ -97,5 +97,10 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
mir_tls = TlsAlloc();
LoadCoreModule();
}
+ else if (fdwReason == DLL_THREAD_DETACH) {
+ HANDLE hEvent = TlsGetValue(mir_tls);
+ if (hEvent)
+ CloseHandle(hEvent);
+ }
return TRUE;
}
diff --git a/src/mir_core/miranda.h b/src/mir_core/miranda.h
index 21ced67c86..77ac609e64 100644
--- a/src/mir_core/miranda.h
+++ b/src/mir_core/miranda.h
@@ -89,11 +89,6 @@ HTREEITEM FindNamedTreeItemAtRoot(HWND hwndTree, const TCHAR* name);
/**** threads.cpp **********************************************************************/
-struct MThreadData
-{
- HANDLE m_hEvent;
-};
-
extern DWORD mir_tls;
/**** utils.cpp ************************************************************************/
diff --git a/src/mir_core/modules.cpp b/src/mir_core/modules.cpp
index 8372e966c3..794af53e74 100644
--- a/src/mir_core/modules.cpp
+++ b/src/mir_core/modules.cpp
@@ -82,6 +82,16 @@ static THook *pLastHook = NULL;
/////////////////////////////////////////////////////////////////////////////////////////
+__forceinline HANDLE getThreadEvent()
+{
+ HANDLE pData = (HANDLE)TlsGetValue(mir_tls);
+ if (pData == NULL) {
+ pData = CreateEvent(NULL, FALSE, FALSE, NULL);
+ TlsSetValue(mir_tls, pData);
+ }
+ return pData;
+}
+
static int QueueMainThread(PAPCFUNC pFunc, void* pParam, HANDLE hDoneEvent)
{
int result = QueueUserAPC(pFunc, hMainThread, (ULONG_PTR)pParam);
@@ -256,12 +266,8 @@ MIR_CORE_DLL(int) NotifyEventHooks(HANDLE hEvent, WPARAM wParam, LPARAM lParam)
if ( GetCurrentThreadId() == mainThreadId)
return CallHookSubscribers((THook*)hEvent, wParam, lParam);
- MThreadData* pData = (MThreadData*)TlsGetValue(mir_tls);
- if (pData == NULL)
- return -1;
-
mir_ptr<THookToMainThreadItem> item;
- item->hDoneEvent = pData->m_hEvent;
+ item->hDoneEvent = getThreadEvent();
item->hook = (THook*)hEvent;
item->wParam = wParam;
item->lParam = lParam;
@@ -530,15 +536,11 @@ MIR_CORE_DLL(INT_PTR) CallServiceSync(const char *name, WPARAM wParam, LPARAM lP
if (GetCurrentThreadId() == mainThreadId)
return CallService(name, wParam, lParam);
- MThreadData* pData = (MThreadData*)TlsGetValue(mir_tls);
- if (pData == NULL)
- return CALLSERVICE_NOTFOUND;
-
mir_ptr<TServiceToMainThreadItem> item;
item->wParam = wParam;
item->lParam = lParam;
item->name = name;
- item->hDoneEvent = pData->m_hEvent;
+ item->hDoneEvent = getThreadEvent();
QueueMainThread(CallServiceToMainAPCFunc, item, item->hDoneEvent);
return item->result;
}
diff --git a/src/mir_core/threads.cpp b/src/mir_core/threads.cpp
index cf1cdad48c..9187dfdf7c 100644
--- a/src/mir_core/threads.cpp
+++ b/src/mir_core/threads.cpp
@@ -103,9 +103,6 @@ struct FORK_ARG {
void __cdecl forkthread_r(void * arg)
{
- MThreadData threadData = { CreateEvent(NULL, FALSE, FALSE, NULL) };
- TlsSetValue(mir_tls, &threadData);
-
struct FORK_ARG * fa = (struct FORK_ARG *) arg;
void (*callercode)(void*)=fa->threadcode;
void * cookie=fa->arg;
@@ -121,7 +118,6 @@ void __cdecl forkthread_r(void * arg)
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
Thread_Pop();
- CloseHandle(threadData.m_hEvent);
}
MIR_CORE_DLL(UINT_PTR) forkthread( void (__cdecl *threadcode)(void*), unsigned long stacksize, void *arg)
@@ -144,9 +140,6 @@ MIR_CORE_DLL(UINT_PTR) forkthread( void (__cdecl *threadcode)(void*), unsigned l
unsigned __stdcall forkthreadex_r(void * arg)
{
- MThreadData threadData = { CreateEvent(NULL, FALSE, FALSE, NULL) };
- TlsSetValue(mir_tls, &threadData);
-
struct FORK_ARG *fa = (struct FORK_ARG *)arg;
pThreadFuncEx threadcode = fa->threadcodeex;
pThreadFuncOwner threadcodeex = (pThreadFuncOwner)fa->threadcodeex;
@@ -169,7 +162,6 @@ unsigned __stdcall forkthreadex_r(void * arg)
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
Thread_Pop();
- CloseHandle(threadData.m_hEvent);
return rc;
}