Why is the xadd instruction used to get atomic operation?

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

 



Hi,

    xadd is used in atomic function atomic_add_return() like this:

 188static __inline__ int atomic_add_return(int i, atomic_t *v)
 189{
 190        int __i;
                  ...           ...           ...

 195        /* Modern 486+ processor */
 196        __i = i;
 197        __asm__ __volatile__(
 198                LOCK "xaddl %0, %1;"
 199                :"=r"(i)
 200                :"m"(v->counter), "0"(i));
 201        return i + __i;
 202
                  ...           ...           ...
 211}

The xadd will store v->counter into i, and store v->counter+i into
v->counter. I wonder why we bother to obtain v->counter atomically. I
try to modify the asm with the following code:

static __inline__ int atomic_add_return(int i, atomic_t *v)
{
     __asm__ __volatile__(
          LOCK "addl %1, %0;"
          : "m"(v->counter)
          : "=r"(i), "m"(v->counter));
     return v->counter;
}

Does my code may cause some problems concerning atomic operation?

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux