FreeRDP
callback_cleanup.c
1 
20 #include <winpr/config.h>
21 
22 #include <winpr/crt.h>
23 #include <winpr/pool.h>
24 #include <winpr/library.h>
25 
26 #include "pool.h"
27 
28 #ifdef WINPR_THREAD_POOL
29 
30 #ifdef _WIN32
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,
34  DWORD crel);
35 static VOID(WINAPI* pReleaseMutexWhenCallbackReturns)(PTP_CALLBACK_INSTANCE pci, HANDLE mut);
36 static VOID(WINAPI* pLeaveCriticalSectionWhenCallbackReturns)(PTP_CALLBACK_INSTANCE pci,
37  PCRITICAL_SECTION pcs);
38 static VOID(WINAPI* pFreeLibraryWhenCallbackReturns)(PTP_CALLBACK_INSTANCE pci, HMODULE mod);
39 static VOID(WINAPI* pDisassociateCurrentThreadFromCallback)(PTP_CALLBACK_INSTANCE pci);
40 
41 static BOOL CALLBACK init_module(PINIT_ONCE once, PVOID param, PVOID* context)
42 {
43  HMODULE kernel32 = LoadLibraryA("kernel32.dll");
44  if (kernel32)
45  {
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*);
58  }
59  return TRUE;
60 }
61 #endif
62 
63 VOID SetEventWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, HANDLE evt)
64 {
65 #ifdef _WIN32
66  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
67  if (pSetEventWhenCallbackReturns)
68  {
69  pSetEventWhenCallbackReturns(pci, evt);
70  return;
71  }
72 #endif
73  /* No default implementation */
74 }
75 
76 VOID ReleaseSemaphoreWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, HANDLE sem, DWORD crel)
77 {
78 #ifdef _WIN32
79  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
80  if (pReleaseSemaphoreWhenCallbackReturns)
81  {
82  pReleaseSemaphoreWhenCallbackReturns(pci, sem, crel);
83  return;
84  }
85 #endif
86  /* No default implementation */
87 }
88 
89 VOID ReleaseMutexWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, HANDLE mut)
90 {
91 #ifdef _WIN32
92  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
93  if (pReleaseMutexWhenCallbackReturns)
94  {
95  pReleaseMutexWhenCallbackReturns(pci, mut);
96  return;
97  }
98 #endif
99  /* No default implementation */
100 }
101 
102 VOID LeaveCriticalSectionWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, PCRITICAL_SECTION pcs)
103 {
104 #ifdef _WIN32
105  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
106  if (pLeaveCriticalSectionWhenCallbackReturns)
107  {
108  pLeaveCriticalSectionWhenCallbackReturns(pci, pcs);
109  }
110 #endif
111  /* No default implementation */
112 }
113 
114 VOID FreeLibraryWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, HMODULE mod)
115 {
116 #ifdef _WIN32
117  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
118  if (pFreeLibraryWhenCallbackReturns)
119  {
120  pFreeLibraryWhenCallbackReturns(pci, mod);
121  return;
122  }
123 #endif
124  /* No default implementation */
125 }
126 
127 VOID DisassociateCurrentThreadFromCallback(PTP_CALLBACK_INSTANCE pci)
128 {
129 #ifdef _WIN32
130  InitOnceExecuteOnce(&init_once_module, init_module, NULL, NULL);
131  if (pDisassociateCurrentThreadFromCallback)
132  {
133  pDisassociateCurrentThreadFromCallback(pci);
134  return;
135  }
136 #endif
137  /* No default implementation */
138 }
139 
140 #endif /* WINPR_THREAD_POOL defined */