NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: atomic_ops - atomic_*() vs atomic_*_nv()



On Fri, Nov 20, 2009 at 01:00:16PM +0000, raymond.meyer%rambler.ru@localhost 
wrote:
> The functions are duplicate of each other, the difference is one does
> not return anything and the other returns a new value.

Which is important if you want to an atomic add and test.

> Where would you use either of those functions?

void
MyClassReferenceClass::release()
{
        if (atomic_add_32_nv(refCount, -1) == 0) {
                delete this;
        }
}

> If I want to have a shared counter that doesn't overflow above a certain
> value, how can atomic_ops help?

#define MAX     42

volatile uint32_t current;

bool
acquire(void)
{
        if (atomic_add_32_nv(current, 1) <= MAX)
                return true;

        atomic_add_32(current, -1);
        return false;
}

void
release(void)
{
        atomic_add_32(current, -1);
}

        Kind regards

-- 
Matthias Scheler                                  http://zhadum.org.uk/


Home | Main Index | Thread Index | Old Index