root/sys/simplelock.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. simple_lock_init
  2. simple_lock_init
  3. simple_lock
  4. simple_lock_try
  5. simple_unlock

    1 /*      $OpenBSD: simplelock.h,v 1.11 2004/06/13 21:49:28 niklas Exp $  */
    2 
    3 #ifndef _SIMPLELOCK_H_
    4 #define _SIMPLELOCK_H_
    5 
    6 #ifdef MULTIPROCESSOR
    7 #include <machine/lock.h>
    8 #endif
    9 
   10 /*
   11  * A simple spin lock.
   12  *
   13  * This structure only sets one bit of data, but is sized based on the
   14  * minimum word size that can be operated on by the hardware test-and-set
   15  * instruction. It is only needed for multiprocessors, as uniprocessors
   16  * will always run to completion or a sleep. It is an error to hold one
   17  * of these locks while a process is sleeping.
   18  */
   19 struct simplelock {
   20 #ifdef MULTIPROCESSOR
   21         __cpu_simple_lock_t lock_data;
   22 #else
   23         int     lock_data;
   24 #endif
   25 };
   26 
   27 #ifdef _KERNEL
   28 
   29 /*
   30  * We can't debug locks when we use them in real life.
   31  */
   32 #if defined(MULTIPROCESSOR) && defined(LOCKDEBUG)
   33 #undef LOCKDEBUG
   34 #endif
   35 
   36 #if !defined(MULTIPROCESSOR) || 1
   37 
   38 #define SLOCK_LOCKED 1
   39 #define SLOCK_UNLOCKED 0
   40 
   41 #ifndef LOCKDEBUG
   42 
   43 #define simple_lock(lkp)
   44 #define simple_lock_try(lkp)    (1)     /* always succeeds */
   45 #define simple_unlock(lkp)
   46 #define simple_lock_assert(lkp)
   47 
   48 static __inline void simple_lock_init(struct simplelock *lkp)
   49 {
   50 
   51         lkp->lock_data = SLOCK_UNLOCKED;
   52 }
   53 
   54 #else
   55 
   56 void _simple_unlock(__volatile struct simplelock *, const char *, int);
   57 int _simple_lock_try(__volatile struct simplelock *, const char *, int);
   58 void _simple_lock(__volatile struct simplelock *, const char *, int);
   59 void _simple_lock_assert(__volatile struct simplelock *, int, const char *, int);
   60 
   61 void simple_lock_init(struct simplelock *);
   62 #define simple_unlock(lkp) _simple_unlock(lkp, __FILE__, __LINE__)
   63 #define simple_lock_try(lkp) _simple_lock_try(lkp, __FILE__, __LINE__)
   64 #define simple_lock(lkp) _simple_lock(lkp, __FILE__, __LINE__)
   65 #define simple_lock_assert(lkp, state) _simple_lock_assert(lkp, state, __FILE__, __LINE__)
   66 
   67 #endif /* !defined(LOCKDEBUG) */
   68 
   69 #else  /* MULTIPROCESSOR */
   70 
   71 /*
   72  * The simple-lock routines are the primitives out of which the lock
   73  * package is built. The machine-dependent code must implement an
   74  * atomic test_and_set operation that indivisibly sets the simple lock
   75  * to non-zero and returns its old value. It also assumes that the
   76  * setting of the lock to zero below is indivisible. Simple locks may
   77  * only be used for exclusive locks.
   78  */
   79 
   80 static __inline void simple_lock_init(struct simplelock *lkp)
   81 {
   82         __cpu_simple_lock_init(&lkp->lock_data);
   83 }
   84 
   85 static __inline void simple_lock(__volatile struct simplelock *lkp)
   86 {
   87         __cpu_simple_lock(&lkp->lock_data);
   88 }
   89 
   90 static __inline int simple_lock_try(__volatile struct simplelock *lkp)
   91 {
   92         return (__cpu_simple_lock_try(&lkp->lock_data));
   93 }
   94 
   95 static __inline void simple_unlock(__volatile struct simplelock *lkp)
   96 {
   97         __cpu_simple_unlock(&lkp->lock_data);
   98 }
   99 #endif /* MULTIPROCESSOR */
  100 
  101 #endif /* _KERNEL */
  102 
  103 #endif /* !_SIMPLELOCK_H_ */

/* [<][>][^][v][top][bottom][index][help] */