Are memory barriers necessary for atomic reference counting shared immutable data?

Posted by Dietrich Epp on Stack Overflow See other posts from Stack Overflow or by Dietrich Epp
Published on 2010-04-08T10:57:58Z Indexed on 2010/04/08 22:03 UTC
Read the original article Hit count: 245

I have some immutable data structures that I would like to manage using reference counts, sharing them across threads on an SMP system.

Here's what the release code looks like:

void avocado_release(struct avocado *p)
{
    if (atomic_dec(p->refcount) == 0) {
        free(p->pit);
        free(p->juicy_innards);
        free(p);
    }
}

Does atomic_dec need a memory barrier in it? If so, what kind of memory barrier?

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 only field that changes over the duration of the object.

© Stack Overflow or respective owner

Related posts about c

    Related posts about Atomic