Source code of Windows XP (NT5)
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.

264 lines
8.1 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. elsync.h
  5. Abstract:
  6. This module contains the declarations for providing synchronization
  7. between multiple threads
  8. Revision History:
  9. mohitt, sachins, Apr 23 2000, Created
  10. --*/
  11. #ifndef _EAPOL_SYNC_H_
  12. #define _EAPOL_SYNC_H_
  13. //
  14. // Structure: READ_WRITE_LOCK
  15. //
  16. typedef struct _READ_WRITE_LOCK
  17. {
  18. CHAR RWL_Name[4]; // overhead! but useful :)
  19. CRITICAL_SECTION RWL_ReadWriteBlock;
  20. LONG RWL_ReaderCount;
  21. HANDLE RWL_ReaderDoneEvent;
  22. } READ_WRITE_LOCK, *PREAD_WRITE_LOCK;
  23. //
  24. // FUNCTION DECLARATIONS
  25. //
  26. DWORD
  27. CreateReadWriteLock(
  28. OUT PREAD_WRITE_LOCK pRWL,
  29. IN PCHAR szName);
  30. VOID
  31. DeleteReadWriteLock(
  32. IN PREAD_WRITE_LOCK pRWL);
  33. VOID
  34. AcquireReadLock(
  35. IN PREAD_WRITE_LOCK pRWL);
  36. VOID
  37. ReleaseReadLock(
  38. IN PREAD_WRITE_LOCK pRWL);
  39. VOID
  40. AcquireWriteLock(
  41. IN PREAD_WRITE_LOCK pRWL);
  42. VOID
  43. ReleaseWriteLock(
  44. IN PREAD_WRITE_LOCK pRWL);
  45. #define CREATE_READ_WRITE_LOCK(pRWL, szName) \
  46. CreateReadWriteLock(pRWL, szName)
  47. #define DELETE_READ_WRITE_LOCK(pRWL) \
  48. DeleteReadWriteLock(pRWL)
  49. #define READ_WRITE_LOCK_CREATED(pRWL) \
  50. ((pRWL)->RWL_ReaderDoneEvent != NULL)
  51. // MACRO functions for manipulating a read-write lock
  52. #define ACQUIRE_READ_LOCK(pRWL) \
  53. { \
  54. TRACE1(LOCK, "+R: %.3s", (pRWL)->RWL_Name); \
  55. AcquireReadLock(pRWL); \
  56. TRACE0(LOCK, "Done."); \
  57. }
  58. #define RELEASE_READ_LOCK(pRWL) \
  59. { \
  60. TRACE1(LOCK, "-R: %.3s", (pRWL)->RWL_Name); \
  61. ReleaseReadLock(pRWL); \
  62. TRACE0(LOCK, "Done."); \
  63. }
  64. #define ACQUIRE_WRITE_LOCK(pRWL) \
  65. { \
  66. TRACE1(LOCK, "+W: %.3s", (pRWL)->RWL_Name); \
  67. AcquireWriteLock(pRWL); \
  68. TRACE0(LOCK, "Done."); \
  69. }
  70. #define RELEASE_WRITE_LOCK(pRWL) \
  71. { \
  72. TRACE1(LOCK, "-W: %.3s", (pRWL)->RWL_Name); \
  73. ReleaseWriteLock(pRWL); \
  74. TRACE0(LOCK, "Done."); \
  75. }
  76. //
  77. // MACRO functions for manipulating a dynamic read-write lock
  78. //
  79. #define ACQUIRE_READ_DLOCK(ppLock) \
  80. { \
  81. TRACE1(LOCK, "+R: %s", LOCKSTORE->szName); \
  82. while( \
  83. AcquireDynamicReadwriteLock(ppLock, READ_MODE, LOCKSTORE) \
  84. != NO_ERROR) { Sleep(SECTOMILLISEC(DELTA)); } \
  85. TRACE0(LOCK, "Done."); \
  86. }
  87. #define RELEASE_READ_DLOCK(ppLock) \
  88. { \
  89. TRACE1(LOCK, "-R: %s", LOCKSTORE->szName); \
  90. ReleaseDynamicReadwriteLock(ppLock, READ_MODE, LOCKSTORE); \
  91. TRACE0(LOCK, "Done."); \
  92. }
  93. #define ACQUIRE_WRITE_DLOCK(ppLock) \
  94. { \
  95. TRACE1(LOCK, "+W: %s", LOCKSTORE->szName); \
  96. while( \
  97. AcquireDynamicReadwriteLock(ppLock, WRITE_MODE, LOCKSTORE) \
  98. != NO_ERROR) { Sleep(SECTOMILLISEC(DELTA)); } \
  99. TRACE0(LOCK, "Done."); \
  100. }
  101. #define RELEASE_WRITE_DLOCK(ppLock) \
  102. { \
  103. TRACE1(LOCK, "-W: %s", LOCKSTORE->szName); \
  104. ReleaseDynamicReadwriteLock(ppLock, WRITE_MODE, LOCKSTORE); \
  105. TRACE0(LOCK, "Done."); \
  106. }
  107. //
  108. // STRUCTURE: LOCKED_LIST
  109. // type definition for generic locked list
  110. // access is sychronized with a critical section
  111. //
  112. typedef struct _LOCKED_LIST
  113. {
  114. CHAR name[4];
  115. CRITICAL_SECTION lock;
  116. LIST_ENTRY head;
  117. DWORD created;
  118. } LOCKED_LIST, *PLOCKED_LIST;
  119. #define INITIALIZE_LOCKED_LIST(pLL, szName) \
  120. { \
  121. do \
  122. { \
  123. sprintf((pLL)->name, "%.3s", szName); \
  124. __try { \
  125. InitializeCriticalSection(&((pLL)->lock)); \
  126. } \
  127. __except (EXCEPTION_EXECUTE_HANDLER) { \
  128. break; \
  129. } \
  130. InitializeListHead(&((pLL)->head)); \
  131. (pLL)->created = 0x12345678; \
  132. } while (FALSE); \
  133. }
  134. #define LOCKED_LIST_INITIALIZED(pLL) \
  135. ((pLL)->created == 0x12345678)
  136. #define DELETE_LOCKED_LIST(pLL, FreeFunction) \
  137. { \
  138. (pLL)->created = 0; \
  139. FreeList(&((pLL)->head), FreeFunction); \
  140. DeleteCriticalSection(&(pLL)->lock); \
  141. }
  142. #define AcquireListLock(pLL) EnterCriticalSection(&(pLL)->lock)
  143. #define ReleaseListLock(pLL) LeaveCriticalSection(&(pLL)->lock)
  144. #define LOCKED_QUEUE LOCKED_LIST
  145. #define PLOCKED_QUEUE PLOCKED_LIST
  146. #define INITIALIZE_LOCKED_QUEUE(pLQ, szName) \
  147. INITIALIZE_LOCKED_LIST(pLQ, szName)
  148. #define LOCKED_QUEUE_INITIALIZED(pLQ) \
  149. LOCKED_LIST_INITIALIZED(pLQ)
  150. #define DELETE_LOCKED_QUEUE(pLQ, FreeFunction) \
  151. DELETE_LOCKED_LIST(pLQ, FreeFunction)
  152. //
  153. // STRUCTURE: DYNAMIC_READWRITE_LOCK
  154. //
  155. typedef struct _DYNAMIC_READWRITE_LOCK
  156. {
  157. READ_WRITE_LOCK rwlLock;
  158. union
  159. {
  160. ULONG ulCount; // number of waiting threads
  161. LIST_ENTRY leLink; // link in list of free locks
  162. };
  163. } DYNAMIC_READWRITE_LOCK, *PDYNAMIC_READWRITE_LOCK;
  164. //
  165. // STRUCTURE: DYNAMIC_LOCKS_STORE
  166. // store of free dynamic locks that can be allocated as required.
  167. //
  168. typedef struct _DYNAMIC_LOCKS_STORE
  169. {
  170. CHAR szName[4];
  171. HANDLE hHeap;
  172. LOCKED_LIST llFreeLocksList;
  173. ULONG ulCountAllocated;
  174. ULONG ulCountFree;
  175. } DYNAMIC_LOCKS_STORE, *PDYNAMIC_LOCKS_STORE;
  176. // if more than DYNAMIC_LOCKS_HIGH_THRESHOLD locks are
  177. // allocated then any locks that are freed are destroyed
  178. #define DYNAMIC_LOCKS_HIGH_THRESHOLD 7
  179. #define DYNAMIC_LOCKS_STORE_INITIALIZED(pStore) \
  180. (LOCKED_LIST_INITIALIZED(&(pStore)->llFreeLocksList))
  181. typedef enum { READ_MODE, WRITE_MODE } LOCK_MODE;
  182. DWORD
  183. InitializeDynamicLocksStore (
  184. PDYNAMIC_LOCKS_STORE pStore,
  185. HANDLE hHeap,
  186. PCHAR szName
  187. );
  188. DWORD
  189. DeInitializeDynamicLocksStore (
  190. PDYNAMIC_LOCKS_STORE pStore
  191. );
  192. DWORD
  193. AcquireDynamicReadwriteLock (
  194. PDYNAMIC_READWRITE_LOCK *ppLock,
  195. LOCK_MODE lmMode,
  196. PDYNAMIC_LOCKS_STORE pStore
  197. );
  198. VOID
  199. ReleaseDynamicReadwriteLock (
  200. PDYNAMIC_READWRITE_LOCK *ppLock,
  201. LOCK_MODE lmMode,
  202. PDYNAMIC_LOCKS_STORE pStore
  203. );
  204. #endif // _EAPOL_SYNC_H