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.

285 lines
7.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: ETASK.CXX (16 bit target)
  7. //
  8. // Contents: ETask management code, taken from 16-bit COMPOBJ.CPP
  9. //
  10. // Functions:
  11. //
  12. // History: 08-Mar-94 BobDay Copied parts from \\ole\slm\...\compobj.cpp
  13. // 01-Feb-95 JohannP modified/simplified
  14. //
  15. //--------------------------------------------------------------------------
  16. #include <headers.cxx>
  17. #pragma hdrstop
  18. #include <ole2sp.h>
  19. #include <olecoll.h>
  20. #include <map_kv.h>
  21. #include "comlocal.hxx"
  22. #include "map_htsk.h"
  23. #include "etask.hxx"
  24. #include "call32.hxx"
  25. #include "apilist.hxx"
  26. // NOTE: NEAR forces this variable to be in the default data segment; without
  27. // NEAR, the ambient model of the class CMapHandleEtask, which is FAR,
  28. // causes the variable to be in a far_data segment.
  29. //
  30. HTASK v_hTaskCache = NULL;
  31. Etask NEAR v_etaskCache;
  32. // quick check that the etask is valid (e.g., pointers are valid)
  33. INTERNAL_(BOOL) IsValidEtask(HTASK hTask, Etask FAR& etask)
  34. {
  35. Assert(GetCurrentProcess() == hTask);
  36. thkDebugOut((DEB_DLLS16, "IsValidEtask (%X) pMalloc(%p)\n", hTask, etask.m_pMalloc));
  37. if (!IsValidInterface(etask.m_pMalloc))
  38. {
  39. thkDebugOut((DEB_DLLS16, "IsValidEtask (%X) FALSE\n", hTask));
  40. return FALSE;
  41. }
  42. // FUTURE: verify that stack segment is the same
  43. // FUTURE: verify that hInst/hMod are the same
  44. thkDebugOut((DEB_DLLS16, "IsValidEtask (%X) TRUE\n", hTask));
  45. return TRUE;
  46. }
  47. // if task map empty, clear globals in case lib doesn't get unloaded
  48. INTERNAL_(void) CheckIfMapEmpty(void)
  49. {
  50. // if no more entries, clear v_pMallocShared; this ensures we clear the
  51. // variable if the app holds onto this pointer erroneously.
  52. if (v_mapToEtask.IsEmpty()) {
  53. v_pMallocShared = NULL;
  54. }
  55. }
  56. //+---------------------------------------------------------------------------
  57. //
  58. // Method: LookupEtask
  59. //
  60. // Synopsis: get etask for current task (and also return hTask);
  61. // does not create if none
  62. //
  63. // Arguments: [hTask] --
  64. // [etask] --
  65. //
  66. // Returns:
  67. //
  68. // History: Ole16 created for CompObj 16 bit for Ole2
  69. // 2-03-95 JohannP (Johann Posch) modified/simplified
  70. //
  71. // Notes:
  72. //
  73. //----------------------------------------------------------------------------
  74. STDAPI_(BOOL) LookupEtask(HTASK FAR& hTask, Etask FAR& etask)
  75. {
  76. hTask = GetCurrentProcess();
  77. thkDebugOut((DEB_DLLS16, "LookupEtask on Process (%X) \n", hTask));
  78. if (hTask == v_hTaskCache)
  79. {
  80. thkDebugOut((DEB_DLLS16, "LookupEtask found in cache (%X) \n", hTask));
  81. etask = v_etaskCache;
  82. goto CheckEtask;
  83. }
  84. if (!v_mapToEtask.Lookup(hTask, etask))
  85. {
  86. thkDebugOut((DEB_DLLS16, "LookupEtask faild on lookup (%X) \n", hTask));
  87. return FALSE;
  88. }
  89. else
  90. {
  91. thkDebugOut((DEB_DLLS16, "LookupEtask found in lookup (%X) \n", hTask));
  92. }
  93. // found etask; make this the current cache
  94. v_hTaskCache = hTask;
  95. v_etaskCache = etask;
  96. CheckEtask:
  97. if (IsValidEtask(hTask, etask))
  98. {
  99. return TRUE;
  100. }
  101. else
  102. {
  103. // task got reused; kill cache and entry in map
  104. v_hTaskCache = NULL;
  105. v_mapToEtask.RemoveKey(hTask);
  106. CheckIfMapEmpty();
  107. thkAssert(0 && "LookupEtask - failed (invalid Etask)");
  108. return FALSE;
  109. }
  110. }
  111. //+---------------------------------------------------------------------------
  112. //
  113. // Method: SetEtask
  114. //
  115. // Synopsis: set etask for task given (must be current task); return FALSE
  116. // if OOM (only first time; all other times should return TRUE).
  117. //
  118. // Arguments: [hTask] --
  119. // [etask] --
  120. //
  121. // Returns:
  122. //
  123. // History: Ole16 created for CompObj 16 bit for Ole2
  124. // 02-03-95 JohannP (Johann Posch) modified/simplified
  125. //
  126. // Notes:
  127. //
  128. //----------------------------------------------------------------------------
  129. STDAPI_(BOOL) SetEtask(HTASK hTask, Etask FAR& etask)
  130. {
  131. Assert(GetCurrentProcess() == hTask);
  132. if (!v_mapToEtask.SetAt(hTask, etask))
  133. return FALSE;
  134. Assert(IsValidEtask(hTask, etask));
  135. // map set; make this the current cache
  136. v_hTaskCache = hTask;
  137. v_etaskCache = etask;
  138. return TRUE;
  139. }
  140. //+---------------------------------------------------------------------------
  141. //
  142. // Method: ReleaseEtask
  143. //
  144. // Synopsis: release all fields in the etask; do all the task memory
  145. // (except the task allocator) first; then all the shared
  146. // memory (except the shared allocator); then the shared
  147. // allocator and finally the task allocator.
  148. // Also removes key if htask is given.
  149. //
  150. // Arguments: [htask] --
  151. // [etask] --
  152. //
  153. // Returns:
  154. //
  155. // History: Ole16 created for CompObj 16 bit for Ole2
  156. // 2-03-95 JohannP (Johann Posch) modified/simplified
  157. //
  158. // Notes: Called by daytona and chicago.
  159. //
  160. //----------------------------------------------------------------------------
  161. void ReleaseEtask(HTASK htask, Etask FAR& etask)
  162. {
  163. thkDebugOut((DEB_DLLS16, "ReleaseEtask on Process (%X) \n", htask));
  164. Assert(etask.m_inits == 1);
  165. Assert(etask.m_oleinits == 0);
  166. Assert(etask.m_reserved == 0);
  167. // Release any state that may have been set
  168. if (etask.m_punkState != NULL && IsValidInterface(etask.m_punkState))
  169. {
  170. etask.m_punkState->Release();
  171. }
  172. // first delete all task memory items
  173. delete etask.m_pDlls; // task memory
  174. delete etask.m_pMapToServerCO; // task memory
  175. delete etask.m_pMapToHandlerCO; // task memory
  176. delete etask.m_pArraySH; // task memory
  177. Assert(etask.m_pCThrd == NULL); // task memory; must be gone by now
  178. // remove key now that all task memory that will be freed is freed
  179. if (htask != NULL) {
  180. v_mapToEtask.RemoveKey(htask);
  181. }
  182. // if no more entries, remove last remaining memory; this prevents
  183. // problems if the dll is not unloaded for some reason before being
  184. // used again.
  185. if (v_mapToEtask.IsEmpty())
  186. v_mapToEtask.RemoveAll();
  187. // now remove all shared memory (doesn't need access to task pMalloc)
  188. Assert(etask.m_pMallocShared != NULL);
  189. if (etask.m_pMallocShared->Release() == 0) { // in shared memory
  190. // last use of the shared allocator; set global to null
  191. v_pMallocShared = NULL;
  192. Assert(v_mapToEtask.IsEmpty());
  193. }
  194. CheckIfMapEmpty();
  195. // finally, release the task memory
  196. Assert(etask.m_pMalloc != NULL);
  197. etask.m_pMalloc->Release(); // in task memory
  198. etask.m_pMalloc = NULL;
  199. thkDebugOut((DEB_DLLS16, "ReleaseEtask (%X) pMalloc(%p)\n", htask, etask.m_pMalloc));
  200. // invalidate cache
  201. v_hTaskCache = NULL;
  202. thkDebugOut((DEB_DLLS16, "ReleaseEtask done on (%X) \n", htask));
  203. }
  204. //+---------------------------------------------------------------------------
  205. //
  206. // Function: RemoveEtask
  207. //
  208. // Synopsis: Releases the shares allocator and removes
  209. // the etask from the global list
  210. //
  211. // Arguments: [hTask] -- htask
  212. // [etask] -- etask
  213. //
  214. // Returns:
  215. //
  216. // History: 2-03-95 JohannP (Johann Posch) Created
  217. //
  218. // Notes: Called only by Chicago.
  219. //
  220. //----------------------------------------------------------------------------
  221. STDAPI_(BOOL) RemoveEtask(HTASK FAR& hTask, Etask FAR& etask)
  222. {
  223. hTask = GetCurrentProcess();
  224. thkDebugOut((DEB_DLLS16, "RemoveEtask on Process (%X) \n", hTask));
  225. if (hTask == v_hTaskCache)
  226. {
  227. v_hTaskCache = NULL;
  228. }
  229. v_mapToEtask.RemoveKey(hTask);
  230. if (v_mapToEtask.IsEmpty())
  231. v_mapToEtask.RemoveAll();
  232. thkAssert(etask.m_pMallocShared != NULL);
  233. if(etask.m_pMallocShared->Release() == 0)
  234. {
  235. // in shared memory
  236. // last use of the shared allocator; set global to null
  237. v_pMallocShared = NULL;
  238. thkDebugOut((DEB_DLLS16, "RemoveEtask Set v_pMallocShared to NULL (%X) \n", hTask));
  239. Assert(v_mapToEtask.IsEmpty());
  240. }
  241. CheckIfMapEmpty();
  242. thkDebugOut((DEB_DLLS16, "RemoveEtask done (%X) \n", hTask));
  243. return TRUE;
  244. }