Changeset 1714

Show
Ignore:
Timestamp:
09/11/10 16:09:05 (3 years ago)
Author:
ahu
Message:

thanks to RHEL5/CENTOS5.. we now have assembly in PowerDNS. "Thanks". Spotted by Bas && Imre Gergely.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pdns/pdns/misc.hh

    r1698 r1714  
    351351    explicit AtomicCounter( unsigned int v = 0) : value_( v ) {} 
    352352 
    353     void operator++() 
     353    unsigned int operator++() 
    354354    { 
    355         __sync_add_and_fetch( &value_, 1 ); 
     355      return atomic_exchange_and_add( &value_, +1 ) + 1; 
    356356    } 
    357357 
    358358    unsigned int operator--() 
    359359    { 
    360         return __sync_add_and_fetch( &value_, -1 ); 
     360      return atomic_exchange_and_add( &value_, -1 ) - 1; 
    361361    } 
    362362 
    363363    operator unsigned int() const 
    364364    { 
    365         return __sync_fetch_and_add( &value_, 0 ); 
     365      return atomic_exchange_and_add( &value_, 0); 
    366366    } 
    367367 
     
    371371 
    372372    mutable unsigned int value_; 
     373     
     374    // the below is necessary because __sync_fetch_and_add is not universally available on i386.. I 3> RHEL5.  
     375    #if defined( __GNUC__ ) && ( defined( __i386__ ) || defined( __x86_64__ ) ) 
     376    static int atomic_exchange_and_add( unsigned int * pw, int dv ) 
     377    { 
     378        // int r = *pw; 
     379        // *pw += dv; 
     380        // return r; 
     381 
     382        int r; 
     383 
     384        __asm__ __volatile__ 
     385        ( 
     386            "lock\n\t" 
     387            "xadd %1, %0": 
     388            "+m"( *pw ), "=r"( r ): // outputs (%0, %1) 
     389            "1"( dv ): // inputs (%2 == %1) 
     390            "memory", "cc" // clobbers 
     391        ); 
     392 
     393        return r; 
     394    } 
     395    #else  
     396    static int atomic_exchange_and_add( unsigned int * pw, int dv ) 
     397    { 
     398      return __sync_fetch_and_add(pw, dv); 
     399    } 
     400    #endif 
    373401}; 
    374402