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