Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev Replace home-grown locks with lockmgr().



details:   https://anonhg.NetBSD.org/src/rev/35984ccfcab1
branches:  trunk
changeset: 555008:35984ccfcab1
user:      mycroft <mycroft%NetBSD.org@localhost>
date:      Fri Nov 07 04:10:56 2003 +0000

description:
Replace home-grown locks with lockmgr().

diffstat:

 sys/dev/ata/wd.c    |  69 +++++++++-------------------------------------------
 sys/dev/ata/wdvar.h |   5 +--
 sys/dev/isa/mcd.c   |  61 +++++++++-------------------------------------
 3 files changed, 27 insertions(+), 108 deletions(-)

diffs (truncated from 345 to 300 lines):

diff -r 6827afad6e73 -r 35984ccfcab1 sys/dev/ata/wd.c
--- a/sys/dev/ata/wd.c  Fri Nov 07 03:58:06 2003 +0000
+++ b/sys/dev/ata/wd.c  Fri Nov 07 04:10:56 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: wd.c,v 1.266 2003/10/29 21:27:38 mycroft Exp $ */
+/*     $NetBSD: wd.c,v 1.267 2003/11/07 04:10:56 mycroft Exp $ */
 
 /*
  * Copyright (c) 1998, 2001 Manuel Bouyer.  All rights reserved.
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.266 2003/10/29 21:27:38 mycroft Exp $");
+__KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.267 2003/11/07 04:10:56 mycroft Exp $");
 
 #ifndef WDCDEBUG
 #define WDCDEBUG
@@ -201,8 +201,6 @@
 #ifdef HAS_BAD144_HANDLING
 static void bad144intern(struct wd_softc *);
 #endif
-int    wdlock(struct wd_softc *);
-void   wdunlock(struct wd_softc *);
 
 #define        WD_QUIRK_SPLIT_MOD15_WRITE      0x0001  /* must split certain writes */
 
@@ -278,6 +276,8 @@
        const struct wd_quirk *wdq;
        WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
 
+       lockinit(&wd->sc_lock, PRIBIO | PCATCH, "wdlock", 0, 0);
+
        callout_init(&wd->sc_restart_ch);
 #ifdef NEW_BUFQ_STRATEGY
        bufq_alloc(&wd->sc_q, BUFQ_READ_PRIO|BUFQ_SORT_RAWBLOCK);
@@ -420,6 +420,8 @@
        struct buf *bp;
        int s, bmaj, cmaj, i, mn;
 
+       lockmgr(&sc->sc_lock, LK_DRAIN, NULL);
+
        /* Clean out the bad sector list */
        while (!SLIST_EMPTY(&sc->sc_bslist)) {
                void *head = SLIST_FIRST(&sc->sc_bslist);
@@ -843,51 +845,6 @@
        return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
 }
 
-/*
- * Wait interruptibly for an exclusive lock.
- *
- * XXX
- * Several drivers do this; it should be abstracted and made MP-safe.
- */
-int
-wdlock(struct wd_softc *wd)
-{
-       int error;
-       int s;
-
-       WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
-
-       s = splbio();
-
-       while ((wd->sc_flags & WDF_LOCKED) != 0) {
-               wd->sc_flags |= WDF_WANTED;
-               if ((error = tsleep(wd, PRIBIO | PCATCH,
-                   "wdlck", 0)) != 0) {
-                       splx(s);
-                       return error;
-               }
-       }
-       wd->sc_flags |= WDF_LOCKED;
-       splx(s);
-       return 0;
-}
-
-/*
- * Unlock and wake up any waiters.
- */
-void
-wdunlock(struct wd_softc *wd)
-{
-
-       WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
-
-       wd->sc_flags &= ~WDF_LOCKED;
-       if ((wd->sc_flags & WDF_WANTED) != 0) {
-               wd->sc_flags &= ~WDF_WANTED;
-               wakeup(wd);
-       }
-}
-
 int
 wdopen(dev_t dev, int flag, int fmt, struct proc *p)
 {
@@ -907,7 +864,7 @@
            (error = wd->atabus->ata_addref(wd->drvp)) != 0)
                return (error);
 
-       if ((error = wdlock(wd)) != 0)
+       if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
                goto bad4;
 
        if (wd->sc_dk.dk_openmask != 0) {
@@ -953,7 +910,7 @@
        wd->sc_dk.dk_openmask =
            wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
 
-       wdunlock(wd);
+       lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
        return 0;
 
 bad:
@@ -961,7 +918,7 @@
        }
 
 bad3:
-       wdunlock(wd);
+       lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
 bad4:
        if (wd->sc_dk.dk_openmask == 0)
                wd->atabus->ata_delref(wd->drvp);
@@ -976,7 +933,7 @@
        int error;
 
        WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
-       if ((error = wdlock(wd)) != 0)
+       if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
                return error;
 
        switch (fmt) {
@@ -1000,7 +957,7 @@
                wd->atabus->ata_delref(wd->drvp);
        }
 
-       wdunlock(wd);
+       lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
        return 0;
 }
 
@@ -1256,7 +1213,7 @@
 #endif
                lp = (struct disklabel *)addr;
 
-               if ((error = wdlock(wd)) != 0)
+               if ((error = lockmgr(&wd->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
                        goto bad;
                wd->sc_flags |= WDF_LABELLING;
 
@@ -1277,7 +1234,7 @@
                }
 
                wd->sc_flags &= ~WDF_LABELLING;
-               wdunlock(wd);
+               lockmgr(&wd->sc_lock, LK_RELEASE, NULL);
 bad:
 #ifdef __HAVE_OLD_DISKLABEL
                if (newlabel != NULL)
diff -r 6827afad6e73 -r 35984ccfcab1 sys/dev/ata/wdvar.h
--- a/sys/dev/ata/wdvar.h       Fri Nov 07 03:58:06 2003 +0000
+++ b/sys/dev/ata/wdvar.h       Fri Nov 07 04:10:56 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: wdvar.h,v 1.20 2003/11/02 09:52:33 wiz Exp $   */
+/*     $NetBSD: wdvar.h,v 1.21 2003/11/07 04:10:56 mycroft Exp $       */
 
 /*
  * Copyright (c) 1998, 2001 Manuel Bouyer.
@@ -102,6 +102,7 @@
        /* General disk infos */
        struct device sc_dev;
        struct disk sc_dk;
+       struct lock sc_lock;
        struct bufq_state sc_q;
        struct callout sc_restart_ch;
        int sc_quirks;                  /* any quirks drive might have */
@@ -113,8 +114,6 @@
        int openings;
        struct ataparams sc_params;/* drive characteristics found */
        int sc_flags;     
-#define        WDF_LOCKED      0x001
-#define        WDF_WANTED      0x002
 #define        WDF_WLABEL      0x004 /* label is writable */
 #define        WDF_LABELLING   0x008 /* writing label */
 /*
diff -r 6827afad6e73 -r 35984ccfcab1 sys/dev/isa/mcd.c
--- a/sys/dev/isa/mcd.c Fri Nov 07 03:58:06 2003 +0000
+++ b/sys/dev/isa/mcd.c Fri Nov 07 04:10:56 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mcd.c,v 1.84 2003/06/29 22:30:20 fvdl Exp $    */
+/*     $NetBSD: mcd.c,v 1.85 2003/11/07 04:10:57 mycroft Exp $ */
 
 /*
  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.  All rights reserved.
@@ -56,7 +56,7 @@
 /*static char COPYRIGHT[] = "mcd-driver (C)1993 by H.Veit & B.Moore";*/
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mcd.c,v 1.84 2003/06/29 22:30:20 fvdl Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mcd.c,v 1.85 2003/11/07 04:10:57 mycroft Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -121,6 +121,7 @@
 struct mcd_softc {
        struct  device sc_dev;
        struct  disk sc_dk;
+       struct  lock sc_lock;
        void *sc_ih;
 
        struct callout sc_pintr_ch;
@@ -132,8 +133,6 @@
 
        char    *type;
        int     flags;
-#define        MCDF_LOCKED     0x01
-#define        MCDF_WANTED     0x02
 #define        MCDF_WLABEL     0x04    /* label is writable */
 #define        MCDF_LABELLING  0x08    /* writing label */
 #define        MCDF_LOADED     0x10    /* parameters loaded */
@@ -218,8 +217,6 @@
 void   mcdgetdisklabel __P((struct mcd_softc *));
 int    mcd_get_parms __P((struct mcd_softc *));
 void   mcdstart __P((struct mcd_softc *));
-int    mcdlock __P((struct mcd_softc *));
-void   mcdunlock __P((struct mcd_softc *));
 void   mcd_pseudointr __P((void *));
 
 struct dkdriver mcddkdriver = { mcdstrategy };
@@ -251,6 +248,8 @@
                return;
        }
 
+       lockinit(&sc->sc_lock, PRIBIO | PCATCH, "mcdlock", 0, 0);
+
        sc->sc_iot = iot;
        sc->sc_ioh = ioh;
 
@@ -289,42 +288,6 @@
            IST_EDGE, IPL_BIO, mcdintr, sc);
 }
 
-/*
- * Wait interruptibly for an exclusive lock.
- *
- * XXX
- * Several drivers do this; it should be abstracted and made MP-safe.
- */
-int
-mcdlock(sc)
-       struct mcd_softc *sc;
-{
-       int error;
-
-       while ((sc->flags & MCDF_LOCKED) != 0) {
-               sc->flags |= MCDF_WANTED;
-               if ((error = tsleep(sc, PRIBIO | PCATCH, "mcdlck", 0)) != 0)
-                       return error;
-       }
-       sc->flags |= MCDF_LOCKED;
-       return 0;
-}
-
-/*
- * Unlock and wake up any waiters.
- */
-void
-mcdunlock(sc)
-       struct mcd_softc *sc;
-{
-
-       sc->flags &= ~MCDF_LOCKED;
-       if ((sc->flags & MCDF_WANTED) != 0) {
-               sc->flags &= ~MCDF_WANTED;
-               wakeup(sc);
-       }
-}
-
 int
 mcdopen(dev, flag, fmt, p)
        dev_t dev;
@@ -338,7 +301,7 @@
        if (sc == NULL)
                return ENXIO;
 
-       if ((error = mcdlock(sc)) != 0)
+       if ((error = lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
                return error;
 
        if (sc->sc_dk.dk_openmask != 0) {
@@ -408,7 +371,7 @@
        }
        sc->sc_dk.dk_openmask = sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
 
-       mcdunlock(sc);
+       lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
        return 0;
 
 bad2:



Home | Main Index | Thread Index | Old Index