0
    static __inline__ int atomic_dec_and_test(atomic_t *v)
{
    unsigned char c;

    __asm__ __volatile__(
        LOCK "decl %0; sete %1"
        :"=m" (v->counter), "=qm" (c)
        :"m" (v->counter) : "memory");
    return c != 0;
} 

this implementation puzzle me a lot the atomic means what in this function? only the decrease part? but how can we ensure the sete return the right value? we need to disable the interrupt in the function invoke this one?, seems the atomic is only for one sentence(decl) in this function, not the whole function?

Adambynes
  • 197
  • 1
  • 10
  • i need to make myself clearly, i know the atomic is ensured in decl instruction with the lock prefix but how this function ensure the sete is using the right value the eflag register cause after the decl before sete there are could be a interrupt and eflag value could be changed by the interrupt handler!!!!!!!!!!!! – Adambynes Mar 05 '14 at 16:01
  • Interrupts don't change flags, unless deliberately coded to do so. Otherwise you couldn't write any code. Like, if you couldn't rely on the flags being preserved between a `CMP` and an accompanying conditional instruction, atomic or not. – Jester Mar 05 '14 at 16:38
  • Or, more correctly, flags are restored when returning from an interrupt. – ninjalj Mar 05 '14 at 17:15
  • that's the right answer i need to hear, thanks ninjalj – Adambynes Mar 06 '14 at 03:22

2 Answers2

2

v is an atomic variable. Accesses to that variable must be atomic. The decl instruction is the only instruction in that function that accesses v, so it's the only one that needs to ensure atomic access. After that, a local variable (so not able to be accessed by different CPUs/contexts) is set based on the result of the decl.

So atomic_dec_and_test() means to atomically decrement an atomic variable, and test whether that resulted in reaching a zero value.

ninjalj
  • 42,493
  • 9
  • 106
  • 148
  • i need to make myself clearly, i know the atomic is ensured in decl instruction with the lock prefix but how this function ensure the sete is using the right value the eflag register cause after the decl before sete there are could be a interrupt and eflag value could be changed by the interrupt handler!!!!!!!!!!!! – Adambynes Mar 05 '14 at 16:02
0

I don't know which version of Linux kernel code you're looking at (I didn't find the code snippet you posted on current kernel version and on recent kernel it's been replaced with LOCK_PREFIX), but try to find out what LOCK stands, it's defined as macro. I'm sure you'll find lock instruction inside the LOCK macro, which tells cpu to execute the followed instruction atomically look here. In particular the following line:

In a multiprocessor environment, the LOCK# signal insures that the processor has 
exclusive use of any shared memory while the signal is asserted.

Note it's exclusive use of any shared memory. And I think that is the key.

rakib_
  • 136,911
  • 4
  • 20
  • 26