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.

256 lines
7.6 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. AcquireReadLock(pRWL); \
  55. }
  56. #define RELEASE_READ_LOCK(pRWL) \
  57. { \
  58. ReleaseReadLock(pRWL); \
  59. }
  60. #define ACQUIRE_WRITE_LOCK(pRWL) \
  61. { \
  62. AcquireWriteLock(pRWL); \
  63. }
  64. #define RELEASE_WRITE_LOCK(pRWL) \
  65. { \
  66. ReleaseWriteLock(pRWL); \
  67. }
  68. //
  69. // MACRO functions for manipulating a dynamic read-write lock
  70. //
  71. #define ACQUIRE_READ_DLOCK(ppLock) \
  72. { \
  73. TRACE1(LOCK, "+R: %s", LOCKSTORE->szName); \
  74. while( \
  75. AcquireDynamicReadwriteLock(ppLock, READ_MODE, LOCKSTORE) \
  76. != NO_ERROR) { Sleep(SECTOMILLISEC(DELTA)); } \
  77. TRACE0(LOCK, "Done."); \
  78. }
  79. #define RELEASE_READ_DLOCK(ppLock) \
  80. { \
  81. TRACE1(LOCK, "-R: %s", LOCKSTORE->szName); \
  82. ReleaseDynamicReadwriteLock(ppLock, READ_MODE, LOCKSTORE); \
  83. TRACE0(LOCK, "Done."); \
  84. }
  85. #define ACQUIRE_WRITE_DLOCK(ppLock) \
  86. { \
  87. TRACE1(LOCK, "+W: %s", LOCKSTORE->szName); \
  88. while( \
  89. AcquireDynamicReadwriteLock(ppLock, WRITE_MODE, LOCKSTORE) \
  90. != NO_ERROR) { Sleep(SECTOMILLISEC(DELTA)); } \
  91. TRACE0(LOCK, "Done."); \
  92. }
  93. #define RELEASE_WRITE_DLOCK(ppLock) \
  94. { \
  95. TRACE1(LOCK, "-W: %s", LOCKSTORE->szName); \
  96. ReleaseDynamicReadwriteLock(ppLock, WRITE_MODE, LOCKSTORE); \
  97. TRACE0(LOCK, "Done."); \
  98. }
  99. //
  100. // STRUCTURE: LOCKED_LIST
  101. // type definition for generic locked list
  102. // access is sychronized with a critical section
  103. //
  104. typedef struct _LOCKED_LIST
  105. {
  106. CHAR name[4];
  107. CRITICAL_SECTION lock;
  108. LIST_ENTRY head;
  109. DWORD created;
  110. } LOCKED_LIST, *PLOCKED_LIST;
  111. #define INITIALIZE_LOCKED_LIST(pLL, szName) \
  112. { \
  113. do \
  114. { \
  115. sprintf((pLL)->name, "%.3s", szName); \
  116. __try { \
  117. InitializeCriticalSection(&((pLL)->lock)); \
  118. } \
  119. __except (EXCEPTION_EXECUTE_HANDLER) { \
  120. break; \
  121. } \
  122. InitializeListHead(&((pLL)->head)); \
  123. (pLL)->created = 0x12345678; \
  124. } while (FALSE); \
  125. }
  126. #define LOCKED_LIST_INITIALIZED(pLL) \
  127. ((pLL)->created == 0x12345678)
  128. #define DELETE_LOCKED_LIST(pLL, FreeFunction) \
  129. { \
  130. (pLL)->created = 0; \
  131. FreeList(&((pLL)->head), FreeFunction); \
  132. DeleteCriticalSection(&(pLL)->lock); \
  133. }
  134. #define AcquireListLock(pLL) EnterCriticalSection(&(pLL)->lock)
  135. #define ReleaseListLock(pLL) LeaveCriticalSection(&(pLL)->lock)
  136. #define LOCKED_QUEUE LOCKED_LIST
  137. #define PLOCKED_QUEUE PLOCKED_LIST
  138. #define INITIALIZE_LOCKED_QUEUE(pLQ, szName) \
  139. INITIALIZE_LOCKED_LIST(pLQ, szName)
  140. #define LOCKED_QUEUE_INITIALIZED(pLQ) \
  141. LOCKED_LIST_INITIALIZED(pLQ)
  142. #define DELETE_LOCKED_QUEUE(pLQ, FreeFunction) \
  143. DELETE_LOCKED_LIST(pLQ, FreeFunction)
  144. //
  145. // STRUCTURE: DYNAMIC_READWRITE_LOCK
  146. //
  147. typedef struct _DYNAMIC_READWRITE_LOCK
  148. {
  149. READ_WRITE_LOCK rwlLock;
  150. union
  151. {
  152. ULONG ulCount; // number of waiting threads
  153. LIST_ENTRY leLink; // link in list of free locks
  154. };
  155. } DYNAMIC_READWRITE_LOCK, *PDYNAMIC_READWRITE_LOCK;
  156. //
  157. // STRUCTURE: DYNAMIC_LOCKS_STORE
  158. // store of free dynamic locks that can be allocated as required.
  159. //
  160. typedef struct _DYNAMIC_LOCKS_STORE
  161. {
  162. CHAR szName[4];
  163. HANDLE hHeap;
  164. LOCKED_LIST llFreeLocksList;
  165. ULONG ulCountAllocated;
  166. ULONG ulCountFree;
  167. } DYNAMIC_LOCKS_STORE, *PDYNAMIC_LOCKS_STORE;
  168. // if more than DYNAMIC_LOCKS_HIGH_THRESHOLD locks are
  169. // allocated then any locks that are freed are destroyed
  170. #define DYNAMIC_LOCKS_HIGH_THRESHOLD 7
  171. #define DYNAMIC_LOCKS_STORE_INITIALIZED(pStore) \
  172. (LOCKED_LIST_INITIALIZED(&(pStore)->llFreeLocksList))
  173. typedef enum { READ_MODE, WRITE_MODE } LOCK_MODE;
  174. DWORD
  175. InitializeDynamicLocksStore (
  176. PDYNAMIC_LOCKS_STORE pStore,
  177. HANDLE hHeap,
  178. PCHAR szName
  179. );
  180. DWORD
  181. DeInitializeDynamicLocksStore (
  182. PDYNAMIC_LOCKS_STORE pStore
  183. );
  184. DWORD
  185. AcquireDynamicReadwriteLock (
  186. PDYNAMIC_READWRITE_LOCK *ppLock,
  187. LOCK_MODE lmMode,
  188. PDYNAMIC_LOCKS_STORE pStore
  189. );
  190. VOID
  191. ReleaseDynamicReadwriteLock (
  192. PDYNAMIC_READWRITE_LOCK *ppLock,
  193. LOCK_MODE lmMode,
  194. PDYNAMIC_LOCKS_STORE pStore
  195. );
  196. #endif // _EAPOL_SYNC_H