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.

156 lines
5.4 KiB

  1. //============================================================================
  2. // Copyright (c) 1995, Microsoft Corporation
  3. //
  4. // File: sync.h
  5. //
  6. // History:
  7. // Abolade Gbadegesin Aug-8-1995 Created.
  8. //
  9. // Contains structures and macros used to implement synchronization.
  10. //============================================================================
  11. #ifndef _SYNC_H_
  12. #define _SYNC_H_
  13. //
  14. // type definition for multiple-reader/single-writer lock
  15. // Note: there is a similar facility provided by nturtl.h through the
  16. // structure RTL_RESOURCE and several functions. However, that
  17. // implementation has the potential for starving a thread trying to acquire
  18. // write accesss, if there are a large number of threads interested in
  19. // acquiring read access. Such a scenario is avoided in the implementation
  20. // given in this header. However, a mapping is also given to the
  21. // RTL_RESOURCE functionality, so that the protocol can be compiled to use
  22. // either form
  23. //
  24. #ifdef USE_RWL
  25. //
  26. // use IPRIP's definitions
  27. //
  28. typedef struct _READ_WRITE_LOCK {
  29. CRITICAL_SECTION RWL_ReadWriteBlock;
  30. LONG RWL_ReaderCount;
  31. HANDLE RWL_ReaderDoneEvent;
  32. } READ_WRITE_LOCK, *PREAD_WRITE_LOCK;
  33. DWORD CreateReadWriteLock(PREAD_WRITE_LOCK pRWL);
  34. VOID DeleteReadWriteLock(PREAD_WRITE_LOCK pRWL);
  35. VOID AcquireReadLock(PREAD_WRITE_LOCK pRWL);
  36. VOID ReleaseReadLock(PREAD_WRITE_LOCK pRWL);
  37. VOID AcquireWriteLock(PREAD_WRITE_LOCK pRWL);
  38. VOID ReleaseWriteLock(PREAD_WRITE_LOCK pRWL);
  39. //
  40. // macro functions for manipulating a read-write lock
  41. //
  42. #define CREATE_READ_WRITE_LOCK(pRWL) \
  43. CreateReadWriteLock(pRWL)
  44. #define DELETE_READ_WRITE_LOCK(pRWL) \
  45. DeleteReadWriteLock(pRWL)
  46. #define READ_WRITE_LOCK_CREATED(pRWL) \
  47. ((pRWL)->RWL_ReaderDoneEvent != NULL)
  48. #define ACQUIRE_READ_LOCK(pRWL) \
  49. AcquireReadLock(pRWL)
  50. #define RELEASE_READ_LOCK(pRWL) \
  51. ReleaseReadLock(pRWL)
  52. #define ACQUIRE_WRITE_LOCK(pRWL) \
  53. AcquireWriteLock(pRWL)
  54. #define RELEASE_WRITE_LOCK(pRWL) \
  55. ReleaseWriteLock(pRWL)
  56. #define READ_LOCK_TO_WRITE_LOCK(pRWL) \
  57. (ReleaseReadLock(pRWL), AcquireWriteLock(pRWL))
  58. #define WRITE_LOCK_TO_READ_LOCK(pRWL) \
  59. (ReleaseWriteLock(pRWL), AcquireReadLock(pRWL))
  60. #else // i.e. !USE_RWL
  61. //
  62. // use the RTL_RESOURCE mechanism
  63. //
  64. typedef RTL_RESOURCE READ_WRITE_LOCK, *PREAD_WRITE_LOCK;
  65. #define CREATE_READ_WRITE_LOCK(pRWL) \
  66. RtlInitializeResource((pRWL))
  67. #define DELETE_READ_WRITE_LOCK(pRWL) \
  68. RtlDeleteResource((pRWL))
  69. #define READ_WRITE_LOCK_CREATED(pRWL) (TRUE)
  70. #define ACQUIRE_READ_LOCK(pRWL) \
  71. RtlAcquireResourceShared((pRWL),TRUE)
  72. #define RELEASE_READ_LOCK(pRWL) \
  73. RtlReleaseResource((pRWL))
  74. #define ACQUIRE_WRITE_LOCK(pRWL) \
  75. RtlAcquireResourceExclusive((pRWL),TRUE)
  76. #define RELEASE_WRITE_LOCK(pRWL) \
  77. RtlReleaseResource((pRWL))
  78. #define READ_LOCK_TO_WRITE_LOCK(pRWL) \
  79. RtlConvertSharedToExclusive((pRWL))
  80. #define WRITE_LOCK_TO_READ_LOCK(pRWL) \
  81. RtlConvertExclusiveToShared((pRWL))
  82. #endif // USE_RWL
  83. //
  84. // type definition for generic locked list
  85. // access is sychronized with a critical section
  86. //
  87. typedef struct _LOCKED_LIST {
  88. LIST_ENTRY LL_Head;
  89. CRITICAL_SECTION LL_Lock;
  90. DWORD LL_Created;
  91. } LOCKED_LIST, *PLOCKED_LIST;
  92. //
  93. // macro functions for manipulating the locked list
  94. //
  95. #define CREATE_LOCKED_LIST(pLL) \
  96. InitializeListHead(&(pLL)->LL_Head); \
  97. InitializeCriticalSection(&(pLL)->LL_Lock); \
  98. (pLL)->LL_Created = 0x12345678
  99. #define LOCKED_LIST_CREATED(pLL) \
  100. ((pLL)->LL_Created == 0x12345678)
  101. #define DELETE_LOCKED_LIST(pLL,type,field) { \
  102. PLIST_ENTRY _ple; \
  103. (pLL)->LL_Created = 0; \
  104. DeleteCriticalSection(&(pLL)->LL_Lock); \
  105. while (!IsListEmpty(&(pLL)->LL_Head)) { \
  106. _ple = RemoveHeadList(&(pLL)->LL_Head); \
  107. FREE(CONTAINING_RECORD(_ple,type,field)); \
  108. } \
  109. }
  110. #define ACQUIRE_LIST_LOCK(pLL) \
  111. EnterCriticalSection(&(pLL)->LL_Lock)
  112. #define RELEASE_LIST_LOCK(pLL) \
  113. LeaveCriticalSection(&(pLL)->LL_Lock)
  114. #endif // _SYNC_H_