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.

189 lines
4.0 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // iaswin32.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // Declares wrappers around a variety of Win32 objects.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/10/2000 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef IASWIN32_H
  19. #define IASWIN32_H
  20. #if _MSC_VER >= 1000
  21. #pragma once
  22. #endif
  23. ///////////////////////////////////////////////////////////////////////////////
  24. //
  25. // CLASS
  26. //
  27. // CriticalSection
  28. //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. class CriticalSection
  31. {
  32. public:
  33. CriticalSection()
  34. {
  35. if (!InitializeCriticalSectionAndSpinCount(&cs, 0x80001000))
  36. { throw std::bad_alloc(); }
  37. }
  38. ~CriticalSection()
  39. { DeleteCriticalSection(&cs); }
  40. void lock() throw ()
  41. { EnterCriticalSection(&cs); }
  42. void unlock() throw ()
  43. { LeaveCriticalSection(&cs); }
  44. bool tryLock() throw ()
  45. { return TryEnterCriticalSection(&cs) != FALSE; }
  46. private:
  47. CRITICAL_SECTION cs;
  48. // Not implemented.
  49. CriticalSection(const CriticalSection&);
  50. CriticalSection& operator=(const CriticalSection&);
  51. };
  52. ///////////////////////////////////////////////////////////////////////////////
  53. //
  54. // CLASS
  55. //
  56. // Event
  57. //
  58. ///////////////////////////////////////////////////////////////////////////////
  59. class Event
  60. {
  61. public:
  62. Event(BOOL manualReset = FALSE, BOOL initialState = FALSE)
  63. : h(CreateEvent(NULL, manualReset, initialState, NULL))
  64. { if (!h) { throw std::bad_alloc(); } }
  65. ~Event() throw ()
  66. { CloseHandle(h); }
  67. void reset() throw ()
  68. { ResetEvent(h); }
  69. void set() throw ()
  70. { SetEvent(h); }
  71. void wait(ULONG msec = INFINITE) throw ()
  72. { WaitForSingleObject(h, msec); }
  73. operator HANDLE() throw ()
  74. { return h; }
  75. private:
  76. HANDLE h;
  77. // Not implemented.
  78. Event(const Event&);
  79. Event& operator=(const Event&);
  80. };
  81. ///////////////////////////////////////////////////////////////////////////////
  82. //
  83. // CLASS
  84. //
  85. // RWLock
  86. //
  87. // DESCRIPTION
  88. //
  89. // This class implements a RWLock synchronization object. Threads
  90. // may request either exclusive or shared access to the protected area.
  91. //
  92. ///////////////////////////////////////////////////////////////////////////////
  93. class RWLock
  94. {
  95. public:
  96. RWLock();
  97. ~RWLock() throw ();
  98. void Lock() const throw ();
  99. void LockExclusive() throw ();
  100. void Unlock() const throw ();
  101. protected:
  102. // Number of threads sharing the perimeter.
  103. mutable LONG sharing;
  104. // Number of threads waiting for shared access.
  105. LONG waiting;
  106. // Pointer to either sharing or waiting depending on the current state of
  107. // the perimeter.
  108. PLONG count;
  109. // Synchronizes exclusive access.
  110. mutable CRITICAL_SECTION exclusive;
  111. // Wakes up threads waiting for shared access.
  112. HANDLE sharedOK;
  113. // Wakes up threads waiting for exclusive access.
  114. HANDLE exclusiveOK;
  115. private:
  116. // Not implemented.
  117. RWLock(const RWLock&) throw ();
  118. RWLock& operator=(const RWLock&) throw ();
  119. };
  120. ///////////////////////////////////////////////////////////////////////////////
  121. //
  122. // CLASS
  123. //
  124. // Count
  125. //
  126. ///////////////////////////////////////////////////////////////////////////////
  127. class Count
  128. {
  129. public:
  130. Count() throw ()
  131. : value(0)
  132. { }
  133. Count(const Count& c) throw ()
  134. : value(c)
  135. { }
  136. Count& operator=(const Count& c) throw ()
  137. {
  138. value = c.value;
  139. return *this;
  140. }
  141. LONG operator=(LONG l) throw ()
  142. {
  143. value = l;
  144. return value;
  145. }
  146. LONG operator++() throw ()
  147. { return InterlockedIncrement(&value); }
  148. LONG operator--() throw ()
  149. { return InterlockedDecrement(&value); }
  150. operator LONG() const throw ()
  151. { return value; }
  152. private:
  153. LONG value;
  154. };
  155. #endif // IASWIN32_H