<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: SpinWaitLock in Intel® Moderncode for Parallel Architectures</title>
    <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SpinWaitLock/m-p/860844#M2318</link>
    <description>You need #StoreLoad style memory fence in critical store-load sequence. You have basically an instance of Dekker/Peterson algorithm in your slow-path:&lt;BR /&gt;&lt;A href="http://en.wikipedia.org/wiki/Peterson%27s_algorithm"&gt;http://en.wikipedia.org/wiki/Peterson%27s_algorithm&lt;/A&gt;&lt;BR /&gt;It won't work without StoreLoad memory fence.&lt;BR /&gt;&lt;BR /&gt;In order to fix you need to insert:&lt;BR /&gt;lock()&lt;BR /&gt;...&lt;BR /&gt; sleeping++; &lt;BR /&gt; _mm_mfence();&lt;BR /&gt; while(_InterlockedCompareExchange(&amp;amp;spinlock, 1, 0)) &lt;BR /&gt;&lt;BR /&gt;unlock()&lt;BR /&gt;...&lt;SPAN&gt;&lt;BR /&gt;spinlock=0; &lt;BR /&gt;_mm_mfence();&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;SPAN class="keyword"&gt;if&lt;/SPAN&gt;&lt;SPAN&gt;(sleeping)&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Tue, 15 Dec 2009 14:29:18 GMT</pubDate>
    <dc:creator>Dmitry_Vyukov</dc:creator>
    <dc:date>2009-12-15T14:29:18Z</dc:date>
    <item>
      <title>SpinWaitLock</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SpinWaitLock/m-p/860843#M2317</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I have implemented a simple spinlock based on the intrinsic InterlockedCompareExchange.&lt;BR /&gt;The waiting thread loops for some time. After a while it gives up and waits on a condition variable until the lock becomes free again.&lt;BR /&gt;&lt;BR /&gt;I believe to code is correct, as long as everything is executed in the order defined by the code.&lt;BR /&gt;Still it happens that a thread is sleeping forever, while the lock is free and sleeping is 1.&lt;BR /&gt;&lt;BR /&gt;It appears that sleeping is incremented _after_ the waiting thread sleeps. I just don't get it.&lt;BR /&gt;Any hints are appreciated.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Ralph&lt;BR /&gt;&lt;BR /&gt;
&lt;PRE&gt;[cpp]static volatile unsigned long spinlock;&lt;BR /&gt;static volatile unsigned long sleeping;&lt;BR /&gt;&lt;BR /&gt;lock()&lt;BR /&gt;{  while(_InterlockedCompareExchange(&amp;amp;spinlock, 1, 0))&lt;BR /&gt;   {  nloops++;&lt;BR /&gt;   &lt;BR /&gt;      if(nloops &amp;gt; threshold)&lt;BR /&gt;      {  pthread_mutex_lock(&amp;amp;mutex);&lt;BR /&gt;         sleeping++;&lt;BR /&gt;         while(_InterlockedCompareExchange(&amp;amp;spinlock, 1, 0))&lt;BR /&gt;            pthread_cond_wait(&amp;amp;cond, &amp;amp;mutex);&lt;BR /&gt;         sleeping--;&lt;BR /&gt;         pthread_mutex_unlock(&amp;amp;mutex);&lt;BR /&gt;         break;&lt;BR /&gt;      }&lt;BR /&gt;   }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;unlock()&lt;BR /&gt;{  spinlock=0;&lt;BR /&gt;   if(sleeping)&lt;BR /&gt;   {  pthread_mutex_lock(&amp;amp;mutex);&lt;BR /&gt;      pthread_cond_signal(&amp;amp;cond);&lt;BR /&gt;      pthread_mutex_unlock(&amp;amp;mutex);&lt;BR /&gt;   }&lt;BR /&gt;}[/cpp]&lt;/PRE&gt;
&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Dec 2009 14:17:32 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SpinWaitLock/m-p/860843#M2317</guid>
      <dc:creator>racker</dc:creator>
      <dc:date>2009-12-15T14:17:32Z</dc:date>
    </item>
    <item>
      <title>Re: SpinWaitLock</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SpinWaitLock/m-p/860844#M2318</link>
      <description>You need #StoreLoad style memory fence in critical store-load sequence. You have basically an instance of Dekker/Peterson algorithm in your slow-path:&lt;BR /&gt;&lt;A href="http://en.wikipedia.org/wiki/Peterson%27s_algorithm"&gt;http://en.wikipedia.org/wiki/Peterson%27s_algorithm&lt;/A&gt;&lt;BR /&gt;It won't work without StoreLoad memory fence.&lt;BR /&gt;&lt;BR /&gt;In order to fix you need to insert:&lt;BR /&gt;lock()&lt;BR /&gt;...&lt;BR /&gt; sleeping++; &lt;BR /&gt; _mm_mfence();&lt;BR /&gt; while(_InterlockedCompareExchange(&amp;amp;spinlock, 1, 0)) &lt;BR /&gt;&lt;BR /&gt;unlock()&lt;BR /&gt;...&lt;SPAN&gt;&lt;BR /&gt;spinlock=0; &lt;BR /&gt;_mm_mfence();&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;SPAN class="keyword"&gt;if&lt;/SPAN&gt;&lt;SPAN&gt;(sleeping)&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Dec 2009 14:29:18 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SpinWaitLock/m-p/860844#M2318</guid>
      <dc:creator>Dmitry_Vyukov</dc:creator>
      <dc:date>2009-12-15T14:29:18Z</dc:date>
    </item>
  </channel>
</rss>

