xref: /netbsd-src/tests/rump/kernspace/workqueue.c (revision aef814ffa9ed9b7280264cd46f87376120c0d3b7)
1 /*	$NetBSD: workqueue.c,v 1.1 2017/09/29 12:42:36 maya 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.1 2017/09/29 12:42:36 maya 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 		workqueue_enqueue(sc->wq, &sc->wk, NULL);
87 		mutex_enter(&sc->mtx);
88 		cv_timedwait(&sc->cv, &sc->mtx, 2);
89 		mutex_exit(&sc->mtx);
90 	}
91 
92 	KASSERT(sc->counter == ITERATIONS);
93 
94 	cv_destroy(&sc->cv);
95 	mutex_destroy(&sc->mtx);
96 	workqueue_destroy(sc->wq);
97 }
98 
99