Subject: Synchronization with locks in kernel
To: None <tech-kern@netbsd.org>
From: Ian Zagorskih <ianzag@megasignal.com>
List: tech-kern
Date: 06/22/2003 20:07:38
Hi all,

Inside my driver I would like to block client process until hardware inte=
rrupt=20
is armed. Pseudo-code is like this:

--------
struct dev_softc {
    struct device sc_dev;
    struct some_lock sc_lock;
};

void dev_interrupt_handler(void *aux) {
    struct dev_softc *sc =3D aux;

    /* Do update lock to unblock [possibly] waiting client process */
    update_lock(&sc->sc_lock);

    /* Clear interrupt request */
    dev_clear_interrupt(sc);
}

/* Kernel side function called by client to do something */
dev_do_something() {
    struct dev_softc *sc =3D device_lookup().

    /* Here we write some device registers which will raise interrupt */
    dev_request_interrupt(sc);

    /* Here we probably doing something else (Point A) */
    print("tralala\n");

    /* Now we are waiting for interrupt */
    wait_for_lock(&sc->sc_lock);

    /* Handle interrupt */
    /* ...... */
}
--------

So i want to block inside client's code waiting for some external asynchr=
onous=20
event [hardware interrupt in my case].

I have tried to do this with tsleep()/wakeup() synchronization calls like=
=20
this:

--------
void dev_interrupt_handler(void *aux) {
    struct dev_softc *sc =3D aux;

    wakeup(sc);
}

dev_do_something() {
    struct dev_softc *sc =3D device_lookup().

    dev_request_interrupt(sc);    /* 1 */
    print("tralala\n");           /* 2 */
    tsleep(sc);                   /* 3 */
}
--------

=2E..but [obviously] i do loose interrupts when interrupt is occured fast=
 enough=20
i.e. between points 1 and 3 so tsleep(sc) is blocked too late and for=20
nothing. Well, it's supposed to be so course this scheme is stateless.

So as far as i understand, i must use some scheme that would count lock's=
=20
hold/release calls and keep reference counter. Simple locks and managed l=
ocks=20
seems to be the exact thing what i need but ATM i don't understand how to=
 use=20
them :( It's not POSIX or some other well known standard [well, same as a=
ll=20
kernel though] and documentaiton is a bit weird for me. Of course possibl=
y=20
that's just my English's fault and POSIX background.

Can you please give me some simple example how to use locks ? If it fits =
in my=20
case it would be great !

Thank you.

------
JSC Novosibirsk Geophysical Equipment Development Center.
WEB=09http://www.megasignal.com