title: Are memory barriers necessary for atomic reference counting shared immutable data?
body: <p>I have some immutable data structures that I would like to manage using reference counts, sharing them across threads on an SMP system.</p>
  
  <p>Here's what the release code looks like:</p>
  
  <pre><code>void avocado_release(struct avocado *p)
  {
      if (atomic_dec(p-&gt;refcount) == 0) {
          free(p-&gt;pit);
          free(p-&gt;juicy_innards);
          free(p);
      }
  }
  </code></pre>
  
  <p>Does <code>atomic_dec</code> need a memory barrier in it?  If so, what kind of memory barrier?</p>
  
  <p>Additional notes: The application must run on PowerPC and x86, so any processor-specific information is welcomed.  I already know about the GCC atomic builtins.  As for immutability, the refcount is the <em>only</em> field that changes over the duration of the object.</p>