sysuthrd.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. fwritelock:=RTLEventCreate;
  38. RTLeventSetEvent(fwritelock);
  39. fwaitingwriterlock:=RTLEventCreate;
  40. RTLEventResetEvent(fwaitingwriterlock);
  41. fwritelocked:=0;
  42. freadercount:=0;
  43. freaderqueue:=BasicEventCreate(nil,true,false,'');
  44. end;
  45. destructor TMultiReadExclusiveWriteSynchronizer.Destroy;
  46. begin
  47. System.InterlockedExchange(fwritelocked,0);
  48. RtlEventDestroy(fwritelock);
  49. RtlEventDestroy(fwaitingwriterlock);
  50. BasicEventDestroy(freaderqueue);
  51. end;
  52. function TMultiReadExclusiveWriteSynchronizer.Beginwrite : boolean;
  53. begin
  54. { wait for any other writers that may be in progress }
  55. RTLEventWaitFor(fwritelock);
  56. { it is possible that we earlier on missed waiting on the
  57. fwaitingwriterlock and that it's still set (must be done
  58. after aquiring the fwritelock, because otherwise one
  59. writer could reset the fwaitingwriterlock of another one
  60. that's about to wait on it) }
  61. BasicEventResetEvent(fwaitingwriterlock);
  62. { new readers have to block from now on; writers get priority to avoid
  63. writer starvation (since they have to compete with potentially many
  64. concurrent readers) }
  65. BasicEventResetEvent(freaderqueue);
  66. { for quick checking by candidate-readers }
  67. System.InterlockedExchange(fwritelocked,1);
  68. { wait until all readers are gone -- freadercount and fwritelocked are only
  69. accessed using atomic operations (that's why we use
  70. InterLockedExchangeAdd(x,0) below) -> always in-order. The writer always
  71. first sets fwritelocked and then checks freadercount, while the readers
  72. always first increase freadercount and then check fwritelocked }
  73. while (System.InterLockedExchangeAdd(freadercount,0)<>0) do
  74. RTLEventWaitFor(fwaitingwriterlock);
  75. { we have the writer lock, and all readers are gone }
  76. result:=true;
  77. end;
  78. procedure TMultiReadExclusiveWriteSynchronizer.Endwrite;
  79. begin
  80. { Finish all writes inside the section so that everything executing
  81. afterwards will certainly see these results }
  82. WriteBarrier;
  83. { signal potential readers that the coast is clear }
  84. System.InterlockedExchange(fwritelocked,0);
  85. { wake up waiting readers (if any); do not check first whether freadercount
  86. is <> 0, because the InterlockedDecrement in the while loop of BeginRead
  87. can have already occurred, so a single reader may be about to wait on
  88. freaderqueue even though freadercount=0. Setting an event multiple times
  89. is no problem. }
  90. BasicEventSetEvent(freaderqueue);
  91. { free the writer lock so another writer can become active }
  92. RTLeventSetEvent(fwritelock);
  93. end;
  94. procedure TMultiReadExclusiveWriteSynchronizer.Beginread;
  95. Const
  96. wrSignaled = 0;
  97. wrTimeout = 1;
  98. wrAbandoned= 2;
  99. wrError = 3;
  100. begin
  101. System.InterlockedIncrement(freadercount);
  102. { wait until there is no more writer }
  103. while System.InterLockedExchangeAdd(fwritelocked,0)<>0 do
  104. begin
  105. { there's a writer busy or wanting to start -> wait until it's
  106. finished; a writer may already be blocked in the mean time, so
  107. wake it up if we're the last to go to sleep }
  108. if System.InterlockedDecrement(freadercount)=0 then
  109. RTLEventSetEvent(fwaitingwriterlock);
  110. if (BasicEventWaitFor(high(cardinal),freaderqueue) in [wrAbandoned,wrError]) then
  111. raise Exception.create('BasicEventWaitFor failed in TMultiReadExclusiveWriteSynchronizer.Beginread');
  112. { and try again: first increase freadercount, only then check
  113. fwritelocked }
  114. System.InterlockedIncrement(freadercount);
  115. end;
  116. end;
  117. procedure TMultiReadExclusiveWriteSynchronizer.Endread;
  118. begin
  119. { Make sure that all read operations have finished, so that none of those
  120. can still be executed after a writer starts working and changes some
  121. things }
  122. ReadBarrier;
  123. { If no more readers, wake writer in the ready-queue if any. Since a writer
  124. always first atomically sets the fwritelocked and then atomically checks
  125. the freadercount, first modifying freadercount and then checking fwritelock
  126. ensures that we cannot miss one of the events regardless of execution
  127. order. }
  128. if (System.InterlockedDecrement(freadercount)=0) and
  129. (System.InterLockedExchangeAdd(fwritelocked,0)<>0) then
  130. RTLEventSetEvent(fwaitingwriterlock);
  131. end;