Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 21 May 2002 13:27:13 -0700 (PDT)
From:      Archie Cobbs <archie@dellroad.org>
To:        freebsd-arch@freebsd.org
Subject:   question about atomic.h
Message-ID:  <200205212027.g4LKRD058396@arch20m.dellroad.org>

next in thread | raw e-mail | index | archive | help
Hi-

I'm trying to understand the logic behind the
atomic_{add,subtract}_{char,short,int,long}() functions.

Looking at i386/include/atomic.h, it appears all of these functions
return void.

It seems like they would be more useful if they returned the new
value.. is that possible? In any case, the comment is misleading
in this respect-- it should include a (void) cast, e.g.:

    -atomic_add_short(P, V)       (*(u_short*)(P) += (V))
    +atomic_add_short(P, V)       (void)(*(u_short*)(P) += (V))

The example in mind is a function that decrements a reference
count on an object without obtaining the object's mutex. If the
ref count goes to zero you want to free the object (and in this
case there is no need to acquire the mutex because you have the
only reference).

With atomic_subtract_int() returning void, there's no way to do
that without acquiring the mutex, because you can't atomically
decrement *and* read the reference count.

Thanks for any insights,
-Archie

__________________________________________________________________________
Archie Cobbs     *     Packet Design     *     http://www.packetdesign.com

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200205212027.g4LKRD058396>