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