Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know of any lock-free (or micro-locking as in your example above) MREW approach that could be implemented on Intel86 code.</p> <p>For small (fast-expiring) locks a spinning approach from the <a href="http://code.google.com/p/omnithreadlibrary/source/browse/branches/fast-notifications/OtlSync.pas" rel="noreferrer">OmniThreadLibrary</a> works fine:</p> <pre><code>type TOmniMREW = record strict private omrewReference: integer; //Reference.Bit0 is 'writing in progress' flag public procedure EnterReadLock; inline; procedure EnterWriteLock; inline; procedure ExitReadLock; inline; procedure ExitWriteLock; inline; end; { TOmniMREW } procedure TOmniMREW.EnterReadLock; var currentReference: integer; begin //Wait on writer to reset write flag so Reference.Bit0 must be 0 than increase Reference repeat currentReference := omrewReference AND NOT 1; until currentReference = InterlockedCompareExchange(omrewReference, currentReference + 2, currentReference); end; { TOmniMREW.EnterReadLock } procedure TOmniMREW.EnterWriteLock; var currentReference: integer; begin //Wait on writer to reset write flag so omrewReference.Bit0 must be 0 then set omrewReference.Bit0 repeat currentReference := omrewReference AND NOT 1; until currentReference = InterlockedCompareExchange(omrewReference, currentReference + 1, currentReference); //Now wait on all readers repeat until omrewReference = 1; end; { TOmniMREW.EnterWriteLock } procedure TOmniMREW.ExitReadLock; begin //Decrease omrewReference InterlockedExchangeAdd(omrewReference, -2); end; { TOmniMREW.ExitReadLock } procedure TOmniMREW.ExitWriteLock; begin omrewReference := 0; end; { TOmniMREW.ExitWriteLock } </code></pre> <p>I just noticed a possible alignment issue here - the code should check that omrewReference is 4-aligned. Will notify the author.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload