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.

249 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. thread_functions.cxx
  5. Abstract:
  6. Public routines for the worker process thread pool
  7. Creates global instance of THREAD_POOL and forwards calls
  8. Author:
  9. Taylor Weiss (TaylorW) 12-Jan-2000
  10. Jeffrey Wall (jeffwall) April 2001
  11. --*/
  12. #include <iis.h>
  13. #include <dbgutil.h>
  14. #include <thread_pool.h>
  15. #include <regconst.h>
  16. //
  17. // Only initialize ourselves once
  18. //
  19. LONG g_cInitializeCount = -1;
  20. //
  21. // global pointer for process thread pool
  22. //
  23. THREAD_POOL * g_pThreadPool = NULL;
  24. /**********************************************************************
  25. Public function definitions
  26. **********************************************************************/
  27. HRESULT
  28. ThreadPoolInitialize( DWORD cbInitialStackSize )
  29. /*++
  30. Routine Description:
  31. Initializes the thread pool.
  32. NO SYNCHRONIZATION HERE
  33. Make sure the initialization of the thread pool is complete (this function returns S_OK)
  34. before calling other public API functions, including this one.
  35. Arguments:
  36. None.
  37. Return Value:
  38. NOERROR if thread pool is initialized
  39. FAILED() otherwise
  40. --*/
  41. {
  42. HRESULT hr = S_OK;
  43. BOOL fRet = FALSE;
  44. if ( InterlockedIncrement( &g_cInitializeCount ) != 0 )
  45. {
  46. //
  47. // Already inited
  48. //
  49. DBGPRINTF(( DBG_CONTEXT,
  50. "W3TP: ThreadPoolInitialize() already called\n" ));
  51. hr = S_OK;
  52. goto done;
  53. }
  54. DBG_ASSERT(NULL == g_pThreadPool);
  55. THREAD_POOL_CONFIG poolConfig;
  56. hr = InitializeThreadPoolConfigWithDefaults(&poolConfig);
  57. if (FAILED(hr))
  58. {
  59. goto done;
  60. }
  61. hr = OverrideThreadPoolConfigWithRegistry(&poolConfig,
  62. REGISTRY_KEY_INETINFO_PARAMETERS_W);
  63. if (FAILED(hr))
  64. {
  65. goto done;
  66. }
  67. fRet = THREAD_POOL::CreateThreadPool(&g_pThreadPool, &poolConfig);
  68. if (FALSE == fRet)
  69. {
  70. hr = E_FAIL;
  71. goto done;
  72. }
  73. DBG_ASSERT(NULL != g_pThreadPool);
  74. hr = S_OK;
  75. done:
  76. return hr;
  77. }
  78. HRESULT
  79. ThreadPoolTerminate( VOID )
  80. /*++
  81. Routine Description:
  82. Cleans up the thread pool. At this point all clients should
  83. have terminated (cleanly we hope) and our threads should
  84. be idle.
  85. Arguments:
  86. None.
  87. Return Value:
  88. NOERROR if clean shutdown
  89. FAILED() otherwise
  90. --*/
  91. {
  92. HRESULT hr = S_OK;
  93. BOOL fRet = FALSE;
  94. if ( InterlockedDecrement( &g_cInitializeCount ) >= 0 )
  95. {
  96. //
  97. // Someone else is using the pool
  98. //
  99. DBGPRINTF(( DBG_CONTEXT,
  100. "W3TP: ThreadPoolTerminate() called but pool still in use\n" ));
  101. hr = S_OK;
  102. goto done;
  103. }
  104. //
  105. // Now we can cleanup!
  106. //
  107. DBG_ASSERT(NULL != g_pThreadPool);
  108. g_pThreadPool->TerminateThreadPool();
  109. g_pThreadPool = NULL;
  110. hr = S_OK;
  111. done:
  112. return hr;
  113. }
  114. BOOL ThreadPoolPostCompletion(
  115. IN DWORD dwBytesTransferred,
  116. IN LPOVERLAPPED_COMPLETION_ROUTINE Function,
  117. IN LPOVERLAPPED lpo
  118. )
  119. /*++
  120. Routine Description:
  121. Posts a completion to the port. Results in an asynchronous callback.
  122. Arguments:
  123. dwBytesTransferred - bytes transferred for this completions
  124. Function - function to call on completion
  125. lpo - overlapped pointer
  126. Return Value:
  127. TRUE if completion posted, otherwise FALSE
  128. --*/
  129. {
  130. DBG_ASSERT(g_pThreadPool);
  131. return g_pThreadPool->PostCompletion(dwBytesTransferred,
  132. Function,
  133. lpo);
  134. }
  135. BOOL
  136. ThreadPoolBindIoCompletionCallback(
  137. HANDLE FileHandle, // handle to file
  138. LPOVERLAPPED_COMPLETION_ROUTINE Function, // callback
  139. ULONG Flags // reserved
  140. )
  141. /*++
  142. Routine Description:
  143. Binds given handle to completion port
  144. Arguments:
  145. FileHandle - handle to bind
  146. Function - function to call on completion
  147. Flags - not used
  148. Return Value:
  149. TRUE if handle bound to port, otherwise FALSE
  150. --*/
  151. {
  152. DBG_ASSERT(g_pThreadPool);
  153. return g_pThreadPool->BindIoCompletionCallback(FileHandle,
  154. Function,
  155. Flags);
  156. }
  157. ULONG_PTR
  158. ThreadPoolSetInfo(
  159. IN THREAD_POOL_INFO InfoId,
  160. IN ULONG_PTR Data
  161. )
  162. /*++
  163. Routine Description:
  164. Sets thread pool configuration data
  165. Arguments:
  166. InfoId - Data item to set
  167. Data - New value for item
  168. Return Value:
  169. The old data value
  170. --*/
  171. {
  172. DBG_ASSERT(g_pThreadPool);
  173. return g_pThreadPool->SetInfo(InfoId, Data);
  174. }