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.

167 lines
4.9 KiB

  1. //============================================================================
  2. // Copyright (c) 1995, Microsoft Corporation
  3. //
  4. // File: sync.h
  5. //
  6. // History:
  7. // V Raman July-11-1997 Created.
  8. //
  9. // Lock structures and synchronization routines.
  10. // Lock structures borrowed from RIPv2 by Abolade Gbadegesin.
  11. // Dynamic locking idea borrowed from RTM by Vadim Eydelman.
  12. //============================================================================
  13. #ifndef _SYNC_H_
  14. #define _SYNC_H_
  15. //----------------------------------------------------------------------------
  16. //
  17. // Read/Write locks for synchronization of access to various lists
  18. //
  19. // Given the large number of lists (including the various hash buckets) and
  20. // the relatively small number of clients that concurrently invoke MGM
  21. // API, statically allocating a lock structure for each list was
  22. // considered expensive. Locks are created as needed and stored in a
  23. // stack structure (implemented as a singly linked list) after use.
  24. //
  25. // Subsequent requests for locks are all satisfied by reusing the locks
  26. // stored on the stack. Only if the stack is empty ie. all locks in the
  27. // stack are in use are new locks created.
  28. //
  29. // This ensures that the most number of locks created is no larger than
  30. // the maximum number of concurrent clients at any time.
  31. //
  32. // csReadWriteBlock - Critical section guarding access to
  33. // lReaderCount.
  34. // lReaderCount - Count of readers currently using the
  35. // shared resource.
  36. // hReaderDoneEvent - Event on which writers block when readers
  37. // are currently using the lists.
  38. // lUseCount - Count of readers + writers. Used to
  39. // determine if there are any threads waiting
  40. // on the lock. If there are none the lock
  41. // can be released to te stack of locks.
  42. //----------------------------------------------------------------------------
  43. typedef struct _MGM_READ_WRITE_LOCK
  44. {
  45. SINGLE_LIST_ENTRY sleLockList;
  46. CRITICAL_SECTION csReaderWriterBlock;
  47. LONG lReaderCount;
  48. HANDLE hReaderDoneEvent;
  49. LONG lUseCount;
  50. } MGM_READ_WRITE_LOCK, *PMGM_READ_WRITE_LOCK;
  51. //----------------------------------------------------------------------------
  52. //
  53. // Read/write locks are created dynamically and stored for
  54. // reuse in a stack struture.
  55. //
  56. //----------------------------------------------------------------------------
  57. typedef struct _LOCK_LIST
  58. {
  59. SINGLE_LIST_ENTRY sleHead;
  60. CRITICAL_SECTION csListLock;
  61. BOOL bInit;
  62. } LOCK_LIST, *PLOCK_LIST;
  63. //----------------------------------------------------------------------------
  64. // Standard locked list structure.
  65. //----------------------------------------------------------------------------
  66. typedef struct _MGM_LOCKED_LIST
  67. {
  68. LIST_ENTRY leHead;
  69. PMGM_READ_WRITE_LOCK pmrwlLock;
  70. DWORD dwCreated;
  71. } MGM_LOCKED_LIST, *PMGM_LOCKED_LIST;
  72. #define CREATE_LOCKED_LIST( p ) \
  73. { \
  74. (p)-> pmrwlLock = NULL; \
  75. InitializeListHead( &(p)->leHead ); \
  76. (p)-> dwCreated = 0x12345678; \
  77. }
  78. #define DELETE_LOCKED_LIST( p ) \
  79. { \
  80. (p)-> pmrwlLock = NULL; \
  81. if ( !IsListEmpty( &(p)-> leHead ) ) \
  82. TRACE0( ANY, "Locked list being deleted is not empty" ); \
  83. InitializeListHead( &(p)-> leHead ); \
  84. (p)-> dwCreated = 0; \
  85. }
  86. #define LOCKED_LIST_HEAD( p ) &(p)-> leHead
  87. //
  88. // Routines to create/delete locks
  89. //
  90. DWORD
  91. CreateReadWriteLock(
  92. IN OUT PMGM_READ_WRITE_LOCK * ppmrwl
  93. );
  94. VOID
  95. DeleteReadWriteLock(
  96. IN OUT PMGM_READ_WRITE_LOCK pmrwl
  97. );
  98. VOID
  99. DeleteLockList(
  100. );
  101. //
  102. // Routines to acquire and release locks.
  103. //
  104. DWORD
  105. AcquireReadLock(
  106. IN OUT PMGM_READ_WRITE_LOCK * ppmrwl
  107. );
  108. VOID
  109. ReleaseReadLock(
  110. IN OUT PMGM_READ_WRITE_LOCK * ppmrwl
  111. );
  112. DWORD
  113. AcquireWriteLock(
  114. IN OUT PMGM_READ_WRITE_LOCK * ppmrwl
  115. );
  116. VOID
  117. ReleaseWriteLock(
  118. IN OUT PMGM_READ_WRITE_LOCK * ppmrwl
  119. );
  120. #endif // _SYNC_H_