Note that there are some explanatory texts on larger screens.

plurals
  1. POconfused about spin lock
    text
    copied!<p>I read spin lock code <a href="http://www.codeproject.com/KB/threads/spinlocks.aspx" rel="nofollow noreferrer">from here</a>, especially this part</p> <pre><code>inline void Enter(void) { int prev_s; do { prev_s = TestAndSet(&amp;m_s, 0); if (m_s == 0 &amp;&amp; prev_s == 1) { break; } // reluinquish current timeslice (can only // be used when OS available and // we do NOT want to 'spin') // HWSleep(0); } while (true); } </code></pre> <p>Why do we need to test two conditions m_s == 0 &amp;&amp; prev_s == 1? I think just test prev_s == 1 should be enough. Any ideas?</p> <p>EDIT: version 2. should we fix in this way if there is a bug?</p> <pre><code>inline void Enter(void) { do { if (m_s == 0 &amp;&amp; 1 == TestAndSet(&amp;m_s, 0)) { break; } // reluinquish current timeslice (can only // be used when OS available and // we do NOT want to 'spin') // HWSleep(0); } while (true); } </code></pre> <p>EDIT: version 3. I think version 3 from functional level is correct, but performance is not good enough since each time we need to write, no read test ahead. Is my understanding correct?</p> <pre><code>inline void Enter(void) { do { if (1 == TestAndSet(&amp;m_s, 0)) { break; } // reluinquish current timeslice (can only // be used when OS available and // we do NOT want to 'spin') // HWSleep(0); } while (true); } </code></pre> <p>@dragonfly, here is my bug fix version 4 (fixed a bug in version 2 as you pointed out), could you review whether it is correct please? Thanks!</p> <p>EDIT: version 4.</p> <pre><code>inline void Enter(void) { do { if (m_s == 1 &amp;&amp; 1 == TestAndSet(&amp;m_s, 0)) { break; } // reluinquish current timeslice (can only // be used when OS available and // we do NOT want to 'spin') // HWSleep(0); } while (true); } </code></pre>
 

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