Home | History | Annotate | Line # | Download | only in kernspace
workqueue.c revision 1.6.2.2
      1  1.6.2.2  martin /*	$NetBSD: workqueue.c,v 1.6.2.2 2018/01/16 13:01:10 martin Exp $	*/
      2  1.6.2.2  martin 
      3  1.6.2.2  martin /*-
      4  1.6.2.2  martin  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      5  1.6.2.2  martin  * All rights reserved.
      6  1.6.2.2  martin  *
      7  1.6.2.2  martin  * Redistribution and use in source and binary forms, with or without
      8  1.6.2.2  martin  * modification, are permitted provided that the following conditions
      9  1.6.2.2  martin  * are met:
     10  1.6.2.2  martin  * 1. Redistributions of source code must retain the above copyright
     11  1.6.2.2  martin  *    notice, this list of conditions and the following disclaimer.
     12  1.6.2.2  martin  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.6.2.2  martin  *    notice, this list of conditions and the following disclaimer in the
     14  1.6.2.2  martin  *    documentation and/or other materials provided with the distribution.
     15  1.6.2.2  martin  *
     16  1.6.2.2  martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17  1.6.2.2  martin  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  1.6.2.2  martin  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  1.6.2.2  martin  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.6.2.2  martin  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21  1.6.2.2  martin  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.6.2.2  martin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  1.6.2.2  martin  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.6.2.2  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  1.6.2.2  martin  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.6.2.2  martin  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  1.6.2.2  martin  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.6.2.2  martin  */
     29  1.6.2.2  martin 
     30  1.6.2.2  martin #include <sys/cdefs.h>
     31  1.6.2.2  martin #if !defined(lint)
     32  1.6.2.2  martin __RCSID("$NetBSD: workqueue.c,v 1.6.2.2 2018/01/16 13:01:10 martin Exp $");
     33  1.6.2.2  martin #endif /* !lint */
     34  1.6.2.2  martin 
     35  1.6.2.2  martin #include <sys/param.h>
     36  1.6.2.2  martin #include <sys/condvar.h>
     37  1.6.2.2  martin #include <sys/kernel.h>
     38  1.6.2.2  martin #include <sys/kmem.h>
     39  1.6.2.2  martin #include <sys/kthread.h>
     40  1.6.2.2  martin #include <sys/mutex.h>
     41  1.6.2.2  martin #include <sys/workqueue.h>
     42  1.6.2.2  martin 
     43  1.6.2.2  martin #include "kernspace.h"
     44  1.6.2.2  martin 
     45  1.6.2.2  martin struct test_softc {
     46  1.6.2.2  martin 	kmutex_t mtx;
     47  1.6.2.2  martin 	kcondvar_t cv;
     48  1.6.2.2  martin 	struct workqueue *wq;
     49  1.6.2.2  martin 	struct work wk;
     50  1.6.2.2  martin 	int counter;
     51  1.6.2.2  martin };
     52  1.6.2.2  martin 
     53  1.6.2.2  martin static void
     54  1.6.2.2  martin rump_work1(struct work *wk, void *arg)
     55  1.6.2.2  martin {
     56  1.6.2.2  martin 	struct test_softc *sc = arg;
     57  1.6.2.2  martin 
     58  1.6.2.2  martin 	mutex_enter(&sc->mtx);
     59  1.6.2.2  martin 	++sc->counter;
     60  1.6.2.2  martin 	cv_broadcast(&sc->cv);
     61  1.6.2.2  martin 	mutex_exit(&sc->mtx);
     62  1.6.2.2  martin }
     63  1.6.2.2  martin 
     64  1.6.2.2  martin static struct test_softc *
     65  1.6.2.2  martin create_sc(void)
     66  1.6.2.2  martin {
     67  1.6.2.2  martin 	int rv;
     68  1.6.2.2  martin 	struct test_softc *sc;
     69  1.6.2.2  martin 
     70  1.6.2.2  martin 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
     71  1.6.2.2  martin 	mutex_init(&sc->mtx, MUTEX_DEFAULT, IPL_NONE);
     72  1.6.2.2  martin 	cv_init(&sc->cv, "rumpwqcv");
     73  1.6.2.2  martin 	rv = workqueue_create(&sc->wq, "rumpwq",
     74  1.6.2.2  martin 	    rump_work1, sc, PRI_SOFTNET, IPL_SOFTNET, 0);
     75  1.6.2.2  martin 	if (rv)
     76  1.6.2.2  martin 		panic("workqueue creation failed: %d", rv);
     77  1.6.2.2  martin 
     78  1.6.2.2  martin 	sc->counter = 0;
     79  1.6.2.2  martin 
     80  1.6.2.2  martin 	return sc;
     81  1.6.2.2  martin }
     82  1.6.2.2  martin 
     83  1.6.2.2  martin static void
     84  1.6.2.2  martin destroy_sc(struct test_softc *sc)
     85  1.6.2.2  martin {
     86  1.6.2.2  martin 
     87  1.6.2.2  martin 	cv_destroy(&sc->cv);
     88  1.6.2.2  martin 	mutex_destroy(&sc->mtx);
     89  1.6.2.2  martin 	workqueue_destroy(sc->wq);
     90  1.6.2.2  martin }
     91  1.6.2.2  martin 
     92  1.6.2.2  martin void
     93  1.6.2.2  martin rumptest_workqueue1()
     94  1.6.2.2  martin {
     95  1.6.2.2  martin 	struct test_softc *sc;
     96  1.6.2.2  martin 
     97  1.6.2.2  martin 	sc = create_sc();
     98  1.6.2.2  martin 
     99  1.6.2.2  martin #define ITERATIONS 12435
    100  1.6.2.2  martin 	for (int i = 0; i < ITERATIONS; ++i) {
    101  1.6.2.2  martin 		int e;
    102  1.6.2.2  martin 		mutex_enter(&sc->mtx);
    103  1.6.2.2  martin 		workqueue_enqueue(sc->wq, &sc->wk, NULL);
    104  1.6.2.2  martin 		e = cv_timedwait(&sc->cv, &sc->mtx, hz * 2);
    105  1.6.2.2  martin 		if (e != 0)
    106  1.6.2.2  martin 			panic("cv_timedwait timed out (i=%d)", i);
    107  1.6.2.2  martin 		mutex_exit(&sc->mtx);
    108  1.6.2.2  martin 	}
    109  1.6.2.2  martin 
    110  1.6.2.2  martin 	KASSERT(sc->counter == ITERATIONS);
    111  1.6.2.2  martin 
    112  1.6.2.2  martin 	destroy_sc(sc);
    113  1.6.2.2  martin #undef ITERATIONS
    114  1.6.2.2  martin }
    115  1.6.2.2  martin 
    116  1.6.2.2  martin void
    117  1.6.2.2  martin rumptest_workqueue_wait(void)
    118  1.6.2.2  martin {
    119  1.6.2.2  martin 	struct test_softc *sc;
    120  1.6.2.2  martin 	struct work dummy;
    121  1.6.2.2  martin 
    122  1.6.2.2  martin 	sc = create_sc();
    123  1.6.2.2  martin 
    124  1.6.2.2  martin #define ITERATIONS 12435
    125  1.6.2.2  martin 	for (size_t i = 0; i < ITERATIONS; ++i) {
    126  1.6.2.2  martin 		KASSERT(sc->counter == i);
    127  1.6.2.2  martin 		workqueue_enqueue(sc->wq, &sc->wk, NULL);
    128  1.6.2.2  martin 		workqueue_wait(sc->wq, &sc->wk);
    129  1.6.2.2  martin 		KASSERT(sc->counter == (i + 1));
    130  1.6.2.2  martin 	}
    131  1.6.2.2  martin 
    132  1.6.2.2  martin 	KASSERT(sc->counter == ITERATIONS);
    133  1.6.2.2  martin 
    134  1.6.2.2  martin 	/* Wait for a work that is not enqueued. Just return immediately. */
    135  1.6.2.2  martin 	workqueue_wait(sc->wq, &dummy);
    136  1.6.2.2  martin 
    137  1.6.2.2  martin 	destroy_sc(sc);
    138  1.6.2.2  martin #undef ITERATIONS
    139  1.6.2.2  martin }
    140