<?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 Can we eliminate a memory barrier through code dependency ? in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/can-we-eliminate-a-memory-barrier-through-code-dependency/m-p/4480286#M680334</link>
    <description>We don't want to use a memory barrier and lock(such as spin lock, semaphore).&lt;BR /&gt;We hope to get the same effect like a memory barrier  through artificial code dependency.&lt;BR /&gt;As far as I know.. if there is a "dependency" in C code , CPU or compiler doesn't reorder the instructions.&lt;BR /&gt;then.. I wrote berow code. can you  100% guarantee that it works?&lt;BR /&gt;any help will be appreciated.&lt;BR /&gt;thanks :)&lt;BR /&gt;&lt;BR /&gt;[environment]&lt;BR /&gt;* HP-UX 11.31&lt;BR /&gt;* multi cpu, multi thread&lt;BR /&gt;&lt;BR /&gt;---------------------------------------------------------&lt;BR /&gt;// The original code is simplified to be understood more easily.&lt;BR /&gt;&lt;BR /&gt;struct data {&lt;BR /&gt;    unsigned int x;&lt;BR /&gt;    unsigned int sync_a;&lt;BR /&gt;    unsigned int sync_b;&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;void setx(struct data * p, int t)&lt;BR /&gt;{&lt;BR /&gt;    volatile unsigned int count = 0;&lt;BR /&gt;    if ( count++ == 0) { p-&amp;gt;sync_b++; } &lt;BR /&gt;    p-&amp;gt;x = t;&lt;BR /&gt;    if (count++ != 0 ) { p-&amp;gt;sync_a++; } &lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;int getx(struct data * p)&lt;BR /&gt;{&lt;BR /&gt;    unsigned int s0;&lt;BR /&gt;    unsigned int s1;&lt;BR /&gt;    unsigned int ret=0;&lt;BR /&gt;    s0=0;&lt;BR /&gt;    s1=0;&lt;BR /&gt;    while(1)&lt;BR /&gt;    {&lt;BR /&gt;        volatile unsigned int count = 0;&lt;BR /&gt;        if ( count++ == 0) { s0 = p-&amp;gt;sync_a; } &lt;BR /&gt;        ret = p-&amp;gt;x;&lt;BR /&gt;        if (count++ != 0 ) { s1 = p-&amp;gt;sync_b; } &lt;BR /&gt;        if( s0 == s1 )&lt;BR /&gt;        {&lt;BR /&gt;            break;&lt;BR /&gt;        }&lt;BR /&gt;        else&lt;BR /&gt;        {&lt;BR /&gt;           //pthread_yield();&lt;BR /&gt;        }&lt;BR /&gt;    }&lt;BR /&gt;    return ret;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;int main(void)&lt;BR /&gt;{&lt;BR /&gt;    struct data pp = { 100,0,0 };&lt;BR /&gt;    setx( &amp;amp;pp , 200 );&lt;BR /&gt;    printf("%d\n", getx(&amp;amp;pp));&lt;BR /&gt;    return 0;&lt;BR /&gt;}&lt;BR /&gt;---------------------------------------------------------&lt;BR /&gt;</description>
    <pubDate>Mon, 17 Aug 2009 01:54:11 GMT</pubDate>
    <dc:creator>Lee gi choen</dc:creator>
    <dc:date>2009-08-17T01:54:11Z</dc:date>
    <item>
      <title>Can we eliminate a memory barrier through code dependency ?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/can-we-eliminate-a-memory-barrier-through-code-dependency/m-p/4480286#M680334</link>
      <description>We don't want to use a memory barrier and lock(such as spin lock, semaphore).&lt;BR /&gt;We hope to get the same effect like a memory barrier  through artificial code dependency.&lt;BR /&gt;As far as I know.. if there is a "dependency" in C code , CPU or compiler doesn't reorder the instructions.&lt;BR /&gt;then.. I wrote berow code. can you  100% guarantee that it works?&lt;BR /&gt;any help will be appreciated.&lt;BR /&gt;thanks :)&lt;BR /&gt;&lt;BR /&gt;[environment]&lt;BR /&gt;* HP-UX 11.31&lt;BR /&gt;* multi cpu, multi thread&lt;BR /&gt;&lt;BR /&gt;---------------------------------------------------------&lt;BR /&gt;// The original code is simplified to be understood more easily.&lt;BR /&gt;&lt;BR /&gt;struct data {&lt;BR /&gt;    unsigned int x;&lt;BR /&gt;    unsigned int sync_a;&lt;BR /&gt;    unsigned int sync_b;&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;void setx(struct data * p, int t)&lt;BR /&gt;{&lt;BR /&gt;    volatile unsigned int count = 0;&lt;BR /&gt;    if ( count++ == 0) { p-&amp;gt;sync_b++; } &lt;BR /&gt;    p-&amp;gt;x = t;&lt;BR /&gt;    if (count++ != 0 ) { p-&amp;gt;sync_a++; } &lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;int getx(struct data * p)&lt;BR /&gt;{&lt;BR /&gt;    unsigned int s0;&lt;BR /&gt;    unsigned int s1;&lt;BR /&gt;    unsigned int ret=0;&lt;BR /&gt;    s0=0;&lt;BR /&gt;    s1=0;&lt;BR /&gt;    while(1)&lt;BR /&gt;    {&lt;BR /&gt;        volatile unsigned int count = 0;&lt;BR /&gt;        if ( count++ == 0) { s0 = p-&amp;gt;sync_a; } &lt;BR /&gt;        ret = p-&amp;gt;x;&lt;BR /&gt;        if (count++ != 0 ) { s1 = p-&amp;gt;sync_b; } &lt;BR /&gt;        if( s0 == s1 )&lt;BR /&gt;        {&lt;BR /&gt;            break;&lt;BR /&gt;        }&lt;BR /&gt;        else&lt;BR /&gt;        {&lt;BR /&gt;           //pthread_yield();&lt;BR /&gt;        }&lt;BR /&gt;    }&lt;BR /&gt;    return ret;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;int main(void)&lt;BR /&gt;{&lt;BR /&gt;    struct data pp = { 100,0,0 };&lt;BR /&gt;    setx( &amp;amp;pp , 200 );&lt;BR /&gt;    printf("%d\n", getx(&amp;amp;pp));&lt;BR /&gt;    return 0;&lt;BR /&gt;}&lt;BR /&gt;---------------------------------------------------------&lt;BR /&gt;</description>
      <pubDate>Mon, 17 Aug 2009 01:54:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/can-we-eliminate-a-memory-barrier-through-code-dependency/m-p/4480286#M680334</guid>
      <dc:creator>Lee gi choen</dc:creator>
      <dc:date>2009-08-17T01:54:11Z</dc:date>
    </item>
    <item>
      <title>Re:  Can we eliminate a memory barrier through code dependency ?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/can-we-eliminate-a-memory-barrier-through-code-dependency/m-p/4480287#M680335</link>
      <description>You didn't mention whether PA or Integrity.  I would forget about the former and use your mutexes.&lt;BR /&gt;Also you don't have any comments on what you are protecting, nor indicate where your multiple threads are and what you want to synchronize.&lt;BR /&gt;&lt;BR /&gt;I'm not sure why you have these ifs:&lt;BR /&gt;if (count++ == 0) { p-&amp;gt;sync_b++; }&lt;BR /&gt;&lt;BR /&gt;Just remove them:&lt;BR /&gt;++count;&lt;BR /&gt;++p-&amp;gt;sync_b;&lt;BR /&gt;ret = p-&amp;gt;x;&lt;BR /&gt;++count;&lt;BR /&gt;++p-&amp;gt;sync_a;&lt;BR /&gt;&lt;BR /&gt;Or course this may not do what you want.  Does this come from an algorithm on the web or book?</description>
      <pubDate>Mon, 17 Aug 2009 04:28:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/can-we-eliminate-a-memory-barrier-through-code-dependency/m-p/4480287#M680335</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2009-08-17T04:28:14Z</dc:date>
    </item>
    <item>
      <title>Re:  Can we eliminate a memory barrier through code dependency ?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/can-we-eliminate-a-memory-barrier-through-code-dependency/m-p/4480288#M680336</link>
      <description>if I understand you you want to use our&lt;BR /&gt;if (count++..) as memory fencing&lt;BR /&gt;&lt;BR /&gt;when you do your setx sync_a can not be flushed to central memory, while sync_b have been flushed by the if(count++!=0)&lt;BR /&gt;&lt;BR /&gt;so you can have a discrepency between your sync_a and sync_b seen by an other cpu.&lt;BR /&gt;&lt;BR /&gt;but you can be sure that p-&amp;gt;x is flushed before sync_a&lt;BR /&gt;&lt;BR /&gt;so indeed at a next loop you should get ret with the right value.</description>
      <pubDate>Mon, 17 Aug 2009 10:23:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/can-we-eliminate-a-memory-barrier-through-code-dependency/m-p/4480288#M680336</guid>
      <dc:creator>Laurent Menase</dc:creator>
      <dc:date>2009-08-17T10:23:34Z</dc:date>
    </item>
  </channel>
</rss>

