<?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 SMP locking in Intel® Moderncode for Parallel Architectures</title>
    <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763243#M8</link>
    <description>FWIW, y&lt;SPAN style="font-family: verdana, sans-serif;"&gt;ou can do a simple mutex with atomic swapand a binary semaphore. Here is some pseudo-code, memory barriersomittedfor clarity:&lt;/SPAN&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;[bash]struct mutex
{
    atomic_word m_state; // = 0
    binary_semaphore m_waitset;


    void lock()
    {
        if (ATOMIC_SWAP(&amp;amp;m_state, 1))
        {
            while (ATOMIC_SWAP(&amp;amp;m_state, 2))
            {
                m_waitset.wait();
            }
        }
    }


    void unlock()
    {
        if (ATOMIC_SWAP(&amp;amp;m_state, 0) == 2)
        {
            m_waitset.post();
        }
    }
};[/bash]&lt;/PRE&gt; &lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;You can also create a very nifty bakery-style read/write spinlock using atomic fetch-and-add. Joe Seigh created this extremely neatalgorithm; memory barriersomittedfor clarity:&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;[bash]struct rwspinlock { 
    enum constant 
    { 
        READ_ACCESS  = 0x10000, 
        WRITE_ACCESS = 1 
    }; 


    atomic_word m_next;     // = 0 
    atomic_word m_current;  // = 0 


    bool prv_check_read(atomic_word ticket) 
    { 
        return (ticket == (ATOMIC_LOAD(&amp;amp;m_current) % READ_ACCESS); 
    } 


    bool prv_check_write(atomic_word ticket) 
    { 
        return (ticket == ATOMIC_LOAD(&amp;amp;m_current)); 
    } 


    void rdlock() 
    { 
        atomic_word ticket = ATOMIC_FAA(&amp;amp;m_next, READ_ACCESS) % READ_ACCESS; 
        while (! prv_check_read(ticket)) cpu_yield();
    } 


    void rdunlock() 
    { 
        ATOMIC_FAA(&amp;amp;m_current, READ_ACCESS); 
    } 


    void wrlock() 
    { 
        atomic_word ticket = ATOMIC_FAA(&amp;amp;m_next, WRITE_ACCESS); 
        while (! prv_check_write(ticket)) cpu_yield();
    } 


    void wrunlock() 
    { 
        ATOMIC_FAA(&amp;amp;m_current, WRITE_ACCESS); 
    } 
}; [/bash]&lt;/PRE&gt; &lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Wed, 01 Sep 2010 01:05:47 GMT</pubDate>
    <dc:creator>Chris_M__Thomasson</dc:creator>
    <dc:date>2010-09-01T01:05:47Z</dc:date>
    <item>
      <title>SMP locking</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763236#M1</link>
      <description>Hi,&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;I would like to know more information on implementing a SMP lock?&lt;/DIV&gt;&lt;DIV&gt;Does it have any HW dependancy?&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;What happens if two processorts try to execute same instruction in SMP case? How do we achieve locking in this case when only one OS present in memory?&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Thanks,&lt;/DIV&gt;&lt;DIV&gt;Mani&lt;/DIV&gt;</description>
      <pubDate>Fri, 13 Aug 2010 14:22:45 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763236#M1</guid>
      <dc:creator>rkmanikanta</dc:creator>
      <dc:date>2010-08-13T14:22:45Z</dc:date>
    </item>
    <item>
      <title>SMP locking</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763237#M2</link>
      <description>&lt;P&gt;Hello Mani,&lt;BR /&gt;&lt;BR /&gt;Here are some articles that might be helpful:&lt;BR /&gt;&lt;A href="http://software.intel.com/en-us/articles/effective-implementation-of-locks-using-spin-locks/"&gt;http://software.intel.com/en-us/articles/effective-implementation-of-locks-using-spin-locks/&lt;/A&gt;&lt;BR /&gt;&lt;A href="http://www.embeddedintel.com/special_features.php?article=240"&gt;http://www.embeddedintel.com/special_features.php?article=240&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Let me know if you were looking for something else.&lt;BR /&gt;&lt;BR /&gt;==&lt;BR /&gt;Aubrey W.&lt;BR /&gt;Intel Software Network Support&lt;/P&gt;</description>
      <pubDate>Tue, 17 Aug 2010 22:21:30 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763237#M2</guid>
      <dc:creator>Aubrey_W_</dc:creator>
      <dc:date>2010-08-17T22:21:30Z</dc:date>
    </item>
    <item>
      <title>SMP locking</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763238#M3</link>
      <description>Mani -&lt;BR /&gt;&lt;BR /&gt;A simple lock can be implemented with the atomic Compare-and-Swap (CAS) instruction. To lock, threads compare the lock value to '1' and swap the value of '0' into the lock if the comparison succeeds. Other threads attempting the same operation after a successful locking will find the lock value == 0 and must spin until the value changes back to 1. To unlock, a thread simply stores a '1' back inlock variable (or uses CAS to compare with '0' and swap in a '1' on success).&lt;BR /&gt;&lt;BR /&gt;Since the operation is atomic, only one thread at a time can execute the instruction at a time to completion.&lt;BR /&gt;&lt;BR /&gt;There is such an instruction on the Itanium processor. I don't believe any other IA chip has this (but I may be wrong). There is a Windows intrinsic that can be called to execute a CAS atomically (InterlockedCompareAndExchange).&lt;BR /&gt;&lt;BR /&gt;Hope something here helps.&lt;BR /&gt;&lt;BR /&gt;--clay</description>
      <pubDate>Tue, 17 Aug 2010 22:34:46 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763238#M3</guid>
      <dc:creator>ClayB</dc:creator>
      <dc:date>2010-08-17T22:34:46Z</dc:date>
    </item>
    <item>
      <title>SMP locking</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763239#M4</link>
      <description>Hi Aubrey,&lt;DIV&gt;&lt;BR /&gt;Thanks for the links.&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Rgds,&lt;/DIV&gt;&lt;DIV&gt;Mani&lt;/DIV&gt;</description>
      <pubDate>Wed, 18 Aug 2010 10:04:58 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763239#M4</guid>
      <dc:creator>rkmanikanta</dc:creator>
      <dc:date>2010-08-18T10:04:58Z</dc:date>
    </item>
    <item>
      <title>SMP locking</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763240#M5</link>
      <description>Hi Clay,&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;I have a question:&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;For SMP locking, does HW support is necessary? If not, how can SW does it?&lt;/DIV&gt;&lt;DIV&gt;Can Normal Semaphore will do job for me?&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Thanks,&lt;/DIV&gt;&lt;DIV&gt;Mani&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 18 Aug 2010 10:06:17 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763240#M5</guid>
      <dc:creator>rkmanikanta</dc:creator>
      <dc:date>2010-08-18T10:06:17Z</dc:date>
    </item>
    <item>
      <title>SMP locking</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763241#M6</link>
      <description>&lt;DIV id="tiny_quote"&gt;
                &lt;DIV style="margin-left: 2px; margin-right: 2px;"&gt;Quoting &lt;A rel="/en-us/services/profile/quick_profile.php?is_paid=&amp;amp;user_id=488727" class="basic" href="https://community.intel.com/en-us/profile/488727/"&gt;rkmanikanta&lt;/A&gt;&lt;/DIV&gt;
                &lt;DIV style="background-color: #e5e5e5; padding: 5px; border: 1px inset; margin-left: 2px; margin-right: 2px;"&gt;&lt;I&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;For SMP locking, does HW support is necessary? If not, how can SW does it?&lt;/DIV&gt;&lt;DIV&gt;Can Normal Semaphore will do job for me?&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/I&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It's impossible to implement locking w/o hardware support in some form. But every SMP capable hardware has to have such support. &lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 18 Aug 2010 10:21:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763241#M6</guid>
      <dc:creator>Dmitry_Vyukov</dc:creator>
      <dc:date>2010-08-18T10:21:11Z</dc:date>
    </item>
    <item>
      <title>SMP locking</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763242#M7</link>
      <description>Mani,&lt;BR /&gt;&lt;BR /&gt;In addition to CAS you can also use and atomic Swap or and atomic Add/Increment&lt;BR /&gt;&lt;BR /&gt;The following is a non-fair lock&lt;BR /&gt;&lt;BR /&gt;volatile long YourLock = 0;&lt;BR /&gt;...&lt;BR /&gt; // lock&lt;BR /&gt; while(InterlockedExchange(&amp;amp;YourLock, 1) != 0)&lt;BR /&gt; _mm_pause();&lt;BR /&gt;...&lt;BR /&gt; // unlock&lt;BR /&gt; YourLock = 0;&lt;BR /&gt;&lt;BR /&gt;---------------------------------&lt;BR /&gt;The following is a fair lock&lt;BR /&gt;&lt;BR /&gt;struct FairLock&lt;BR /&gt;{&lt;BR /&gt; volatile long A;&lt;BR /&gt; volatile long B;&lt;BR /&gt; FairLock() { A = B = 0;};&lt;BR /&gt; ~FairLock() { Lock(); }&lt;BR /&gt; void Lock()&lt;BR /&gt; {&lt;BR /&gt; long myTurn = InterlockedExchangeAdd(&amp;amp;A, 1);&lt;BR /&gt; while(B != myTurn)&lt;BR /&gt; _mm_pause();&lt;BR /&gt; }&lt;BR /&gt; void Unlock()&lt;BR /&gt; {&lt;BR /&gt; ++B;&lt;BR /&gt; } &lt;BR /&gt;}&lt;BR /&gt;struct FairLockLock&lt;BR /&gt;{&lt;BR /&gt; FairLock* aFairLock;&lt;BR /&gt; FairLockLock(FairLock* _aFairLock)&lt;BR /&gt; {&lt;BR /&gt; aFairLock = _aFairLock;&lt;BR /&gt; aFairLock-&amp;gt;Lock();&lt;BR /&gt; }&lt;BR /&gt; ~FairLockLock() { aFairLock-&amp;gt;Unlock(); }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;...&lt;BR /&gt;FairLock YourLock;&lt;BR /&gt;...&lt;BR /&gt; {&lt;BR /&gt; FairLockLock lock(&amp;amp;YourLock);&lt;BR /&gt;... code runs holding lock on YourLock&lt;BR /&gt; } // dtor unlocks lock on YourLock&lt;BR /&gt;&lt;BR /&gt;(above is untested code)&lt;BR /&gt;&lt;BR /&gt;There are many ways to perform locks&lt;BR /&gt;The above methods are suitible for short held locks (using _mm_pause())&lt;BR /&gt;For longer held locks you may wish to consider using Yield() or other less CPU hogging methods.&lt;BR /&gt;&lt;BR /&gt;Jim Dempsey</description>
      <pubDate>Wed, 18 Aug 2010 13:14:50 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763242#M7</guid>
      <dc:creator>jimdempseyatthecove</dc:creator>
      <dc:date>2010-08-18T13:14:50Z</dc:date>
    </item>
    <item>
      <title>SMP locking</title>
      <link>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763243#M8</link>
      <description>FWIW, y&lt;SPAN style="font-family: verdana, sans-serif;"&gt;ou can do a simple mutex with atomic swapand a binary semaphore. Here is some pseudo-code, memory barriersomittedfor clarity:&lt;/SPAN&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;[bash]struct mutex
{
    atomic_word m_state; // = 0
    binary_semaphore m_waitset;


    void lock()
    {
        if (ATOMIC_SWAP(&amp;amp;m_state, 1))
        {
            while (ATOMIC_SWAP(&amp;amp;m_state, 2))
            {
                m_waitset.wait();
            }
        }
    }


    void unlock()
    {
        if (ATOMIC_SWAP(&amp;amp;m_state, 0) == 2)
        {
            m_waitset.post();
        }
    }
};[/bash]&lt;/PRE&gt; &lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;You can also create a very nifty bakery-style read/write spinlock using atomic fetch-and-add. Joe Seigh created this extremely neatalgorithm; memory barriersomittedfor clarity:&lt;/DIV&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;PRE&gt;[bash]struct rwspinlock { 
    enum constant 
    { 
        READ_ACCESS  = 0x10000, 
        WRITE_ACCESS = 1 
    }; 


    atomic_word m_next;     // = 0 
    atomic_word m_current;  // = 0 


    bool prv_check_read(atomic_word ticket) 
    { 
        return (ticket == (ATOMIC_LOAD(&amp;amp;m_current) % READ_ACCESS); 
    } 


    bool prv_check_write(atomic_word ticket) 
    { 
        return (ticket == ATOMIC_LOAD(&amp;amp;m_current)); 
    } 


    void rdlock() 
    { 
        atomic_word ticket = ATOMIC_FAA(&amp;amp;m_next, READ_ACCESS) % READ_ACCESS; 
        while (! prv_check_read(ticket)) cpu_yield();
    } 


    void rdunlock() 
    { 
        ATOMIC_FAA(&amp;amp;m_current, READ_ACCESS); 
    } 


    void wrlock() 
    { 
        atomic_word ticket = ATOMIC_FAA(&amp;amp;m_next, WRITE_ACCESS); 
        while (! prv_check_write(ticket)) cpu_yield();
    } 


    void wrunlock() 
    { 
        ATOMIC_FAA(&amp;amp;m_current, WRITE_ACCESS); 
    } 
}; [/bash]&lt;/PRE&gt; &lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 01 Sep 2010 01:05:47 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Moderncode-for-Parallel/SMP-locking/m-p/763243#M8</guid>
      <dc:creator>Chris_M__Thomasson</dc:creator>
      <dc:date>2010-09-01T01:05:47Z</dc:date>
    </item>
  </channel>
</rss>

