sysuthrd.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2005,2009 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. constructor TSimpleRWSync.Create;
  11. begin
  12. System.InitCriticalSection(Crit);
  13. end;
  14. destructor TSimpleRWSync.Destroy;
  15. begin
  16. System.DoneCriticalSection(Crit);
  17. end;
  18. function TSimpleRWSync.Beginwrite : boolean;
  19. begin
  20. System.EnterCriticalSection(Crit);
  21. result:=true;
  22. end;
  23. procedure TSimpleRWSync.Endwrite;
  24. begin
  25. System.LeaveCriticalSection(Crit);
  26. end;
  27. procedure TSimpleRWSync.Beginread;
  28. begin
  29. System.EnterCriticalSection(Crit);
  30. end;
  31. procedure TSimpleRWSync.Endread;
  32. begin
  33. System.LeaveCriticalSection(Crit);
  34. end;
  35. constructor TMultiReadExclusiveWriteSynchronizer.Create;
  36. begin
  37. System.InitCriticalSection(fwritelock);
  38. fwaitingwriterlock:=RTLEventCreate;
  39. RTLEventResetEvent(fwaitingwriterlock);
  40. { make sure all threads see the initialisation of fwritelock and
  41. freadercount (we only use atomic operation further on as well) }
  42. System.InterlockedExchange(freadercount,0);
  43. System.InterlockedExchange(fwritelocked,0);
  44. freaderqueue:=BasicEventCreate(nil,true,false,'');
  45. end;
  46. destructor TMultiReadExclusiveWriteSynchronizer.Destroy;
  47. begin
  48. System.InterlockedExchange(fwritelocked,0);
  49. System.DoneCriticalSection(fwritelock);
  50. RtlEventDestroy(fwaitingwriterlock);
  51. BasicEventDestroy(freaderqueue);
  52. end;
  53. function TMultiReadExclusiveWriteSynchronizer.Beginwrite : boolean;
  54. begin
  55. { wait for any other writers that may be in progress }
  56. System.EnterCriticalSection(fwritelock);
  57. { it is possible that earlier on we missed waiting on the
  58. fwaitingwriterlock and that it's still set (must be done
  59. after acquiring the fwritelock, because otherwise one
  60. writer could reset the fwaitingwriterlock of another one
  61. that's about to wait on it) }
  62. RTLeventResetEvent(fwaitingwriterlock);
  63. { new readers have to block from now on; writers get priority to avoid
  64. writer starvation (since they have to compete with potentially many
  65. concurrent readers and other writers) }
  66. BasicEventResetEvent(freaderqueue);
  67. { for quick checking by candidate-readers -- use interlockedincrement/
  68. decrement instead of setting 1/0, because a single thread can
  69. recursively acquire the write lock multiple times }
  70. System.InterlockedIncrement(fwritelocked);
  71. { wait until all readers are gone -- freadercount and fwritelocked are only
  72. accessed using atomic operations (that's why we use
  73. InterLockedExchangeAdd(x,0) below) -> always in-order. The writer always
  74. first sets fwritelocked and then checks freadercount, while the readers
  75. always first increase freadercount and then check fwritelocked }
  76. while (System.InterLockedExchangeAdd(freadercount,0)<>0) do
  77. RTLEventWaitFor(fwaitingwriterlock);
  78. { Make sure that out-of-order execution cannot already perform reads
  79. inside the critical section before the lock has been acquired }
  80. ReadBarrier;
  81. { we have the writer lock, and all readers are gone }
  82. result:=true;
  83. end;
  84. procedure TMultiReadExclusiveWriteSynchronizer.Endwrite;
  85. begin
  86. { Finish all writes inside the section so that everything executing
  87. afterwards will certainly see these results }
  88. WriteBarrier;
  89. { signal potential readers that the coast is clear if all recursive
  90. write locks have been freed }
  91. if System.InterlockedDecrement(fwritelocked)=0 then
  92. begin
  93. { wake up waiting readers (if any); do not check first whether freadercount
  94. is <> 0, because the InterlockedDecrement in the while loop of BeginRead
  95. can have already occurred, so a single reader may be about to wait on
  96. freaderqueue even though freadercount=0. Setting an event multiple times
  97. is no problem. }
  98. BasicEventSetEvent(freaderqueue);
  99. end;
  100. { free the writer lock so another writer can become active }
  101. System.LeaveCriticalSection(fwritelock);
  102. end;
  103. procedure TMultiReadExclusiveWriteSynchronizer.Beginread;
  104. Const
  105. wrSignaled = 0;
  106. wrTimeout = 1;
  107. wrAbandoned= 2;
  108. wrError = 3;
  109. begin
  110. System.InterlockedIncrement(freadercount);
  111. { wait until there is no more writer }
  112. while System.InterLockedExchangeAdd(fwritelocked,0)<>0 do
  113. begin
  114. { there's a writer busy or wanting to start -> wait until it's
  115. finished; a writer may already be blocked in the mean time, so
  116. wake it up if we're the last to go to sleep }
  117. if System.InterlockedDecrement(freadercount)=0 then
  118. RTLEventSetEvent(fwaitingwriterlock);
  119. if (BasicEventWaitFor(high(cardinal),freaderqueue) in [wrAbandoned,wrError]) then
  120. raise Exception.create('BasicEventWaitFor failed in TMultiReadExclusiveWriteSynchronizer.Beginread');
  121. { and try again: first increase freadercount, only then check
  122. fwritelocked }
  123. System.InterlockedIncrement(freadercount);
  124. end;
  125. { Make sure that out-of-order execution cannot perform reads
  126. inside the critical section before the lock has been acquired }
  127. ReadBarrier;
  128. end;
  129. procedure TMultiReadExclusiveWriteSynchronizer.Endread;
  130. begin
  131. { If no more readers, wake writer in the ready-queue if any. Since a writer
  132. always first atomically sets fwritelocked and then atomically checks the
  133. freadercount, first modifying freadercount and then checking fwritelock
  134. ensures that we cannot miss one of the events regardless of execution
  135. order. }
  136. if (System.InterlockedDecrement(freadercount)=0) and
  137. (System.InterLockedExchangeAdd(fwritelocked,0)<>0) then
  138. RTLEventSetEvent(fwaitingwriterlock);
  139. end;