diff options
author | George Hazan <george.hazan@gmail.com> | 2012-07-23 15:44:06 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2012-07-23 15:44:06 +0000 |
commit | 6923945a269905e5990d2f66dfa1bbb445e60f70 (patch) | |
tree | 30790d1cdd40398d50106bbc58377ad8cb2609b9 | |
parent | fbd49cdebcf274f8a1a1bb08317bd10992a77030 (diff) |
minor improvement for async calls
git-svn-id: http://svn.miranda-ng.org/main/trunk@1126 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
-rw-r--r-- | src/mir_core/miranda.cpp | 3 | ||||
-rw-r--r-- | src/mir_core/miranda.h | 9 | ||||
-rw-r--r-- | src/mir_core/modules.cpp | 17 | ||||
-rw-r--r-- | src/mir_core/threads.cpp | 9 |
4 files changed, 32 insertions, 6 deletions
diff --git a/src/mir_core/miranda.cpp b/src/mir_core/miranda.cpp index 61b14ced06..9a3719f845 100644 --- a/src/mir_core/miranda.cpp +++ b/src/mir_core/miranda.cpp @@ -33,6 +33,7 @@ int hLangpack = 0; HINSTANCE hInst = 0;
HANDLE hStackMutex, hThreadQueueEmpty;
+DWORD mir_tls = 0;
/////////////////////////////////////////////////////////////////////////////////////////
// module init
@@ -80,6 +81,7 @@ MIR_CORE_DLL(void) UnloadCoreModule(void) DestroyWindow(hAPCWindow);
CloseHandle(hStackMutex);
CloseHandle(hThreadQueueEmpty);
+ TlsFree(mir_tls);
DestroyModularEngine();
UnloadLangPackModule();
@@ -92,6 +94,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
if (fdwReason == DLL_PROCESS_ATTACH) {
hInst = hinstDLL;
+ mir_tls = TlsAlloc();
LoadCoreModule();
}
return TRUE;
diff --git a/src/mir_core/miranda.h b/src/mir_core/miranda.h index a83d6e2c2f..21ced67c86 100644 --- a/src/mir_core/miranda.h +++ b/src/mir_core/miranda.h @@ -87,6 +87,15 @@ TCHAR* LangPackTranslateStringT(int hLangpack, const TCHAR* tszEnglish); HTREEITEM FindNamedTreeItemAtRoot(HWND hwndTree, const TCHAR* name);
+/**** threads.cpp **********************************************************************/
+
+struct MThreadData
+{
+ HANDLE m_hEvent;
+};
+
+extern DWORD mir_tls;
+
/**** utils.cpp ************************************************************************/
void HotkeyToName(TCHAR *buf, int size, BYTE shift, BYTE key);
diff --git a/src/mir_core/modules.cpp b/src/mir_core/modules.cpp index ec9ff1160a..8372e966c3 100644 --- a/src/mir_core/modules.cpp +++ b/src/mir_core/modules.cpp @@ -86,10 +86,9 @@ static int QueueMainThread(PAPCFUNC pFunc, void* pParam, HANDLE hDoneEvent) {
int result = QueueUserAPC(pFunc, hMainThread, (ULONG_PTR)pParam);
PostMessage(hAPCWindow, WM_NULL, 0, 0); // let this get processed in its own time
- if (hDoneEvent) {
+ if (hDoneEvent)
WaitForSingleObject(hDoneEvent, INFINITE);
- CloseHandle(hDoneEvent);
- }
+
return result;
}
@@ -257,8 +256,12 @@ 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 = CreateEvent(NULL, FALSE, FALSE, NULL);
+ item->hDoneEvent = pData->m_hEvent;
item->hook = (THook*)hEvent;
item->wParam = wParam;
item->lParam = lParam;
@@ -527,11 +530,15 @@ 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 = CreateEvent(NULL, FALSE, FALSE, NULL);
+ item->hDoneEvent = pData->m_hEvent;
QueueMainThread(CallServiceToMainAPCFunc, item, item->hDoneEvent);
return item->result;
}
diff --git a/src/mir_core/threads.cpp b/src/mir_core/threads.cpp index 90dcdaaf5d..cf1cdad48c 100644 --- a/src/mir_core/threads.cpp +++ b/src/mir_core/threads.cpp @@ -103,6 +103,9 @@ 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;
@@ -118,7 +121,7 @@ void __cdecl forkthread_r(void * arg) SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
Thread_Pop();
- return;
+ CloseHandle(threadData.m_hEvent);
}
MIR_CORE_DLL(UINT_PTR) forkthread( void (__cdecl *threadcode)(void*), unsigned long stacksize, void *arg)
@@ -141,6 +144,9 @@ 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;
@@ -163,6 +169,7 @@ unsigned __stdcall forkthreadex_r(void * arg) SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
Thread_Pop();
+ CloseHandle(threadData.m_hEvent);
return rc;
}
|