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.

233 lines
6.3 KiB

  1. /*++
  2. Copyright (c) 1999, Microsoft Corporation
  3. Module Name:
  4. sample\sync.h
  5. Abstract:
  6. The file contains the READ_WRITE_LOCK definition which allows
  7. multiple-reader/single-writer.
  8. --*/
  9. #ifndef _SYNC_H_
  10. #define _SYNC_H_
  11. //
  12. // type definition for READ_WRITE_LOCK
  13. //
  14. typedef struct _READ_WRITE_LOCK
  15. {
  16. CRITICAL_SECTION RWL_ReadWriteBlock;
  17. LONG RWL_ReaderCount;
  18. HANDLE RWL_ReaderDoneEvent;
  19. } READ_WRITE_LOCK, *PREAD_WRITE_LOCK;
  20. DWORD
  21. CreateReadWriteLock(
  22. PREAD_WRITE_LOCK pRWL);
  23. VOID
  24. DeleteReadWriteLock(
  25. PREAD_WRITE_LOCK pRWL);
  26. VOID
  27. AcquireReadLock(
  28. PREAD_WRITE_LOCK pRWL);
  29. VOID
  30. ReleaseReadLock(
  31. PREAD_WRITE_LOCK pRWL);
  32. VOID
  33. AcquireWriteLock(
  34. PREAD_WRITE_LOCK pRWL);
  35. VOID
  36. ReleaseWriteLock(
  37. PREAD_WRITE_LOCK pRWL);
  38. //
  39. // macro functions for manipulating a read-write lock
  40. //
  41. #define CREATE_READ_WRITE_LOCK(pRWL) \
  42. CreateReadWriteLock(pRWL)
  43. #define DELETE_READ_WRITE_LOCK(pRWL) \
  44. DeleteReadWriteLock(pRWL)
  45. #define READ_WRITE_LOCK_CREATED(pRWL) \
  46. ((pRWL)->RWL_ReaderDoneEvent != NULL)
  47. #define ACQUIRE_READ_LOCK(pRWL) \
  48. AcquireReadLock(pRWL)
  49. #define RELEASE_READ_LOCK(pRWL) \
  50. ReleaseReadLock(pRWL)
  51. #define ACQUIRE_WRITE_LOCK(pRWL) \
  52. AcquireWriteLock(pRWL)
  53. #define RELEASE_WRITE_LOCK(pRWL) \
  54. ReleaseWriteLock(pRWL)
  55. // atomic. works since a critical section can be entered recursively.
  56. #define WRITE_LOCK_TO_READ_LOCK(pRWL) \
  57. { \
  58. ACQUIRE_READ_LOCK(pRWL); \
  59. RELEASE_WRITE_LOCK(pRWL); \
  60. }
  61. //
  62. // type definition for generic locked list
  63. // access is sychronized with a critical section
  64. //
  65. typedef struct _LOCKED_LIST
  66. {
  67. CRITICAL_SECTION lock;
  68. LIST_ENTRY head;
  69. DWORD created;
  70. } LOCKED_LIST, *PLOCKED_LIST;
  71. //
  72. // VOID
  73. // INITIALIZE_LOCKED_LIST (
  74. // PLOCKED_LIST pLL
  75. // );
  76. //
  77. #define INITIALIZE_LOCKED_LIST(pLL) \
  78. { \
  79. do \
  80. { \
  81. __try { \
  82. InitializeCriticalSection(&((pLL)->lock)); \
  83. } \
  84. __except (EXCEPTION_EXECUTE_HANDLER) { \
  85. break; \
  86. } \
  87. InitializeListHead(&((pLL)->head)); \
  88. (pLL)->created = 0x12345678; \
  89. } while (FALSE); \
  90. }
  91. //
  92. // BOOL
  93. // LOCKED_LIST_INITIALIZED (
  94. // PLOCKED_LIST pLL
  95. // );
  96. //
  97. #define LOCKED_LIST_INITIALIZED(pLL) \
  98. ((pLL)->created == 0x12345678)
  99. //
  100. // VOID
  101. // DELETE_LOCKED_LIST (
  102. // PLOCKED_LIST pLL,
  103. // VOID(*FreeFunction)(PLIST_ENTRY)
  104. // );
  105. //
  106. #define DELETE_LOCKED_LIST(pLL, FreeFunction) \
  107. { \
  108. (pLL)->created = 0; \
  109. FreeList(&((pLL)->head), FreeFunction); \
  110. DeleteCriticalSection(&(pLL)->lock); \
  111. }
  112. #define ACQUIRE_LIST_LOCK(pLL) \
  113. EnterCriticalSection(&(pLL)->lock)
  114. #define RELEASE_LIST_LOCK(pLL) \
  115. LeaveCriticalSection(&(pLL)->lock)
  116. #define LOCKED_QUEUE LOCKED_LIST
  117. #define PLOCKED_QUEUE PLOCKED_LIST
  118. #define INITIALIZE_LOCKED_QUEUE(pLQ) \
  119. INITIALIZE_LOCKED_LIST(pLQ)
  120. #define LOCKED_QUEUE_INITIALIZED(pLQ) \
  121. LOCKED_LIST_INITIALIZED(pLQ)
  122. #define DELETE_LOCKED_QUEUE(pLQ, FreeFunction) \
  123. DELETE_LOCKED_LIST(pLQ, FreeFunction)
  124. #define ACQUIRE_QUEUE_LOCK(pLQ) \
  125. ACQUIRE_LIST_LOCK(pLQ)
  126. #define RELEASE_QUEUE_LOCK(pLQ) \
  127. RELEASE_LIST_LOCK(pLQ)
  128. //
  129. // type definition for DYNAMIC_READWRITE_LOCK
  130. //
  131. typedef struct _DYNAMIC_READWRITE_LOCK {
  132. READ_WRITE_LOCK rwlLock;
  133. union
  134. {
  135. ULONG ulCount; // number of waiting threads
  136. LIST_ENTRY leLink; // link in list of free locks
  137. };
  138. } DYNAMIC_READWRITE_LOCK, *PDYNAMIC_READWRITE_LOCK;
  139. //
  140. // type definition of DYNAMIC_LOCKS_STORE
  141. // store of free dynamic locks that can be allocated as required.
  142. //
  143. typedef struct _DYNAMIC_LOCKS_STORE {
  144. HANDLE hHeap;
  145. LOCKED_LIST llFreeLocksList;
  146. ULONG ulCountAllocated;
  147. ULONG ulCountFree;
  148. } DYNAMIC_LOCKS_STORE, *PDYNAMIC_LOCKS_STORE;
  149. // if more than DYNAMIC_LOCKS_HIGH_THRESHOLD locks are
  150. // allocated then any locks that are freed are destroyed
  151. #define DYNAMIC_LOCKS_HIGH_THRESHOLD 7
  152. #define DYNAMIC_LOCKS_STORE_INITIALIZED(pStore) \
  153. (LOCKED_LIST_INITIALIZED(&(pStore)->llFreeLocksList))
  154. typedef enum { READ_MODE, WRITE_MODE } LOCK_MODE;
  155. DWORD
  156. InitializeDynamicLocksStore (
  157. PDYNAMIC_LOCKS_STORE pStore,
  158. HANDLE hHeap
  159. );
  160. DWORD
  161. DeInitializeDynamicLocksStore (
  162. PDYNAMIC_LOCKS_STORE pStore
  163. );
  164. DWORD
  165. AcquireDynamicReadwriteLock (
  166. PDYNAMIC_READWRITE_LOCK *ppLock,
  167. LOCK_MODE lmMode,
  168. PDYNAMIC_LOCKS_STORE pStore
  169. );
  170. VOID
  171. ReleaseDynamicReadwriteLock (
  172. PDYNAMIC_READWRITE_LOCK *ppLock,
  173. LOCK_MODE lmMode,
  174. PDYNAMIC_LOCKS_STORE pStore
  175. );
  176. #endif // _SYNC_H_