Home | History | Annotate | Line # | Download | only in kernspace
workqueue.c revision 1.3
      1 /*	$NetBSD: workqueue.c,v 1.3 2017/12/28 04:38:02 ozaki-r 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.3 2017/12/28 04:38:02 ozaki-r 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 	mutex_enter(&sc->mtx);
     59 	++sc->counter;
     60 	cv_broadcast(&sc->cv);
     61 	mutex_exit(&sc->mtx);
     62 }
     63 
     64 void
     65 rumptest_workqueue1()
     66 {
     67 
     68 	int rv;
     69 
     70 	struct test_softc *sc;
     71 
     72 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
     73 
     74 	mutex_init(&sc->mtx, MUTEX_DEFAULT, IPL_NONE);
     75 	cv_init(&sc->cv, "rumpwqcv");
     76 
     77 	rv = workqueue_create(&sc->wq, "rumpwq",
     78 	    rump_work1, sc, PRI_SOFTNET, IPL_SOFTNET, 0);
     79 	if (rv)
     80 		panic("workqueue creation failed: %d", rv);
     81 
     82 	sc->counter = 0;
     83 
     84 #define ITERATIONS 12435
     85 	for (size_t i = 0; i < ITERATIONS; ++i) {
     86 		int e;
     87 		mutex_enter(&sc->mtx);
     88 		workqueue_enqueue(sc->wq, &sc->wk, NULL);
     89 		e = cv_timedwait(&sc->cv, &sc->mtx, hz * 2);
     90 		if (e != 0)
     91 			panic("cv_timedwait timed out (i=%lu)", i);
     92 		mutex_exit(&sc->mtx);
     93 	}
     94 
     95 	KASSERT(sc->counter == ITERATIONS);
     96 
     97 	cv_destroy(&sc->cv);
     98 	mutex_destroy(&sc->mtx);
     99 	workqueue_destroy(sc->wq);
    100 }
    101 
    102