1 /* $NetBSD: workqueue.c,v 1.6 2017/12/28 07:46:34 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.6 2017/12/28 07:46:34 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 static struct test_softc * 65 create_sc(void) 66 { 67 int rv; 68 struct test_softc *sc; 69 70 sc = kmem_zalloc(sizeof(*sc), KM_SLEEP); 71 mutex_init(&sc->mtx, MUTEX_DEFAULT, IPL_NONE); 72 cv_init(&sc->cv, "rumpwqcv"); 73 rv = workqueue_create(&sc->wq, "rumpwq", 74 rump_work1, sc, PRI_SOFTNET, IPL_SOFTNET, 0); 75 if (rv) 76 panic("workqueue creation failed: %d", rv); 77 78 sc->counter = 0; 79 80 return sc; 81 } 82 83 static void 84 destroy_sc(struct test_softc *sc) 85 { 86 87 cv_destroy(&sc->cv); 88 mutex_destroy(&sc->mtx); 89 workqueue_destroy(sc->wq); 90 } 91 92 void 93 rumptest_workqueue1() 94 { 95 struct test_softc *sc; 96 97 sc = create_sc(); 98 99 #define ITERATIONS 12435 100 for (int i = 0; i < ITERATIONS; ++i) { 101 int e; 102 mutex_enter(&sc->mtx); 103 workqueue_enqueue(sc->wq, &sc->wk, NULL); 104 e = cv_timedwait(&sc->cv, &sc->mtx, hz * 2); 105 if (e != 0) 106 panic("cv_timedwait timed out (i=%d)", i); 107 mutex_exit(&sc->mtx); 108 } 109 110 KASSERT(sc->counter == ITERATIONS); 111 112 destroy_sc(sc); 113 #undef ITERATIONS 114 } 115 116 void 117 rumptest_workqueue_wait(void) 118 { 119 struct test_softc *sc; 120 struct work dummy; 121 122 sc = create_sc(); 123 124 #define ITERATIONS 12435 125 for (size_t i = 0; i < ITERATIONS; ++i) { 126 KASSERT(sc->counter == i); 127 workqueue_enqueue(sc->wq, &sc->wk, NULL); 128 workqueue_wait(sc->wq, &sc->wk); 129 KASSERT(sc->counter == (i + 1)); 130 } 131 132 KASSERT(sc->counter == ITERATIONS); 133 134 /* Wait for a work that is not enqueued. Just return immediately. */ 135 workqueue_wait(sc->wq, &dummy); 136 137 destroy_sc(sc); 138 #undef ITERATIONS 139 } 140