20 #include <winpr/config.h>
22 #include <winpr/crt.h>
23 #include <winpr/pool.h>
24 #include <winpr/library.h>
28 #ifdef WINPR_THREAD_POOL
31 static INIT_ONCE init_once_module = INIT_ONCE_STATIC_INIT;
32 static VOID(WINAPI* pSetEventWhenCallbackReturns)(PTP_CALLBACK_INSTANCE pci, HANDLE evt);
33 static VOID(WINAPI* pReleaseSemaphoreWhenCallbackReturns)(PTP_CALLBACK_INSTANCE pci, HANDLE sem,
35 static VOID(WINAPI* pReleaseMutexWhenCallbackReturns)(PTP_CALLBACK_INSTANCE pci, HANDLE mut);
36 static VOID(WINAPI* pLeaveCriticalSectionWhenCallbackReturns)(PTP_CALLBACK_INSTANCE pci,
38 static VOID(WINAPI* pFreeLibraryWhenCallbackReturns)(PTP_CALLBACK_INSTANCE pci, HMODULE mod);
39 static VOID(WINAPI* pDisassociateCurrentThreadFromCallback)(PTP_CALLBACK_INSTANCE pci);
41 static BOOL CALLBACK init_module(
PINIT_ONCE once, PVOID param, PVOID* context)
43 HMODULE kernel32 = LoadLibraryA(
"kernel32.dll");
46 pSetEventWhenCallbackReturns =
47 GetProcAddressAs(kernel32,
"SetEventWhenCallbackReturns"),
void*);
48 pReleaseSemaphoreWhenCallbackReturns =
49 GetProcAddressAs(kernel32,
"ReleaseSemaphoreWhenCallbackReturns",
void*);
50 pReleaseMutexWhenCallbackReturns =
51 GetProcAddressAs(kernel32,
"ReleaseMutexWhenCallbackReturns",
void*);
52 pLeaveCriticalSectionWhenCallbackReturns =
53 GetProcAddressAs(kernel32,
"LeaveCriticalSectionWhenCallbackReturns",
void*);
54 pFreeLibraryWhenCallbackReturns =
55 GetProcAddressAs(kernel32,
"FreeLibraryWhenCallbackReturns",
void*);
56 pDisassociateCurrentThreadFromCallback =
57 GetProcAddressAs(kernel32,
"DisassociateCurrentThreadFromCallback",
void*);
63 VOID SetEventWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, HANDLE evt)
66 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
67 if (pSetEventWhenCallbackReturns)
69 pSetEventWhenCallbackReturns(pci, evt);
76 VOID ReleaseSemaphoreWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, HANDLE sem, DWORD crel)
79 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
80 if (pReleaseSemaphoreWhenCallbackReturns)
82 pReleaseSemaphoreWhenCallbackReturns(pci, sem, crel);
89 VOID ReleaseMutexWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, HANDLE mut)
92 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
93 if (pReleaseMutexWhenCallbackReturns)
95 pReleaseMutexWhenCallbackReturns(pci, mut);
102 VOID LeaveCriticalSectionWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci,
PCRITICAL_SECTION pcs)
105 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
106 if (pLeaveCriticalSectionWhenCallbackReturns)
108 pLeaveCriticalSectionWhenCallbackReturns(pci, pcs);
114 VOID FreeLibraryWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, HMODULE mod)
117 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
118 if (pFreeLibraryWhenCallbackReturns)
120 pFreeLibraryWhenCallbackReturns(pci, mod);
127 VOID DisassociateCurrentThreadFromCallback(PTP_CALLBACK_INSTANCE pci)
130 InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
131 if (pDisassociateCurrentThreadFromCallback)
133 pDisassociateCurrentThreadFromCallback(pci);