Windows NT 4.0 source code leak
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.

657 lines
12 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. State.hxx
  6. Abstract:
  7. Handles state objects and critical sections.
  8. Author:
  9. Albert Ting (AlbertT) 28-May-1994
  10. Revision History:
  11. --*/
  12. #ifndef _STATE_HXX
  13. #define _STATE_HXX
  14. typedef DWORD STATEVAR;
  15. /********************************************************************
  16. State object (DWORD) for bitfields.
  17. ********************************************************************/
  18. class TState {
  19. SIGNATURE( 'stat' )
  20. ALWAYS_VALID
  21. SAFE_NEW
  22. public:
  23. #if DBG
  24. TState( VOID );
  25. TState( STATEVAR StateVar );
  26. ~TState( VOID );
  27. STATEVAR operator|=( STATEVAR StateVarOn );
  28. STATEVAR operator&=( STATEVAR StateVarMask );
  29. STATEVAR operator|=( INT iStateVarOn )
  30. { return operator|=( (STATEVAR)iStateVarOn ); }
  31. STATEVAR operator&=( INT iStateVarMask )
  32. { return operator&=( (STATEVAR)iStateVarMask ); }
  33. #else
  34. TState(VOID) : _StateVar(0)
  35. { }
  36. TState(STATEVAR StateVar) : _StateVar( StateVar )
  37. { }
  38. ~TState(VOID)
  39. { }
  40. STATEVAR operator|=( STATEVAR StateVarOn )
  41. { return _StateVar |= StateVarOn; }
  42. STATEVAR operator&=( STATEVAR StateVarMask )
  43. { return _StateVar &= StateVarMask; }
  44. STATEVAR operator|=( INT iStateVarOn )
  45. { return operator|=( (STATEVAR)iStateVarOn ); }
  46. STATEVAR operator&=( INT iStateVarMask )
  47. { return operator&=( (STATEVAR)iStateVarMask ); }
  48. #endif
  49. BOOL bBit( STATEVAR StateVarBit )
  50. { return _StateVar & StateVarBit ? TRUE : FALSE; }
  51. STATEVAR operator&( TState& State )
  52. { return _StateVar & State._StateVar; }
  53. STATEVAR operator=( STATEVAR StateVarNew )
  54. { return _StateVar = StateVarNew; }
  55. TState& operator=( const TState& StateNew )
  56. {
  57. _StateVar = StateNew._StateVar;
  58. return *this;
  59. }
  60. operator STATEVAR() const
  61. { return _StateVar; }
  62. private:
  63. STATEVAR _StateVar;
  64. #if DBG
  65. TBackTraceMem _BackTrace;
  66. virtual BOOL bValidateState() const
  67. { return TRUE; }
  68. virtual BOOL bValidateSet(STATEVAR StateVarOn) const
  69. {
  70. UNREFERENCED_PARAMETER( StateVarOn );
  71. return TRUE;
  72. }
  73. virtual BOOL bValidateMask(STATEVAR StateVarMask) const
  74. {
  75. UNREFERENCED_PARAMETER( StateVarMask );
  76. return TRUE;
  77. }
  78. #endif
  79. };
  80. /********************************************************************
  81. Critical section implementation
  82. Avoid using vEnter and vLeave, since there may be a codepath
  83. that forgets to call one or the other. Instead, use
  84. TCritSec{Hard}Lock and TCritSecUnlock.
  85. MCritSec:
  86. This is the basic critical section. It has vEnter and vLeave,
  87. but these should be used sparingly--use TCritSec*Lock instead.
  88. TCritSecLock:
  89. Used to acquire a critical section for the lifetime of the
  90. TCritSecLock object.
  91. {
  92. foo();
  93. {
  94. TCritSecLock CSL( CritSec ); // CritSec acquired here.
  95. bar();
  96. } // CritSec released here since
  97. // CSL is destroyed.
  98. }
  99. -----------------------------------------------------------------
  100. TCritSecUnlock:
  101. Used to release an acquired critical section for the lifetime of
  102. the TCritSecUnlock object.
  103. Note: The critical section _must_ already be acquired.
  104. {
  105. TCritSecLock CSL( CritSec );
  106. while( bDoIt ){
  107. {
  108. TCritSecLock CSU( CritSec );
  109. Sleep( 10000 );
  110. }
  111. vTouchProtectedData();
  112. }
  113. }
  114. -----------------------------------------------------------------
  115. TCritSecHardLock:
  116. ** Acts same as TCritSecLock on free builds, but on CHK builds it
  117. ** asserts if lock is freed while lock is held.
  118. {
  119. TCritSecLock CSL( CritSec ); // <- *** (A)
  120. TBar *pBar = gData.pBar;
  121. foo();
  122. pBar->UseMe();
  123. }
  124. In the above snippet, the author wants the entire block to be
  125. within the CritSec. However, he/she didn't realize foo leaves
  126. CritSec because it needs to hit the net--and in doing so, gData.pBar
  127. may have been deleted/changed by another thread. For example,
  128. foo() may do the following:
  129. VOID
  130. foo(
  131. VOID
  132. )
  133. {
  134. {
  135. TCritSecUnlock( CritSec );
  136. vLongOperationOverNet(); // <- BUG! Callee assumes
  137. // CritSec _not_ left!
  138. }
  139. //
  140. // Back inside CritSec.
  141. //
  142. }
  143. To catch this in debug builds, (A) above should use TCritSecHardLock.
  144. This will assert when we try and leave CritSec. Care must still
  145. be taken, however, since leaving CritSec may be a rare codepath
  146. and not hit during testing.
  147. -----------------------------------------------------------------
  148. Note: TCrit*Lock uses space to store the critical section. In
  149. cases where this is known or already stored somewhere, this is
  150. wasted space. We should change this to use templates to avoid
  151. this extra reference.
  152. ********************************************************************/
  153. class MCritSec;
  154. class TCritSecLock {
  155. SIGNATURE( 'cslk' )
  156. ALWAYS_VALID
  157. SAFE_NEW
  158. public:
  159. TCritSecLock(
  160. MCritSec& CritSec
  161. );
  162. ~TCritSecLock(
  163. VOID
  164. );
  165. private:
  166. MCritSec& _CritSec;
  167. };
  168. class TCritSecUnlock {
  169. SIGNATURE( 'csul' )
  170. ALWAYS_VALID
  171. SAFE_NEW
  172. public:
  173. TCritSecUnlock(
  174. MCritSec& CritSec
  175. );
  176. ~TCritSecUnlock(
  177. VOID
  178. );
  179. private:
  180. MCritSec& _CritSec;
  181. };
  182. class TCritSecHardLock {
  183. friend MCritSec;
  184. SIGNATURE( 'cshl' )
  185. ALWAYS_VALID
  186. SAFE_NEW
  187. public:
  188. TCritSecHardLock(
  189. MCritSec& CritSec
  190. );
  191. ~TCritSecHardLock(
  192. VOID
  193. );
  194. private:
  195. MCritSec& _CritSec;
  196. #if DBG
  197. DWORD _dwEntryCountMarker;
  198. DLINK( TCritSecHardLock, CritSecHardLock );
  199. #endif
  200. };
  201. /********************************************************************
  202. MCritSec
  203. ********************************************************************/
  204. class MCritSec {
  205. friend TDebugExt;
  206. friend TCritSecHardLock;
  207. SIGNATURE( 'crsc' )
  208. ALWAYS_VALID
  209. SAFE_NEW
  210. public:
  211. #if DBG
  212. MCritSec();
  213. ~MCritSec();
  214. VOID vEnter();
  215. VOID vLeave();
  216. BOOL bInside() const;
  217. BOOL bOutside() const;
  218. #else
  219. MCritSec()
  220. { InitializeCriticalSection(&_CritSec); }
  221. ~MCritSec()
  222. { DeleteCriticalSection(&_CritSec); }
  223. VOID vEnter()
  224. { EnterCriticalSection(&_CritSec); }
  225. VOID vLeave()
  226. { LeaveCriticalSection(&_CritSec); }
  227. #endif
  228. private:
  229. CRITICAL_SECTION _CritSec;
  230. #if DBG
  231. //
  232. // Current owner, entry count, and tickcount at entry time. These
  233. // values change for each entrance.
  234. //
  235. DWORD _dwThreadOwner;
  236. DWORD _dwEntryCount;
  237. DWORD _dwTickCountEntered;
  238. //
  239. // Statistics gathering.
  240. //
  241. DWORD _dwTickCountBlockedTotal;
  242. DWORD _dwTickCountInsideTotal;
  243. DWORD _dwEntryCountTotal;
  244. DLINK_BASE( TCritSecHardLock, CritSecHardLock, CritSecHardLock );
  245. TBackTraceMem _BackTrace;
  246. #endif
  247. };
  248. /********************************************************************
  249. MRef and TRefLock: Locking objects used for ref counting.
  250. class TFoo : public MRef, public MGenWin {
  251. ...
  252. };
  253. class TUserOfFoo {
  254. private:
  255. REF_LOCK( TFoo, Foo );
  256. ...
  257. };
  258. //
  259. // Acquires lock on foo by initialization.
  260. //
  261. TUserOfFoo( TFoo* pFoo ) : RL_Foo( pFoo ) {}
  262. //
  263. // During destruction of TUserOfFoo, the reference count
  264. // of pFoo is automatically decremented.
  265. //
  266. //
  267. // Acquires lock on foo after initialization.
  268. //
  269. vGetLock()
  270. {
  271. Foo.vAcquire( pFoo1 );
  272. ...
  273. //
  274. // Get the pFoo pointer from RLFoo.
  275. //
  276. pFoo1 = pFoo();
  277. Foo.vRelease( pFoo1 );
  278. }
  279. ********************************************************************/
  280. inline
  281. TCritSecLock::
  282. TCritSecLock(
  283. MCritSec& CritSec
  284. ) : _CritSec( CritSec )
  285. {
  286. _CritSec.vEnter();
  287. }
  288. inline
  289. TCritSecLock::
  290. ~TCritSecLock(
  291. VOID
  292. )
  293. {
  294. _CritSec.vLeave();
  295. }
  296. inline
  297. TCritSecUnlock::
  298. TCritSecUnlock(
  299. MCritSec& CritSec
  300. ) : _CritSec( CritSec )
  301. {
  302. _CritSec.vLeave();
  303. SPLASSERT( _CritSec.bOutside( ));
  304. }
  305. inline
  306. TCritSecUnlock::
  307. ~TCritSecUnlock( VOID )
  308. {
  309. _CritSec.vEnter();
  310. }
  311. #if !DBG
  312. inline
  313. TCritSecHardLock::
  314. TCritSecHardLock(
  315. MCritSec& CritSec
  316. ) : _CritSec( CritSec )
  317. {
  318. _CritSec.vEnter();
  319. }
  320. inline
  321. TCritSecHardLock::
  322. ~TCritSecHardLock(
  323. VOID
  324. )
  325. {
  326. _CritSec.vLeave();
  327. }
  328. #endif
  329. class TRefLock;
  330. /************************************************************
  331. VRef: virtual base class for all ref counting.
  332. ************************************************************/
  333. class VRef {
  334. SAFE_NEW
  335. public:
  336. virtual ~VRef()
  337. { }
  338. //
  339. // Virtuals that must be overwritten by the reference classes.
  340. //
  341. virtual VOID vIncRef() = 0;
  342. virtual LONG cDecRef() = 0;
  343. virtual VOID vDecRefDelete() = 0;
  344. protected:
  345. //
  346. // Clients of the reference class should override this class
  347. // to perform cleanup. Generally clients will implement vRefZeroed
  348. // to delete themselves.
  349. //
  350. virtual VOID vRefZeroed() = 0;
  351. };
  352. /************************************************************
  353. MRefNone: quick ref counter, no sync done.
  354. ************************************************************/
  355. class MRefQuick : public VRef {
  356. SIGNATURE( 'refq' )
  357. ALWAYS_VALID
  358. protected:
  359. VAR( LONG, cRef );
  360. public:
  361. #if DBG
  362. TBackTraceMem _BackTrace;
  363. MRefQuick( VOID ) : _cRef( 0 )
  364. { }
  365. ~MRefQuick( VOID )
  366. {
  367. SPLASSERT( !_cRef );
  368. }
  369. #else
  370. MRefQuick( VOID ) : _cRef( 0 )
  371. { }
  372. #endif
  373. VOID vIncRef();
  374. LONG cDecRef();
  375. VOID vDecRefDelete();
  376. };
  377. /************************************************************
  378. MRefCom: Refcount with interlocks
  379. ************************************************************/
  380. class MRefCom : public VRef {
  381. friend BOOL bSplLibInit( VOID );
  382. friend VOID vSplLibFree( VOID );
  383. SIGNATURE( 'refc' )
  384. ALWAYS_VALID
  385. public:
  386. #if DBG
  387. TBackTraceMem _BackTrace;
  388. MRefCom( VOID ) : _cRef( 0 )
  389. { }
  390. ~MRefCom( VOID )
  391. {
  392. SPLASSERT( !_cRef );
  393. }
  394. #else
  395. MRefCom( VOID ) : _cRef( 0 )
  396. { }
  397. #endif
  398. VOID vIncRef();
  399. LONG cDecRef();
  400. VOID vDecRefDelete();
  401. protected:
  402. //
  403. // Must be LONG, not REFCOUNT for Interlocked* apis.
  404. //
  405. VAR( LONG, cRef );
  406. private:
  407. #if DBG
  408. static MCritSec* gpcsCom;
  409. #endif
  410. };
  411. /************************************************************
  412. TRefLock: used to lock a VRef.
  413. ************************************************************/
  414. class TRefLock {
  415. SIGNATURE( 'refl' )
  416. ALWAYS_VALID
  417. SAFE_NEW
  418. public:
  419. VRef* _pRef;
  420. TRefLock(
  421. VOID
  422. ) : _pRef( NULL )
  423. { }
  424. TRefLock(
  425. VRef* pRef
  426. )
  427. {
  428. _pRef = pRef;
  429. _pRef->vIncRef( );
  430. }
  431. ~TRefLock( )
  432. {
  433. if( _pRef )
  434. _pRef->cDecRef( );
  435. }
  436. VOID vAcquire( VRef* pRef )
  437. {
  438. SPLASSERT( !_pRef );
  439. _pRef = pRef;
  440. _pRef->vIncRef( );
  441. }
  442. VOID vRelease( VOID )
  443. {
  444. SPLASSERT( _pRef );
  445. _pRef->cDecRef( );
  446. _pRef = NULL;
  447. }
  448. };
  449. #define REF_LOCK( type, name ) \
  450. type* p##name() \
  451. { return (type*)RL_##name._pRef; } \
  452. VOID name##_vAcquire( type* ptr ) \
  453. { RL_##name.vAcquire( ptr ); } \
  454. VOID name##_vRelease( VOID ) \
  455. { RL_##name.vRelease(); } \
  456. TRefLock RL_##name
  457. #endif // ndef _STATE_HXX