linux-old/include/linux/spinlock.h
<<
>>
Prefs
   1#ifndef __LINUX_SPINLOCK_H
   2#define __LINUX_SPINLOCK_H
   3
   4#include <linux/config.h>
   5
   6/*
   7 * These are the generic versions of the spinlocks and read-write
   8 * locks..
   9 */
  10#define spin_lock_irqsave(lock, flags)          do { local_irq_save(flags);       spin_lock(lock); } while (0)
  11#define spin_lock_irq(lock)                     do { local_irq_disable();         spin_lock(lock); } while (0)
  12#define spin_lock_bh(lock)                      do { local_bh_disable();          spin_lock(lock); } while (0)
  13
  14#define read_lock_irqsave(lock, flags)          do { local_irq_save(flags);       read_lock(lock); } while (0)
  15#define read_lock_irq(lock)                     do { local_irq_disable();         read_lock(lock); } while (0)
  16#define read_lock_bh(lock)                      do { local_bh_disable();          read_lock(lock); } while (0)
  17
  18#define write_lock_irqsave(lock, flags)         do { local_irq_save(flags);      write_lock(lock); } while (0)
  19#define write_lock_irq(lock)                    do { local_irq_disable();        write_lock(lock); } while (0)
  20#define write_lock_bh(lock)                     do { local_bh_disable();         write_lock(lock); } while (0)
  21
  22#define spin_unlock_irqrestore(lock, flags)     do { spin_unlock(lock);  local_irq_restore(flags); } while (0)
  23#define spin_unlock_irq(lock)                   do { spin_unlock(lock);  local_irq_enable();       } while (0)
  24#define spin_unlock_bh(lock)                    do { spin_unlock(lock);  local_bh_enable();        } while (0)
  25
  26#define read_unlock_irqrestore(lock, flags)     do { read_unlock(lock);  local_irq_restore(flags); } while (0)
  27#define read_unlock_irq(lock)                   do { read_unlock(lock);  local_irq_enable();       } while (0)
  28#define read_unlock_bh(lock)                    do { read_unlock(lock);  local_bh_enable();        } while (0)
  29
  30#define write_unlock_irqrestore(lock, flags)    do { write_unlock(lock); local_irq_restore(flags); } while (0)
  31#define write_unlock_irq(lock)                  do { write_unlock(lock); local_irq_enable();       } while (0)
  32#define write_unlock_bh(lock)                   do { write_unlock(lock); local_bh_enable();        } while (0)
  33#define spin_trylock_bh(lock)                   ({ int __r; local_bh_disable();\
  34                                                __r = spin_trylock(lock);      \
  35                                                if (!__r) local_bh_enable();   \
  36                                                __r; })
  37
  38#ifdef CONFIG_SMP
  39#include <asm/spinlock.h>
  40
  41#elif !defined(spin_lock_init) /* !SMP and spin_lock_init not previously
  42                                  defined (e.g. by including asm/spinlock.h */
  43
  44#define DEBUG_SPINLOCKS 0       /* 0 == no debugging, 1 == maintain lock state, 2 == full debug */
  45
  46#if (DEBUG_SPINLOCKS < 1)
  47
  48#define atomic_dec_and_lock(atomic,lock) atomic_dec_and_test(atomic)
  49#define ATOMIC_DEC_AND_LOCK
  50
  51/*
  52 * Your basic spinlocks, allowing only a single CPU anywhere
  53 *
  54 * Most gcc versions have a nasty bug with empty initializers.
  55 */
  56#if (__GNUC__ > 2)
  57  typedef struct { } spinlock_t;
  58  #define SPIN_LOCK_UNLOCKED (spinlock_t) { }
  59#else
  60  typedef struct { int gcc_is_buggy; } spinlock_t;
  61  #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
  62#endif
  63
  64#define spin_lock_init(lock)    do { } while(0)
  65#define spin_lock(lock)         (void)(lock) /* Not "unused variable". */
  66#define spin_is_locked(lock)    (0)
  67#define spin_trylock(lock)      ({1; })
  68#define spin_unlock_wait(lock)  do { } while(0)
  69#define spin_unlock(lock)       do { } while(0)
  70
  71#elif (DEBUG_SPINLOCKS < 2)
  72
  73typedef struct {
  74        volatile unsigned long lock;
  75} spinlock_t;
  76#define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
  77
  78#define spin_lock_init(x)       do { (x)->lock = 0; } while (0)
  79#define spin_is_locked(lock)    (test_bit(0,(lock)))
  80#define spin_trylock(lock)      (!test_and_set_bit(0,(lock)))
  81
  82#define spin_lock(x)            do { (x)->lock = 1; } while (0)
  83#define spin_unlock_wait(x)     do { } while (0)
  84#define spin_unlock(x)          do { (x)->lock = 0; } while (0)
  85
  86#else /* (DEBUG_SPINLOCKS >= 2) */
  87
  88typedef struct {
  89        volatile unsigned long lock;
  90        volatile unsigned int babble;
  91        const char *module;
  92} spinlock_t;
  93#define SPIN_LOCK_UNLOCKED (spinlock_t) { 0, 25, __BASE_FILE__ }
  94
  95#include <linux/kernel.h>
  96
  97#define spin_lock_init(x)       do { (x)->lock = 0; } while (0)
  98#define spin_is_locked(lock)    (test_bit(0,(lock)))
  99#define spin_trylock(lock)      (!test_and_set_bit(0,(lock)))
 100
 101#define spin_lock(x)            do {unsigned long __spinflags; save_flags(__spinflags); cli(); if ((x)->lock&&(x)->babble) {printk("%s:%d: spin_lock(%s:%p) already locked\n", __BASE_FILE__,__LINE__, (x)->module, (x));(x)->babble--;} (x)->lock = 1; restore_flags(__spinflags);} while (0)
 102#define spin_unlock_wait(x)     do {unsigned long __spinflags; save_flags(__spinflags); cli(); if ((x)->lock&&(x)->babble) {printk("%s:%d: spin_unlock_wait(%s:%p) deadlock\n", __BASE_FILE__,__LINE__, (x)->module, (x));(x)->babble--;} restore_flags(__spinflags);} while (0)
 103#define spin_unlock(x)          do {unsigned long __spinflags; save_flags(__spinflags); cli(); if (!(x)->lock&&(x)->babble) {printk("%s:%d: spin_unlock(%s:%p) not locked\n", __BASE_FILE__,__LINE__, (x)->module, (x));(x)->babble--;} (x)->lock = 0; restore_flags(__spinflags);} while (0)
 104
 105#endif  /* DEBUG_SPINLOCKS */
 106
 107/*
 108 * Read-write spinlocks, allowing multiple readers
 109 * but only one writer.
 110 *
 111 * NOTE! it is quite common to have readers in interrupts
 112 * but no interrupt writers. For those circumstances we
 113 * can "mix" irq-safe locks - any writer needs to get a
 114 * irq-safe write-lock, but readers can get non-irqsafe
 115 * read-locks.
 116 *
 117 * Most gcc versions have a nasty bug with empty initializers.
 118 */
 119#if (__GNUC__ > 2)
 120  typedef struct { } rwlock_t;
 121  #define RW_LOCK_UNLOCKED (rwlock_t) { }
 122#else
 123  typedef struct { int gcc_is_buggy; } rwlock_t;
 124  #define RW_LOCK_UNLOCKED (rwlock_t) { 0 }
 125#endif
 126
 127#define rwlock_init(lock)       do { } while(0)
 128#define read_lock(lock)         (void)(lock) /* Not "unused variable". */
 129#define read_unlock(lock)       do { } while(0)
 130#define write_lock(lock)        (void)(lock) /* Not "unused variable". */
 131#define write_unlock(lock)      do { } while(0)
 132
 133#endif /* !SMP */
 134
 135/* "lock on reference count zero" */
 136#ifndef ATOMIC_DEC_AND_LOCK
 137#include <asm/atomic.h>
 138extern int atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
 139#endif
 140
 141#endif /* __LINUX_SPINLOCK_H */
 142
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.