[zeromq-dev] Question on atomic_counter

Stewart Douglas sdouglas at snowgold.com
Thu Aug 5 19:51:55 CEST 2010


Am trying to use the atomic_counter class in a separate piece of code, 
and noticed a compilation warning when compiling on a different box with 
an older version of GCC. But get no warning on my normal box when 
compiling with GCC 4.4.3. Am more used to more warnings from the newer 
version of gcc.

Running on a Ubuntu 64 bit on a Xeon processor , I believe the code 
below will be what executes. integer_t is a typedef for uint32_t, so the 
assignment to -decrement seems wrong to me - the zmq code is always 
calling sub with a parameter of one, to reduce the reference count by 
one in a zmq_msg_close() and pub_t::xsend().

So am puzzled how this is working ? Should oldval be an int32_t rather 
than uint32_t ?

         //  Atomic subtraction. Returns false if the counter drops to zero.
         inline bool sub (integer_t decrement)
         {

             integer_t oldval = -decrement;
             volatile integer_t *val = &value;
             __asm__ volatile ("lock; xaddl %0,%1"
                 : "=r" (oldval), "=m" (*val)
                 : "0" (oldval), "m" (*val)
                 : "cc", "memory");
             return oldval != decrement;
         }

Cheers

Stew

-- 
Stewart Douglas
sdouglas at snowgold.com
http://www.snowgold.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20100805/dbff38a2/attachment.htm>


More information about the zeromq-dev mailing list