Back to home page

LXR

 
 

    


0001 #include <linux/export.h>
0002 #include <linux/spinlock.h>
0003 #include <linux/atomic.h>
0004 
0005 /*
0006  * This is an implementation of the notion of "decrement a
0007  * reference count, and return locked if it decremented to zero".
0008  *
0009  * NOTE NOTE NOTE! This is _not_ equivalent to
0010  *
0011  *  if (atomic_dec_and_test(&atomic)) {
0012  *      spin_lock(&lock);
0013  *      return 1;
0014  *  }
0015  *  return 0;
0016  *
0017  * because the spin-lock and the decrement must be
0018  * "atomic".
0019  */
0020 int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock)
0021 {
0022     /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
0023     if (atomic_add_unless(atomic, -1, 1))
0024         return 0;
0025 
0026     /* Otherwise do it the slow way */
0027     spin_lock(lock);
0028     if (atomic_dec_and_test(atomic))
0029         return 1;
0030     spin_unlock(lock);
0031     return 0;
0032 }
0033 
0034 EXPORT_SYMBOL(_atomic_dec_and_lock);