add simple locking primitives
authorKirk McKusick <mckusick@ucbvax.Berkeley.EDU>
Sun, 14 May 1995 02:26:04 +0000 (18:26 -0800)
committerKirk McKusick <mckusick@ucbvax.Berkeley.EDU>
Sun, 14 May 1995 02:26:04 +0000 (18:26 -0800)
SCCS-vsn: sys/hp300/include/param.h 8.3

usr/src/sys/hp300/include/param.h

index d1a6322..781cfac 100644 (file)
@@ -18,6 +18,7 @@
  * Machine dependent constants for HP9000 series 300.
  */
 #define        MACHINE "hp300"
  * Machine dependent constants for HP9000 series 300.
  */
 #define        MACHINE "hp300"
+#define NCPUS  1
 
 /*
  * Round p (pointer or byte index) up to a correctly-aligned value for all
 
 /*
  * Round p (pointer or byte index) up to a correctly-aligned value for all
@@ -173,3 +174,59 @@ int        cpuspeed;
 #define HPMMBASEADDR(v) \
        ((unsigned)(v) & ~HPMMMASK)
 #endif
 #define HPMMBASEADDR(v) \
        ((unsigned)(v) & ~HPMMMASK)
 #endif
+
+/*
+ * A simple spin lock.
+ *
+ * This structure only sets one bit of data, but is sized based on the
+ * minimum word size that can be operated on by the hardware test-and-set
+ * instruction. It is only needed for multiprocessors, as uniprocessors
+ * will always run to completion or a sleep. It is an error to hold one
+ * of these locks while a process is sleeping.
+ */
+struct simplelock {
+       int     lock_data;
+};
+
+#if !defined(DEBUG) && NCPUS > 1
+/*
+ * The simple-lock routines are the primitives out of which the lock
+ * package is built. The machine-dependent code must implement an
+ * atomic test_and_set operation that indivisibly sets the simple lock
+ * to non-zero and returns its old value. It also assumes that the
+ * setting of the lock to zero below is indivisible. Simple locks may
+ * only be used for exclusive locks.
+ */
+static __inline void
+simple_lock_init(lkp)
+       struct simplelock *lkp;
+{
+
+       lkp->lock_data = 0;
+}
+
+static __inline void
+simple_lock(lkp)
+       __volatile struct simplelock *lkp;
+{
+
+       while (test_and_set(&lkp->lock_data))
+               continue;
+}
+
+static __inline int
+simple_lock_try(lkp)
+       __volatile struct simplelock *lkp;
+{
+
+       return (!test_and_set(&lkp->lock_data))
+}
+
+static __inline void
+simple_unlock(lkp)
+       __volatile struct simplelock *lkp;
+{
+
+       lkp->lock_data = 0;
+}
+#endif /* NCPUS > 1 */