home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / NextDeveloper / Headers / netware / kern / lock.h
Encoding:
C/C++ Source or Header  |  1997-03-28  |  8.0 KB  |  256 lines

  1. /* 
  2.  * Mach Operating System
  3.  * Copyright (c) 1993-1987 Carnegie Mellon University
  4.  * All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify and distribute this software and its
  7.  * documentation is hereby granted, provided that both the copyright
  8.  * notice and this permission notice appear in all copies of the
  9.  * software, derivative works or modified versions, and any portions
  10.  * thereof, and that both notices appear in supporting documentation.
  11.  * 
  12.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15.  * 
  16.  * Carnegie Mellon requests users of this software to return to
  17.  * 
  18.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  19.  *  School of Computer Science
  20.  *  Carnegie Mellon University
  21.  *  Pittsburgh PA 15213-3890
  22.  * 
  23.  * any improvements or extensions that they make and grant Carnegie Mellon
  24.  * the rights to redistribute these changes.
  25.  */
  26. /*
  27.  * HISTORY
  28.  * $Log:    lock.h,v $
  29.  * Revision 2.12  93/02/05  07:51:21  danner
  30.  *     Reorganization of lock structure.
  31.  *     Recursion_depth is now a bitfield,
  32.  *     and the thread pointer is the first field.
  33.  *     [93/02/04            danner]
  34.  * 
  35.  * Revision 2.11  93/01/24  13:19:22  danner
  36.  *     Add include of mach/machine/vm_types.h
  37.  * 
  38.  * Revision 2.10  93/01/19  09:00:57  danner
  39.  *     Underlying simple lock type is now volatile natural_t.
  40.  *     [93/01/19            danner]
  41.  * 
  42.  * Revision 2.9  93/01/14  17:35:00  danner
  43.  *     Added ANSI function prototypes.  Made simple lock data
  44.  *     volatile.
  45.  *     [92/12/30            dbg]
  46.  *     ANSI-fied cpp directives.
  47.  *     [92/12/01            af]
  48.  * 
  49.  * Revision 2.8  91/11/12  11:51:58  rvb
  50.  *     Added simple_lock_pause.
  51.  *     [91/11/12            rpd]
  52.  * 
  53.  * Revision 2.7  91/05/18  14:32:17  rpd
  54.  *     Added check_simple_locks.
  55.  *     [91/03/31            rpd]
  56.  * 
  57.  * Revision 2.6  91/05/14  16:43:51  mrt
  58.  *     Correcting copyright
  59.  * 
  60.  * Revision 2.5  91/05/08  12:47:17  dbg
  61.  *     When actually using the locks (on multiprocessors), import the
  62.  *     machine-dependent simple_lock routines from machine/lock.h.
  63.  *     [91/04/26  14:42:23  dbg]
  64.  * 
  65.  * Revision 2.4  91/02/05  17:27:37  mrt
  66.  *     Changed to new Mach copyright
  67.  *     [91/02/01  16:14:39  mrt]
  68.  * 
  69.  * Revision 2.3  90/11/05  14:31:18  rpd
  70.  *     Added simple_lock_taken.
  71.  *     [90/11/04            rpd]
  72.  * 
  73.  * Revision 2.2  90/01/11  11:43:26  dbg
  74.  *     Upgraded to match mainline:
  75.  *          Added decl_simple_lock_data, simple_lock_addr macros.
  76.  *          Rearranged complex lock structure to use decl_simple_lock_data
  77.  *          for the interlock field and put it last (except on ns32000).
  78.  *          [89/01/15  15:16:47  rpd]
  79.  * 
  80.  *     Made all machines use the compact field layout.
  81.  * 
  82.  * Revision 2.1  89/08/03  15:49:42  rwd
  83.  * Created.
  84.  * 
  85.  * Revision 2.2  88/07/20  16:49:35  rpd
  86.  * Allow for sanity-checking of simple locking on uniprocessors,
  87.  * controlled by new option MACH_LDEBUG.  Define composite
  88.  * MACH_SLOCKS, which is true iff simple locking calls expand
  89.  * to code.  It can be used to #if-out declarations, etc, that
  90.  * are only used when simple locking calls are real.
  91.  * 
  92.  *  3-Nov-87  David Black (dlb) at Carnegie-Mellon University
  93.  *    Use optimized lock structure for multimax also.
  94.  *
  95.  * 27-Oct-87  Robert Baron (rvb) at Carnegie-Mellon University
  96.  *    Use optimized lock "structure" for balance now that locks are
  97.  *    done inline.
  98.  *
  99.  * 26-Jan-87  Avadis Tevanian (avie) at Carnegie-Mellon University
  100.  *    Invert logic of no_sleep to can_sleep.
  101.  *
  102.  * 29-Dec-86  David Golub (dbg) at Carnegie-Mellon University
  103.  *    Removed BUSYP, BUSYV, adawi, mpinc, mpdec.  Defined the
  104.  *    interlock field of the lock structure to be a simple-lock.
  105.  *
  106.  *  9-Nov-86  Michael Young (mwyoung) at Carnegie-Mellon University
  107.  *    Added "unsigned" to fields in vax case, for lint.
  108.  *
  109.  * 21-Oct-86  Michael Young (mwyoung) at Carnegie-Mellon University
  110.  *    Added fields for sleep/recursive locks.
  111.  *
  112.  *  7-Oct-86  David L. Black (dlb) at Carnegie-Mellon University
  113.  *    Merged Multimax changes.
  114.  *
  115.  * 26-Sep-86  Michael Young (mwyoung) at Carnegie-Mellon University
  116.  *    Removed reference to "caddr_t" from BUSYV/P.  I really
  117.  *    wish we could get rid of these things entirely.
  118.  *
  119.  * 24-Sep-86  Michael Young (mwyoung) at Carnegie-Mellon University
  120.  *    Changed to directly import boolean declaration.
  121.  *
  122.  *  1-Aug-86  David Golub (dbg) at Carnegie-Mellon University
  123.  *    Added simple_lock_try, sleep locks, recursive locking.
  124.  *
  125.  * 11-Jun-86  Bill Bolosky (bolosky) at Carnegie-Mellon University
  126.  *    Removed ';' from definitions of locking macros (defined only
  127.  *    when NCPU < 2). so as to make things compile.
  128.  *
  129.  * 28-Feb-86  Bill Bolosky (bolosky) at Carnegie-Mellon University
  130.  *    Defined adawi to be add when not on a vax.
  131.  *
  132.  * 07-Nov-85  Michael Wayne Young (mwyoung) at Carnegie-Mellon University
  133.  *    Overhauled from previous version.
  134.  */
  135. /*
  136.  *    File:    kern/lock.h
  137.  *    Author:    Avadis Tevanian, Jr., Michael Wayne Young
  138.  *    Date:    1985
  139.  *
  140.  *    Locking primitives definitions
  141.  */
  142.  
  143. #ifndef    _KERN_LOCK_H_
  144. #define    _KERN_LOCK_H_
  145.  
  146. #import <mach/features.h>
  147.  
  148. #include <mach/boolean.h>
  149. #include <mach/machine/vm_types.h>
  150.  
  151. #define MACH_SLOCKS    ((NCPUS > 1) || MACH_LDEBUG || DRIVERKIT)
  152.  
  153. #ifndef    _MACHINE_SIMPLE_LOCK_DATA_
  154. /*
  155.  *    A simple spin lock.
  156.  */
  157.  
  158. struct slock {
  159.     volatile natural_t lock_data;    /* in general 1 bit is sufficient */
  160. };
  161.  
  162. typedef struct slock    simple_lock_data_t;
  163. typedef struct slock    *simple_lock_t;
  164. #endif    /* _MACHINE_SIMPLE_LOCK_DATA_ */
  165.  
  166. #if    MACH_SLOCKS || defined( NETWARE )
  167. /*
  168.  *    Use the locks.
  169.  */
  170.  
  171. #define    decl_simple_lock_data(class,name) \
  172. class    simple_lock_data_t    name;
  173.  
  174. #define    simple_lock_addr(lock)    (&(lock))
  175.  
  176. #else    /* MACH_SLOCKS */
  177. /*
  178.  * Do not allocate storage for locks if not needed.
  179.  */
  180. #define    decl_simple_lock_data(class,name)
  181. #define    simple_lock_addr(lock)        ((simple_lock_t)0)
  182.  
  183. /*
  184.  *    No multiprocessor locking is necessary.
  185.  */
  186. #define simple_lock_init(l)
  187. #define simple_lock(l)
  188. #define simple_unlock(l)
  189. #define simple_lock_try(l)    (TRUE)    /* always succeeds */
  190. #define simple_lock_taken(l)    (1)    /* always succeeds */
  191. #define check_simple_locks()
  192. #define simple_lock_pause()
  193.  
  194. #endif    /* MACH_SLOCKS */
  195.  
  196. extern simple_lock_t    simple_lock_alloc(void);
  197. extern void        simple_lock_free(simple_lock_t);
  198.  
  199. /*
  200.  *    The general lock structure.  Provides for multiple readers,
  201.  *    upgrading from read to write, and sleeping until the lock
  202.  *    can be gained.
  203.  *
  204.  *    On some architectures, assembly language code in the 'inline'
  205.  *    program fiddles the lock structures.  It must be changed in
  206.  *    concert with the structure layout.
  207.  *
  208.  *    Only the "interlock" field is used for hardware exclusion;
  209.  *    other fields are modified with normal instructions after
  210.  *    acquiring the interlock bit.
  211.  */
  212. struct lock {
  213.     struct thread    *thread;    /* Thread that has lock, if
  214.                        recursive locking allowed */
  215.     unsigned int    read_count:16,    /* Number of accepted readers */
  216.     /* boolean_t */    want_upgrade:1,    /* Read-to-write upgrade waiting */
  217.     /* boolean_t */    want_write:1,    /* Writer is waiting, or
  218.                        locked for write */
  219.     /* boolean_t */    waiting:1,    /* Someone is sleeping on lock */
  220.     /* boolean_t */    can_sleep:1,    /* Can attempts to lock go to sleep? */
  221.             recursion_depth:12, /* Depth of recursion */
  222.             :0; 
  223.     decl_simple_lock_data(,interlock)
  224.                     /* Hardware interlock field.
  225.                        Last in the structure so that
  226.                        field offsets are the same whether
  227.                        or not it is present. */
  228. };
  229.  
  230. typedef struct lock    lock_data_t;
  231. typedef struct lock    *lock_t;
  232.  
  233. /* Sleep locks must work even if no multiprocessing */
  234.  
  235. extern void        lock_init(lock_t, boolean_t);
  236. extern void        lock_sleepable(lock_t, boolean_t);
  237. extern void        lock_write(lock_t);
  238. extern void        lock_read(lock_t);
  239. extern void        lock_done(lock_t);
  240. extern boolean_t    lock_read_to_write(lock_t);
  241. extern void        lock_write_to_read(lock_t);
  242. extern boolean_t    lock_try_write(lock_t);
  243. extern boolean_t    lock_try_read(lock_t);
  244. extern boolean_t    lock_try_read_to_write(lock_t);
  245.  
  246. #define    lock_read_done(l)    lock_done(l)
  247. #define    lock_write_done(l)    lock_done(l)
  248.  
  249. extern void        lock_set_recursive(lock_t);
  250. extern void        lock_clear_recursive(lock_t);
  251.  
  252. extern lock_t        lock_alloc(void);
  253. extern void        lock_free(lock_t);
  254.  
  255. #endif    /* _KERN_LOCK_H_ */
  256.