Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
7.3 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2001-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: threadpooldllmain.cpp
  6. *
  7. * Content: DirectPlay Thread Pool DllMain functions.
  8. *
  9. * History:
  10. * Date By Reason
  11. * ======== ======== =========
  12. * 11/02/01 VanceO Created.
  13. *
  14. ******************************************************************************/
  15. #include "dpnthreadpooli.h"
  16. //=============================================================================
  17. // External globals
  18. //=============================================================================
  19. #ifndef DPNBUILD_LIBINTERFACE
  20. LONG g_lDPTPInterfaceCount = 0; // number of thread pool interfaces outstanding
  21. #endif // ! DPNBUILD_LIBINTERFACE
  22. #ifndef DPNBUILD_MULTIPLETHREADPOOLS
  23. #ifndef DPNBUILD_ONLYONETHREAD
  24. DNCRITICAL_SECTION g_csGlobalThreadPoolLock; // lock protecting the following globals
  25. #endif // !DPNBUILD_ONLYONETHREAD
  26. DWORD g_dwDPTPRefCount = 0; // number of references on the global thread pool object
  27. DPTHREADPOOLOBJECT * g_pDPTPObject = NULL; // pointer to the global thread pool object
  28. #endif // ! DPNBUILD_MULTIPLETHREADPOOLS
  29. #undef DPF_MODNAME
  30. #define DPF_MODNAME "DPThreadPoolInit"
  31. //=============================================================================
  32. // DPThreadPoolInit
  33. //-----------------------------------------------------------------------------
  34. //
  35. // Description: Performs any DLL initialization necessary.
  36. //
  37. // Arguments: None.
  38. //
  39. // Returns: BOOL
  40. // TRUE - Initialization was successful.
  41. // FALSE - An error prevented initialization.
  42. //=============================================================================
  43. BOOL DPThreadPoolInit(HANDLE hModule)
  44. {
  45. #ifndef WINCE
  46. BOOL fInittedTrackedFilePool = FALSE;
  47. #endif // ! WINCE
  48. #ifndef DPNBUILD_MULTIPLETHREADPOOLS
  49. BOOL fInittedGlobalThreadPoolLock = FALSE;
  50. #ifdef DPNBUILD_LIBINTERFACE
  51. HRESULT hr;
  52. DPTHREADPOOLOBJECT * pDPTPObject = NULL;
  53. #endif // DPNBUILD_LIBINTERFACE
  54. #endif // ! DPNBUILD_MULTIPLETHREADPOOLS
  55. #ifndef WINCE
  56. if (! g_TrackedFilePool.Initialize(sizeof(CTrackedFile),
  57. CTrackedFile::FPM_Alloc,
  58. NULL,
  59. NULL,
  60. NULL))
  61. {
  62. DPFX(DPFPREP, 0, "Couldn't initialize tracked file pool!");
  63. goto Failure;
  64. }
  65. fInittedTrackedFilePool = TRUE;
  66. #endif // ! WINCE
  67. #ifndef DPNBUILD_MULTIPLETHREADPOOLS
  68. if (! DNInitializeCriticalSection(&g_csGlobalThreadPoolLock))
  69. {
  70. DPFX(DPFPREP, 0, "Couldn't initialize global thread pool lock!");
  71. goto Failure;
  72. }
  73. fInittedGlobalThreadPoolLock = TRUE;
  74. #ifdef DPNBUILD_LIBINTERFACE
  75. hr = DPTPCF_CreateObject((PVOID*) (&pDPTPObject));
  76. if (hr != DPN_OK)
  77. {
  78. DPFX(DPFPREP, 0, "Couldn't create base thread pool object (err = 0x%lx)!", hr);
  79. goto Failure;
  80. }
  81. //
  82. // Forget about the object, we'll keep an extra reference on it until we
  83. // shut down.
  84. //
  85. pDPTPObject = NULL;
  86. #endif // DPNBUILD_LIBINTERFACE
  87. #endif // ! DPNBUILD_MULTIPLETHREADPOOLS
  88. #ifndef DPNBUILD_NOWINMM
  89. //
  90. // Set our time resolution to 1ms, ignore failure.
  91. //
  92. timeBeginPeriod(1);
  93. #endif // ! DPNBUILD_NOWINMM
  94. return TRUE;
  95. Failure:
  96. #ifndef DPNBUILD_MULTIPLETHREADPOOLS
  97. #ifdef DPNBUILD_LIBINTERFACE
  98. if (pDPTPObject != NULL)
  99. {
  100. DPTPCF_FreeObject(g_pDPTPObject);
  101. pDPTPObject;
  102. }
  103. #endif // DPNBUILD_LIBINTERFACE
  104. if (fInittedGlobalThreadPoolLock)
  105. {
  106. DNDeleteCriticalSection(&g_csGlobalThreadPoolLock);
  107. fInittedGlobalThreadPoolLock = FALSE;
  108. }
  109. #endif // ! DPNBUILD_MULTIPLETHREADPOOLS
  110. #ifndef WINCE
  111. if (fInittedTrackedFilePool)
  112. {
  113. g_TrackedFilePool.DeInitialize();
  114. fInittedTrackedFilePool = FALSE;
  115. }
  116. #endif // ! WINCE
  117. return FALSE;
  118. } // DPThreadPoolInit
  119. #undef DPF_MODNAME
  120. #define DPF_MODNAME "DPThreadPoolDeInit"
  121. //=============================================================================
  122. // DPThreadPoolDeInit
  123. //-----------------------------------------------------------------------------
  124. //
  125. // Description: Cleans up any DLL global resources.
  126. //
  127. // Arguments: None.
  128. //
  129. // Returns: Nothing.
  130. //=============================================================================
  131. void DPThreadPoolDeInit(void)
  132. {
  133. #ifndef DPNBUILD_NOWINMM
  134. timeEndPeriod(1);
  135. #endif // ! DPNBUILD_NOWINMM
  136. #ifndef DPNBUILD_MULTIPLETHREADPOOLS
  137. #ifdef DPNBUILD_LIBINTERFACE
  138. //
  139. // Free the thread pool object we've had since initialization.
  140. //
  141. DNASSERT(g_pDPTPObject != NULL);
  142. DPTPCF_FreeObject(g_pDPTPObject);
  143. #endif // DPNBUILD_LIBINTERFACE
  144. DNDeleteCriticalSection(&g_csGlobalThreadPoolLock);
  145. DNASSERT(g_dwDPTPRefCount == 0);
  146. DNASSERT(g_pDPTPObject == NULL);
  147. #endif // ! DPNBUILD_MULTIPLETHREADPOOLS
  148. #ifndef WINCE
  149. g_TrackedFilePool.DeInitialize();
  150. #endif // ! WINCE
  151. } // DPThreadPoolDeInit
  152. #ifndef DPNBUILD_NOCOMREGISTER
  153. #undef DPF_MODNAME
  154. #define DPF_MODNAME "DPThreadPoolRegister"
  155. //=============================================================================
  156. // DPThreadPoolRegister
  157. //-----------------------------------------------------------------------------
  158. //
  159. // Description: Registers this DLL.
  160. //
  161. // Arguments:
  162. // LPCWSTR wszDLLName - Pointer to Unicode DLL name.
  163. //
  164. // Returns: BOOL
  165. // TRUE - Registration was successful.
  166. // FALSE - An error prevented registration.
  167. //=============================================================================
  168. BOOL DPThreadPoolRegister(LPCWSTR wszDLLName)
  169. {
  170. BOOL fReturn = TRUE;
  171. if (! CRegistry::Register(L"DirectPlay8ThreadPool.1",
  172. L"DirectPlay8 Thread Pool Object",
  173. wszDLLName,
  174. &CLSID_DirectPlay8ThreadPool,
  175. L"DirectPlay8ThreadPool"))
  176. {
  177. DPFX(DPFPREP, 0, "Could not register DirectPlay8ThreadPool object!");
  178. fReturn = FALSE;
  179. }
  180. return fReturn;
  181. } // DPThreadPoolRegister
  182. #undef DPF_MODNAME
  183. #define DPF_MODNAME "DPThreadPoolUnRegister"
  184. //=============================================================================
  185. // DPThreadPoolUnRegister
  186. //-----------------------------------------------------------------------------
  187. //
  188. // Description: Unregisters this DLL.
  189. //
  190. // Arguments: None.
  191. //
  192. // Returns: BOOL
  193. // TRUE - Unregistration was successful.
  194. // FALSE - An error prevented unregistration.
  195. //=============================================================================
  196. BOOL DPThreadPoolUnRegister(void)
  197. {
  198. BOOL fReturn = TRUE;
  199. if (! CRegistry::UnRegister(&CLSID_DirectPlay8ThreadPool))
  200. {
  201. DPFX(DPFPREP, 0, "Could not register DirectPlay8ThreadPool object!");
  202. fReturn = FALSE;
  203. }
  204. return fReturn;
  205. } // DPThreadPoolRegister
  206. #endif // ! DPNBUILD_NOCOMREGISTER
  207. #ifndef DPNBUILD_LIBINTERFACE
  208. #undef DPF_MODNAME
  209. #define DPF_MODNAME "DPThreadPoolGetRemainingObjectCount"
  210. //=============================================================================
  211. // DPThreadPoolGetRemainingObjectCount
  212. //-----------------------------------------------------------------------------
  213. //
  214. // Description: Returns the number of objects owned by this DLL that are
  215. // still outstanding.
  216. //
  217. // Arguments: None.
  218. //
  219. // Returns: DWORD count of objects.
  220. //=============================================================================
  221. DWORD DPThreadPoolGetRemainingObjectCount(void)
  222. {
  223. return g_lDPTPInterfaceCount;
  224. } // DPThreadPoolGetRemainingObjectCount
  225. #endif // ! DPNBUILD_LIBINTERFACE