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.

907 lines
23 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: context.hxx
  7. //
  8. // Contents: Per-context things header
  9. //
  10. // Classes: CPerContext
  11. //
  12. // History: 14-Aug-92 DrewB Created
  13. //
  14. //---------------------------------------------------------------
  15. #ifndef __CONTEXT_HXX__
  16. #define __CONTEXT_HXX__
  17. #include <filest.hxx>
  18. #include <cntxlist.hxx>
  19. #if WIN32 >= 100
  20. #include <entry.hxx>
  21. #include <df32.hxx>
  22. #endif
  23. #ifdef MULTIHEAP
  24. #include <smalloc.hxx>
  25. extern SCODE DfSyncSharedMemory(ULONG ulHeapName);
  26. #else
  27. extern SCODE DfSyncSharedMemory(void);
  28. #endif
  29. interface ILockBytes;
  30. // Maximum length of a mutex name for contexts
  31. #define CONTEXT_MUTEX_NAME_LENGTH 32
  32. //+--------------------------------------------------------------
  33. //
  34. // Class: CPerContext (pc)
  35. //
  36. // Purpose: Holds per-context information
  37. //
  38. // Interface: See below
  39. //
  40. // History: 14-Aug-92 DrewB Created
  41. //
  42. //---------------------------------------------------------------
  43. class CGlobalContext;
  44. SAFE_DFBASED_PTR(CBasedGlobalContextPtr, CGlobalContext);
  45. class CPerContext : public CContext
  46. {
  47. public:
  48. inline CPerContext(
  49. IMalloc *pMalloc,
  50. ILockBytes *plkbBase,
  51. CFileStream *pfstDirty,
  52. ILockBytes *plkbOriginal,
  53. ULONG ulOpenLock);
  54. inline CPerContext(IMalloc *pMalloc);
  55. inline void SetILBInfo(ILockBytes *plkbBase,
  56. CFileStream *pfstDirty,
  57. ILockBytes *plkbOriginal,
  58. ULONG ulOpenLock);
  59. inline void SetLockInfo(BOOL fTakeLock, DFLAGS dfOpenLock);
  60. inline SCODE InitNewContext(void);
  61. inline SCODE InitFromGlobal(CGlobalContext *pgc);
  62. ~CPerContext(void);
  63. inline LONG AddRef(void);
  64. inline LONG Release(void);
  65. #ifdef ASYNC
  66. inline LONG AddRefSharedMem(void);
  67. inline LONG ReleaseSharedMem(void);
  68. inline LONG DecRef(void);
  69. #endif
  70. inline ILockBytes *GetBase(void) const;
  71. inline CFileStream *GetDirty(void) const;
  72. inline ILockBytes *GetOriginal(void) const;
  73. inline ULONG GetOpenLock(void) const;
  74. inline ContextId GetContextId(void) const;
  75. inline CGlobalContext *GetGlobal(void) const;
  76. inline IMalloc * GetMalloc(void) const;
  77. inline void SetOpenLock(ULONG ulOpenLock);
  78. inline SCODE TakeSem(DWORD dwTimeout);
  79. inline void ReleaseSem(void);
  80. #ifdef MULTIHEAP
  81. inline SCODE SetAllocatorState(CPerContext **pppcPrev, CSmAllocator *psma);
  82. inline SCODE GetThreadAllocatorState();
  83. inline CSmAllocator *SetThreadAllocatorState(CPerContext **pppcPrev);
  84. inline void UntakeSem() { ReleaseSem(); }; // workaround macro in docfilep
  85. #endif
  86. #ifdef ASYNC
  87. inline IFillInfo * GetFillInfo(void) const;
  88. inline HANDLE GetNotificationEvent(void) const;
  89. SCODE InitNotificationEvent(ILockBytes *plkbBase);
  90. #if DBG == 1
  91. inline BOOL HaveMutex(void);
  92. #endif
  93. #endif
  94. void Close(void);
  95. #ifdef DIRECTWRITERLOCK
  96. ULONG * GetRecursionCount () {return &_cRecursion; };
  97. #endif
  98. #ifdef MULTIHEAP
  99. BYTE * GetHeapBase () { return _pbBase; };
  100. #endif
  101. inline BOOL IsHandleValid ();
  102. private:
  103. ILockBytes *_plkbBase;
  104. CFileStream *_pfstDirty;
  105. ILockBytes *_plkbOriginal;
  106. ULONG _ulOpenLock;
  107. CBasedGlobalContextPtr _pgc;
  108. LONG _cReferences;
  109. #ifdef ASYNC
  110. LONG _cRefSharedMem;
  111. #endif
  112. IMalloc * const _pMalloc;
  113. #ifdef ASYNC
  114. IFillInfo *_pfi;
  115. HANDLE _hNotificationEvent;
  116. #endif
  117. #ifdef MULTIHEAP
  118. CSharedMemoryBlock *_psmb; // heap object
  119. BYTE * _pbBase; // base address of heap
  120. ULONG _ulHeapName; // name of shared mem region
  121. #endif
  122. #if WIN32 >= 100
  123. CDfMutex _dmtx;
  124. #endif
  125. #ifdef DIRECTWRITERLOCK
  126. ULONG _cRecursion; // recursion count of writers
  127. #endif
  128. };
  129. SAFE_DFBASED_PTR(CBasedPerContextPtr, CPerContext);
  130. //+---------------------------------------------------------------------------
  131. //
  132. // Class: CGlobalContext
  133. //
  134. // Purpose: Holds context-insensitive information
  135. //
  136. // Interface: See below
  137. //
  138. // History: 26-Oct-92 DrewB Created
  139. //
  140. //----------------------------------------------------------------------------
  141. class CGlobalContext : public CContextList
  142. {
  143. public:
  144. inline CGlobalContext(IMalloc *pMalloc);
  145. inline void SetLockInfo(BOOL fTakeLock,
  146. DFLAGS dfOpenLock);
  147. DECLARE_CONTEXT_LIST(CPerContext);
  148. inline BOOL TakeLock(void) const;
  149. inline DFLAGS GetOpenLockFlags(void) const;
  150. inline IMalloc *GetMalloc(void) const;
  151. #if WIN32 >= 100
  152. inline void GetMutexName(TCHAR *ptcsName);
  153. #ifdef ASYNC
  154. inline void GetEventName(TCHAR *ptcsName);
  155. #endif
  156. #endif
  157. private:
  158. BOOL _fTakeLock;
  159. DFLAGS _dfOpenLock;
  160. IMalloc * const _pMalloc;
  161. #if WIN32 >= 100
  162. LARGE_INTEGER _luidMutexName;
  163. #endif
  164. };
  165. inline CGlobalContext::CGlobalContext(IMalloc *pMalloc)
  166. : _pMalloc(pMalloc)
  167. {
  168. #if WIN32 >= 100
  169. // Use a luid as the name for the mutex because the 64-bit
  170. // luid generator is guaranteed to produce machine-wide unique
  171. // values each time it is called, so if we use one for our mutex
  172. // name we know it won't collide with any others
  173. #if !defined(MULTIHEAP)
  174. _luidMutexName.QuadPart = PBasicEntry::GetNewLuid(pMalloc);
  175. #else
  176. if (DFBASEPTR == NULL) // task memory support
  177. {
  178. LUID luid;
  179. AllocateLocallyUniqueId (&luid);
  180. _luidMutexName.LowPart = luid.LowPart;
  181. _luidMutexName.HighPart = luid.HighPart;
  182. }
  183. else
  184. {
  185. _luidMutexName.QuadPart = g_smAllocator.GetHeapName();
  186. }
  187. #endif
  188. #endif
  189. }
  190. inline void CGlobalContext::SetLockInfo(BOOL fTakeLock, DFLAGS dfOpenLock)
  191. {
  192. _fTakeLock = fTakeLock;
  193. _dfOpenLock = dfOpenLock;
  194. }
  195. //+--------------------------------------------------------------
  196. //
  197. // Member: CGlobalContext::TakeLock, public
  198. //
  199. // Synopsis: Returns whether locks should be taken or not
  200. //
  201. // History: 04-Sep-92 DrewB Created
  202. //
  203. //---------------------------------------------------------------
  204. inline BOOL CGlobalContext::TakeLock(void) const
  205. {
  206. return _fTakeLock;
  207. }
  208. //+--------------------------------------------------------------
  209. //
  210. // Member: CGlobalContext::GetOpenLockFlags, public
  211. //
  212. // Synopsis: Returns the open lock flags
  213. //
  214. // History: 04-Sep-92 DrewB Created
  215. //
  216. //---------------------------------------------------------------
  217. inline DFLAGS CGlobalContext::GetOpenLockFlags(void) const
  218. {
  219. return _dfOpenLock;
  220. }
  221. //+--------------------------------------------------------------
  222. //
  223. // Member: CGlobalContext::GetMalloc, public
  224. //
  225. // Synopsis: Returns the allocator associated with this global context
  226. //
  227. // History: 05-May-93 AlexT Created
  228. //
  229. //---------------------------------------------------------------
  230. inline IMalloc *CGlobalContext::GetMalloc(void) const
  231. {
  232. return (IMalloc *)_pMalloc;
  233. }
  234. //+---------------------------------------------------------------------------
  235. //
  236. // Member: CGlobalContext::GetMutexName, public
  237. //
  238. // Synopsis: Returns the name to use for the mutex for this tree
  239. //
  240. // Arguments: [ptcsName] - Name return
  241. //
  242. // Modifies: [ptcsName]
  243. //
  244. // History: 09-Oct-93 DrewB Created
  245. //
  246. // Notes: [ptcsName] should have space for at least
  247. // CONTEXT_MUTEX_NAME_LENGTH characters
  248. //
  249. //----------------------------------------------------------------------------
  250. #if WIN32 >= 100
  251. inline void CGlobalContext::GetMutexName(TCHAR *ptcsName)
  252. {
  253. StringCchPrintf (ptcsName, CONTEXT_MUTEX_NAME_LENGTH,
  254. TSTR("OleDfRoot%X%08lX"), _luidMutexName.HighPart,
  255. _luidMutexName.LowPart);
  256. }
  257. #ifdef ASYNC
  258. inline void CGlobalContext::GetEventName(TCHAR *ptcsName)
  259. {
  260. StringCchPrintf (ptcsName, CONTEXT_MUTEX_NAME_LENGTH,
  261. TSTR("OleAsyncE%X%08lX"), _luidMutexName.HighPart,
  262. _luidMutexName.LowPart);
  263. }
  264. #endif // ASYNC
  265. #endif
  266. inline void CPerContext::SetILBInfo(ILockBytes *plkbBase,
  267. CFileStream *pfstDirty,
  268. ILockBytes *plkbOriginal,
  269. ULONG ulOpenLock)
  270. {
  271. _plkbBase = plkbBase;
  272. _pfstDirty = pfstDirty;
  273. _plkbOriginal = plkbOriginal;
  274. _ulOpenLock = ulOpenLock;
  275. #ifdef ASYNC
  276. _pfi = NULL;
  277. if (_plkbBase)
  278. {
  279. IFillInfo *pfi;
  280. HRESULT hr = _plkbBase->QueryInterface(IID_IFillInfo,
  281. (void **)&pfi);
  282. if (SUCCEEDED(hr))
  283. {
  284. if (SUCCEEDED(InitNotificationEvent(_plkbBase)))
  285. {
  286. _pfi = pfi;
  287. }
  288. else
  289. {
  290. pfi->Release();
  291. }
  292. }
  293. }
  294. #endif
  295. }
  296. //+--------------------------------------------------------------
  297. //
  298. // Member: CPerContext::CPerContext, public
  299. //
  300. // Synopsis: Constructor
  301. //
  302. // Arguments: [plkbBase] - Base lstream
  303. // [pfstDirty] - Dirty lstream
  304. // [plkbOriginal] - Original base lstream for
  305. // independent copies
  306. // [ulOpenLock] - Open lock for base lstream
  307. //
  308. // History: 14-Aug-92 DrewB Created
  309. // 18-May-93 AlexT Added pMalloc
  310. //
  311. //---------------------------------------------------------------
  312. inline CPerContext::CPerContext(
  313. IMalloc *pMalloc,
  314. ILockBytes *plkbBase,
  315. CFileStream *pfstDirty,
  316. ILockBytes *plkbOriginal,
  317. ULONG ulOpenLock)
  318. : _pMalloc(pMalloc)
  319. {
  320. _plkbBase = plkbBase;
  321. _pfstDirty = pfstDirty;
  322. _plkbOriginal = plkbOriginal;
  323. _ulOpenLock = ulOpenLock;
  324. _cReferences = 1;
  325. #ifdef ASYNC
  326. _cRefSharedMem = 1;
  327. #endif
  328. _pgc = NULL;
  329. #ifdef MULTIHEAP
  330. g_smAllocator.GetState(&_psmb, &_pbBase, &_ulHeapName);
  331. #endif
  332. #ifdef DIRECTWRITERLOCK
  333. _cRecursion = 0; // recursion count of writers
  334. #endif
  335. }
  336. //+--------------------------------------------------------------
  337. //
  338. // Member: CPerContext::CPerContext, public
  339. //
  340. // Synopsis: Constructor for a temporary stack-based object
  341. // This is only used for unmarshaling, since we
  342. // need a percontext to own the heap before the
  343. // actual percontext is unmarshaled
  344. //
  345. // Arguments: [pMalloc] - Base IMalloc
  346. //
  347. // History: 29-Nov-95 HenryLee Created
  348. //
  349. //---------------------------------------------------------------
  350. inline CPerContext::CPerContext(IMalloc *pMalloc) : _pMalloc(pMalloc)
  351. {
  352. #ifdef MULTIHEAP
  353. g_smAllocator.GetState(&_psmb, &_pbBase, &_ulHeapName);
  354. #endif
  355. _plkbBase = _plkbOriginal = NULL;
  356. _pfstDirty = NULL;
  357. _ulOpenLock = 0;
  358. _cReferences = 1;
  359. #ifdef ASYNC
  360. _cRefSharedMem = 1;
  361. #endif
  362. _pgc = NULL;
  363. #ifdef ASYNC
  364. _pfi = NULL;
  365. _hNotificationEvent = INVALID_HANDLE_VALUE;
  366. #endif
  367. #ifdef DIRECTWRITERLOCK
  368. _cRecursion = 0; // recursion count of writers
  369. #endif
  370. }
  371. #ifdef ASYNC
  372. inline LONG CPerContext::AddRefSharedMem(void)
  373. {
  374. olAssert(_cRefSharedMem >= _cReferences);
  375. AtomicInc(&_cRefSharedMem);
  376. olAssert(_cRefSharedMem >= _cReferences);
  377. return _cRefSharedMem;
  378. }
  379. inline LONG CPerContext::ReleaseSharedMem(void)
  380. {
  381. LONG lRet;
  382. olAssert(_cRefSharedMem > 0);
  383. olAssert(_cRefSharedMem >= _cReferences);
  384. lRet = AtomicDec(&_cRefSharedMem);
  385. if ((_cReferences == 0) && (_cRefSharedMem == 0))
  386. delete this;
  387. return lRet;
  388. }
  389. inline LONG CPerContext::DecRef(void)
  390. {
  391. olAssert(_cRefSharedMem >= _cReferences);
  392. AtomicDec(&_cReferences);
  393. olAssert(_cRefSharedMem >= _cReferences);
  394. return _cReferences;
  395. }
  396. #endif
  397. //+---------------------------------------------------------------------------
  398. //
  399. // Member: CPerContext::AddRef, public
  400. //
  401. // Synopsis: Increments the ref count
  402. //
  403. // History: 27-Oct-92 DrewB Created
  404. //
  405. //----------------------------------------------------------------------------
  406. inline LONG CPerContext::AddRef(void)
  407. {
  408. #ifdef ASYNC
  409. olAssert(_cRefSharedMem >= _cReferences);
  410. AddRefSharedMem();
  411. #endif
  412. AtomicInc(&_cReferences);
  413. #ifdef ASYNC
  414. olAssert(_cRefSharedMem >= _cReferences);
  415. #endif
  416. return _cReferences;
  417. }
  418. //+---------------------------------------------------------------------------
  419. //
  420. // Member: CPerContext::Release, public
  421. //
  422. // Synopsis: Decrements the ref count
  423. //
  424. // History: 27-Oct-92 DrewB Created
  425. //
  426. //----------------------------------------------------------------------------
  427. inline LONG CPerContext::Release(void)
  428. {
  429. LONG lRet;
  430. olAssert(_cReferences > 0);
  431. #ifdef ASYNC
  432. olAssert(_cRefSharedMem >= _cReferences);
  433. #endif
  434. lRet = AtomicDec(&_cReferences);
  435. if (lRet == 0)
  436. {
  437. #ifdef ASYNC
  438. if (_plkbBase != NULL)
  439. {
  440. Close();
  441. }
  442. #else
  443. delete this;
  444. #endif
  445. }
  446. #ifdef ASYNC
  447. olAssert(_cRefSharedMem >= _cReferences);
  448. //Note: If the object is going to get deleted, it will happen
  449. // in the ReleaseSharedMem call.
  450. lRet = ReleaseSharedMem();
  451. #endif
  452. return lRet;
  453. }
  454. //+--------------------------------------------------------------
  455. //
  456. // Member: CPerContext::GetBase, public
  457. //
  458. // Synopsis: Returns the base
  459. //
  460. // History: 14-Aug-92 DrewB Created
  461. //
  462. //---------------------------------------------------------------
  463. inline ILockBytes *CPerContext::GetBase(void) const
  464. {
  465. return _plkbBase;
  466. }
  467. //+--------------------------------------------------------------
  468. //
  469. // Member: CPerContext::GetDirty, public
  470. //
  471. // Synopsis: Returns the dirty
  472. //
  473. // History: 14-Aug-92 DrewB Created
  474. //
  475. //---------------------------------------------------------------
  476. inline CFileStream *CPerContext::GetDirty(void) const
  477. {
  478. return _pfstDirty;
  479. }
  480. //+--------------------------------------------------------------
  481. //
  482. // Member: CPerContext::GetOriginal, public
  483. //
  484. // Synopsis: Returns the Original
  485. //
  486. // History: 14-Aug-92 DrewB Created
  487. //
  488. //---------------------------------------------------------------
  489. inline ILockBytes *CPerContext::GetOriginal(void) const
  490. {
  491. return _plkbOriginal;
  492. }
  493. //+--------------------------------------------------------------
  494. //
  495. // Member: CPerContext::GetOpenLock, public
  496. //
  497. // Synopsis: Returns the open lock index
  498. //
  499. // History: 04-Sep-92 DrewB Created
  500. //
  501. //---------------------------------------------------------------
  502. inline ULONG CPerContext::GetOpenLock(void) const
  503. {
  504. return _ulOpenLock;
  505. }
  506. //+--------------------------------------------------------------
  507. //
  508. // Member: CPerContext::GetContextId, public
  509. //
  510. // Synopsis: Returns the context id
  511. //
  512. // History: 04-Sep-92 DrewB Created
  513. //
  514. //---------------------------------------------------------------
  515. inline ContextId CPerContext::GetContextId(void) const
  516. {
  517. return ctxid;
  518. }
  519. //+--------------------------------------------------------------
  520. //
  521. // Member: CPerContext::GetMalloc, public
  522. //
  523. // Synopsis: Returns the IMalloc pointer
  524. //
  525. // History: 04-Apr-96 PhilipLa Created
  526. //
  527. //---------------------------------------------------------------
  528. inline IMalloc * CPerContext::GetMalloc(void) const
  529. {
  530. return _pMalloc;
  531. }
  532. //+--------------------------------------------------------------
  533. //
  534. // Member: CPerContext::GetGlobal, public
  535. //
  536. // Synopsis: Returns the global context
  537. //
  538. // History: 04-Sep-92 DrewB Created
  539. //
  540. //---------------------------------------------------------------
  541. inline CGlobalContext *CPerContext::GetGlobal(void) const
  542. {
  543. return BP_TO_P(CGlobalContext *, _pgc);
  544. }
  545. inline void CPerContext::SetLockInfo(BOOL fTakeLock,
  546. DFLAGS dfOpenLock)
  547. {
  548. _pgc->SetLockInfo(fTakeLock, dfOpenLock);
  549. }
  550. //+---------------------------------------------------------------------------
  551. //
  552. // Member: CPerContext::InitNewContext, public
  553. //
  554. // Synopsis: Creates a new context and context list
  555. //
  556. // Returns: Appropriate status code
  557. //
  558. // History: 27-Oct-92 DrewB Created
  559. //
  560. //----------------------------------------------------------------------------
  561. inline SCODE CPerContext::InitNewContext(void)
  562. {
  563. SCODE sc;
  564. CGlobalContext *pgcTemp;
  565. sc = (pgcTemp = new (_pMalloc) CGlobalContext(_pMalloc)) == NULL ?
  566. STG_E_INSUFFICIENTMEMORY : S_OK;
  567. if (SUCCEEDED(sc))
  568. {
  569. _pgc = P_TO_BP(CBasedGlobalContextPtr, pgcTemp);
  570. #if WIN32 >= 100
  571. TCHAR atcMutexName[CONTEXT_MUTEX_NAME_LENGTH];
  572. _pgc->GetMutexName(atcMutexName);
  573. sc = _dmtx.Init(atcMutexName);
  574. if (FAILED(sc))
  575. {
  576. _pgc->Release();
  577. _pgc = NULL;
  578. }
  579. else
  580. #endif
  581. _pgc->Add(this);
  582. }
  583. return sc;
  584. }
  585. //+---------------------------------------------------------------------------
  586. //
  587. // Member: CPerContext::InitFromGlobal, public
  588. //
  589. // Synopsis: Adds a context to the context list
  590. //
  591. // Returns: Appropriate status code
  592. //
  593. // History: 27-Oct-92 DrewB Created
  594. //
  595. //----------------------------------------------------------------------------
  596. inline SCODE CPerContext::InitFromGlobal(CGlobalContext *pgc)
  597. {
  598. SCODE sc;
  599. sc = S_OK;
  600. #if WIN32 >= 100
  601. TCHAR atcMutexName[CONTEXT_MUTEX_NAME_LENGTH];
  602. pgc->GetMutexName(atcMutexName);
  603. sc = _dmtx.Init(atcMutexName);
  604. #endif
  605. if (SUCCEEDED(sc))
  606. {
  607. _pgc = P_TO_BP(CBasedGlobalContextPtr, pgc);
  608. _pgc->AddRef();
  609. _pgc->Add(this);
  610. }
  611. return sc;
  612. }
  613. //+---------------------------------------------------------------------------
  614. //
  615. // Member: CPerContext::SetOpenLock, public
  616. //
  617. // Synopsis: Sets the open lock
  618. //
  619. // History: 13-Jan-93 DrewB Created
  620. //
  621. //----------------------------------------------------------------------------
  622. inline void CPerContext::SetOpenLock(ULONG ulOpenLock)
  623. {
  624. _ulOpenLock = ulOpenLock;
  625. }
  626. //+---------------------------------------------------------------------------
  627. //
  628. // Member: CPerContext::TakeSem, public
  629. //
  630. // Synopsis: Takes the mutex
  631. //
  632. // Arguments: [dwTimeout] - Timeout
  633. //
  634. // Returns: Appropriate status code
  635. //
  636. // History: 09-Oct-93 DrewB Created
  637. //
  638. //----------------------------------------------------------------------------
  639. inline SCODE CPerContext::TakeSem(DWORD dwTimeout)
  640. {
  641. #if WIN32 >= 100
  642. SCODE sc;
  643. olChk(_dmtx.Take(dwTimeout));
  644. #ifdef ONETHREAD
  645. olChkTo(EH_Tree, s_dmtxProcess.Take(dwTimeout));
  646. #endif
  647. #ifdef MULTIHEAP
  648. if (_psmb && !_psmb->IsSynced())
  649. {
  650. olChkTo(EH_Process, _psmb->Sync());
  651. }
  652. #else
  653. olChkTo(EH_Process, DfSyncSharedMemory());
  654. #endif
  655. EH_Err:
  656. return sc;
  657. EH_Process:
  658. #ifdef ONETHREAD
  659. s_dmtxProcess.Release();
  660. EH_Tree:
  661. #endif
  662. _dmtx.Release();
  663. return sc;
  664. #else
  665. return S_OK;
  666. #endif
  667. }
  668. //+---------------------------------------------------------------------------
  669. //
  670. // Member: CPerContext::ReleaseSem, public
  671. //
  672. // Synopsis: Releases the mutex
  673. //
  674. // History: 09-Oct-93 DrewB Created
  675. //
  676. //----------------------------------------------------------------------------
  677. inline void CPerContext::ReleaseSem(void)
  678. {
  679. #if WIN32 >= 100
  680. #ifdef ONETHREAD
  681. s_dmtxProcess.Release();
  682. #endif
  683. _dmtx.Release();
  684. #endif
  685. }
  686. #ifdef MULTIHEAP
  687. //+-------------------------------------------------------------------------
  688. //
  689. // Member: CPerContext::SetThreadAllocatorState, public
  690. //
  691. // Synopsis: set current thread's allocator to be this percontext
  692. //
  693. // History: 29-Nov-95 HenryLee Created
  694. //
  695. //--------------------------------------------------------------------------
  696. inline CSmAllocator *CPerContext::SetThreadAllocatorState(CPerContext**pppcPrev)
  697. {
  698. CSmAllocator *pSmAllocator = &g_smAllocator;
  699. pSmAllocator->SetState(_psmb, _pbBase, _ulHeapName, pppcPrev, this);
  700. return pSmAllocator;
  701. }
  702. //+---------------------------------------------------------------------------
  703. //
  704. // Member: CPerContext::SetAllocatorState, public
  705. //
  706. // Synopsis: sets owner of shared memory heap to this percontext
  707. // remembers the previous context owner
  708. //
  709. // History: 29-Nov-95 HenryLee Created
  710. //
  711. //----------------------------------------------------------------------------
  712. inline SCODE CPerContext::SetAllocatorState (CPerContext **pppcPrev,
  713. CSmAllocator *pSmAllocator)
  714. {
  715. pSmAllocator->SetState(_psmb, _pbBase, _ulHeapName, pppcPrev, this);
  716. return S_OK;
  717. }
  718. //+--------------------------------------------------------------
  719. //
  720. // Member: CPerContext::GetThreadAllocatorState, public
  721. //
  722. // Synopsis: retrives the current thread's allocator state
  723. //
  724. // Arguments: none
  725. //
  726. // History: 29-Nov-95 HenryLee Created
  727. //
  728. //---------------------------------------------------------------
  729. inline SCODE CPerContext::GetThreadAllocatorState()
  730. {
  731. g_smAllocator.GetState(&_psmb, &_pbBase, &_ulHeapName);
  732. return S_OK;
  733. }
  734. //+--------------------------------------------------------------
  735. //
  736. // Class: CSafeMultiHeap
  737. //
  738. // Purpose: 1) sets and restores allocator state for those
  739. // methods that do not take the tree mutex
  740. // 2) for self-destructive methods like IStorage::Release,
  741. // IStream::Release, IEnumSTATSTG::Release, the
  742. // previous percontext may get deleted along with
  743. // whole heap, and it checks for that
  744. //
  745. // Interface: See below
  746. //
  747. // History: 29-Nov-95 HenryLee Created
  748. //
  749. //---------------------------------------------------------------
  750. class CSafeMultiHeap
  751. {
  752. public:
  753. CSafeMultiHeap(CPerContext *ppc);
  754. ~CSafeMultiHeap();
  755. private:
  756. CSmAllocator *_pSmAllocator;
  757. CPerContext *_ppcPrev;
  758. };
  759. #endif // MULTIHEAP
  760. #ifdef ASYNC
  761. inline IFillInfo * CPerContext::GetFillInfo(void) const
  762. {
  763. return _pfi;
  764. }
  765. inline HANDLE CPerContext::GetNotificationEvent(void) const
  766. {
  767. return _hNotificationEvent;
  768. }
  769. #if DBG == 1
  770. inline BOOL CPerContext::HaveMutex(void)
  771. {
  772. return _dmtx.HaveMutex();
  773. }
  774. #endif // #if DBG == 1
  775. #endif // #ifdef ASYNC
  776. inline BOOL CPerContext::IsHandleValid ()
  777. {
  778. TCHAR tcsName[CONTEXT_MUTEX_NAME_LENGTH * 2];
  779. StringCchCopy (tcsName, CONTEXT_MUTEX_NAME_LENGTH,
  780. TEXT("\\BaseNamedObjects\\"));
  781. _pgc->GetMutexName(&tcsName[lstrlen(tcsName)]);
  782. BOOL fValid = _dmtx.IsHandleValid(tcsName);
  783. if (!fValid) // if this object is not valid, don't let anyone else
  784. { // use it because its per-process handles are bogus
  785. ctxid = 0;
  786. }
  787. return fValid;
  788. }
  789. #endif // #ifndef __CONTEXT_HXX__