threadpool.c revision 1.2 1 1.2 thorpej /* $NetBSD: threadpool.c,v 1.2 2018/12/25 21:26:31 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
20 1.1 thorpej * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 1.1 thorpej * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 1.1 thorpej * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 thorpej * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
24 1.1 thorpej * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 1.1 thorpej * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28 1.1 thorpej * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 1.1 thorpej * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30 1.1 thorpej * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 thorpej */
32 1.1 thorpej
33 1.1 thorpej #include <sys/cdefs.h>
34 1.1 thorpej #if !defined(lint)
35 1.2 thorpej __RCSID("$NetBSD: threadpool.c,v 1.2 2018/12/25 21:26:31 thorpej Exp $");
36 1.1 thorpej #endif /* !lint */
37 1.1 thorpej
38 1.1 thorpej #include <sys/param.h>
39 1.1 thorpej #include <sys/condvar.h>
40 1.1 thorpej #include <sys/kernel.h>
41 1.1 thorpej #include <sys/kmem.h>
42 1.1 thorpej #include <sys/mutex.h>
43 1.1 thorpej #include <sys/threadpool.h>
44 1.1 thorpej
45 1.1 thorpej #include "kernspace.h"
46 1.1 thorpej
47 1.1 thorpej void
48 1.1 thorpej rumptest_threadpool_unbound_lifecycle(void)
49 1.1 thorpej {
50 1.1 thorpej threadpool_t *pool0, *pool1, *pool2;
51 1.1 thorpej int error;
52 1.1 thorpej
53 1.1 thorpej error = threadpool_get(&pool0, PRI_NONE);
54 1.1 thorpej KASSERT(error == 0);
55 1.1 thorpej
56 1.1 thorpej error = threadpool_get(&pool1, PRI_NONE);
57 1.1 thorpej KASSERT(error == 0);
58 1.1 thorpej
59 1.1 thorpej KASSERT(pool0 == pool1);
60 1.1 thorpej
61 1.1 thorpej error = threadpool_get(&pool2, PRI_KERNEL_RT);
62 1.1 thorpej KASSERT(error == 0);
63 1.1 thorpej
64 1.1 thorpej KASSERT(pool0 != pool2);
65 1.1 thorpej
66 1.1 thorpej threadpool_put(pool0, PRI_NONE);
67 1.1 thorpej threadpool_put(pool1, PRI_NONE);
68 1.1 thorpej threadpool_put(pool2, PRI_KERNEL_RT);
69 1.1 thorpej }
70 1.1 thorpej
71 1.1 thorpej void
72 1.1 thorpej rumptest_threadpool_percpu_lifecycle(void)
73 1.1 thorpej {
74 1.1 thorpej threadpool_percpu_t *pcpu0, *pcpu1, *pcpu2;
75 1.1 thorpej int error;
76 1.1 thorpej
77 1.1 thorpej error = threadpool_percpu_get(&pcpu0, PRI_NONE);
78 1.1 thorpej KASSERT(error == 0);
79 1.1 thorpej
80 1.1 thorpej error = threadpool_percpu_get(&pcpu1, PRI_NONE);
81 1.1 thorpej KASSERT(error == 0);
82 1.1 thorpej
83 1.1 thorpej KASSERT(pcpu0 == pcpu1);
84 1.1 thorpej
85 1.1 thorpej error = threadpool_percpu_get(&pcpu2, PRI_KERNEL_RT);
86 1.1 thorpej KASSERT(error == 0);
87 1.1 thorpej
88 1.1 thorpej KASSERT(pcpu0 != pcpu2);
89 1.1 thorpej
90 1.1 thorpej threadpool_percpu_put(pcpu0, PRI_NONE);
91 1.1 thorpej threadpool_percpu_put(pcpu1, PRI_NONE);
92 1.1 thorpej threadpool_percpu_put(pcpu2, PRI_KERNEL_RT);
93 1.1 thorpej }
94 1.1 thorpej
95 1.1 thorpej struct test_job_data {
96 1.1 thorpej kmutex_t mutex;
97 1.1 thorpej kcondvar_t cond;
98 1.1 thorpej unsigned int count;
99 1.1 thorpej threadpool_job_t job;
100 1.1 thorpej };
101 1.1 thorpej
102 1.1 thorpej #define FINAL_COUNT 12345
103 1.1 thorpej
104 1.1 thorpej static void
105 1.1 thorpej test_job_func_schedule(threadpool_job_t *job)
106 1.1 thorpej {
107 1.1 thorpej struct test_job_data *data =
108 1.1 thorpej container_of(job, struct test_job_data, job);
109 1.1 thorpej
110 1.1 thorpej mutex_enter(&data->mutex);
111 1.1 thorpej KASSERT(data->count != FINAL_COUNT);
112 1.1 thorpej data->count++;
113 1.1 thorpej cv_broadcast(&data->cond);
114 1.1 thorpej threadpool_job_done(job);
115 1.1 thorpej mutex_exit(&data->mutex);
116 1.1 thorpej }
117 1.1 thorpej
118 1.1 thorpej static void
119 1.1 thorpej test_job_func_cancel(threadpool_job_t *job)
120 1.1 thorpej {
121 1.1 thorpej struct test_job_data *data =
122 1.1 thorpej container_of(job, struct test_job_data, job);
123 1.1 thorpej
124 1.1 thorpej mutex_enter(&data->mutex);
125 1.1 thorpej data->count = 1;
126 1.1 thorpej cv_broadcast(&data->cond);
127 1.1 thorpej while (data->count != FINAL_COUNT - 1)
128 1.1 thorpej cv_wait(&data->cond, &data->mutex);
129 1.1 thorpej data->count = FINAL_COUNT;
130 1.1 thorpej cv_broadcast(&data->cond);
131 1.1 thorpej threadpool_job_done(job);
132 1.1 thorpej mutex_exit(&data->mutex);
133 1.1 thorpej }
134 1.1 thorpej
135 1.1 thorpej static void
136 1.1 thorpej init_test_job_data(struct test_job_data *data, threadpool_job_fn_t fn)
137 1.1 thorpej {
138 1.1 thorpej mutex_init(&data->mutex, MUTEX_DEFAULT, IPL_NONE);
139 1.1 thorpej cv_init(&data->cond, "testjob");
140 1.1 thorpej threadpool_job_init(&data->job, fn, &data->mutex, "testjob");
141 1.1 thorpej data->count = 0;
142 1.1 thorpej }
143 1.1 thorpej
144 1.1 thorpej static void
145 1.1 thorpej fini_test_job_data(struct test_job_data *data)
146 1.1 thorpej {
147 1.1 thorpej threadpool_job_destroy(&data->job);
148 1.1 thorpej cv_destroy(&data->cond);
149 1.1 thorpej mutex_destroy(&data->mutex);
150 1.1 thorpej }
151 1.1 thorpej
152 1.1 thorpej void
153 1.1 thorpej rumptest_threadpool_unbound_schedule(void)
154 1.1 thorpej {
155 1.1 thorpej struct test_job_data data;
156 1.1 thorpej threadpool_t *pool;
157 1.1 thorpej int error;
158 1.1 thorpej
159 1.1 thorpej error = threadpool_get(&pool, PRI_NONE);
160 1.1 thorpej KASSERT(error == 0);
161 1.1 thorpej
162 1.1 thorpej init_test_job_data(&data, test_job_func_schedule);
163 1.1 thorpej
164 1.1 thorpej mutex_enter(&data.mutex);
165 1.1 thorpej while (data.count != FINAL_COUNT) {
166 1.1 thorpej threadpool_schedule_job(pool, &data.job);
167 1.1 thorpej error = cv_timedwait(&data.cond, &data.mutex, hz * 2);
168 1.1 thorpej KASSERT(error != EWOULDBLOCK);
169 1.1 thorpej }
170 1.1 thorpej mutex_exit(&data.mutex);
171 1.1 thorpej
172 1.1 thorpej fini_test_job_data(&data);
173 1.1 thorpej
174 1.1 thorpej threadpool_put(pool, PRI_NONE);
175 1.1 thorpej }
176 1.1 thorpej
177 1.1 thorpej void
178 1.1 thorpej rumptest_threadpool_percpu_schedule(void)
179 1.1 thorpej {
180 1.1 thorpej struct test_job_data data;
181 1.1 thorpej threadpool_percpu_t *pcpu;
182 1.1 thorpej threadpool_t *pool;
183 1.1 thorpej int error;
184 1.1 thorpej
185 1.1 thorpej error = threadpool_percpu_get(&pcpu, PRI_NONE);
186 1.1 thorpej KASSERT(error == 0);
187 1.1 thorpej
188 1.1 thorpej pool = threadpool_percpu_ref(pcpu);
189 1.1 thorpej
190 1.1 thorpej init_test_job_data(&data, test_job_func_schedule);
191 1.1 thorpej
192 1.1 thorpej mutex_enter(&data.mutex);
193 1.1 thorpej while (data.count != FINAL_COUNT) {
194 1.1 thorpej threadpool_schedule_job(pool, &data.job);
195 1.1 thorpej error = cv_timedwait(&data.cond, &data.mutex, hz * 2);
196 1.1 thorpej KASSERT(error != EWOULDBLOCK);
197 1.1 thorpej }
198 1.1 thorpej mutex_exit(&data.mutex);
199 1.1 thorpej
200 1.1 thorpej fini_test_job_data(&data);
201 1.1 thorpej
202 1.1 thorpej threadpool_percpu_put(pcpu, PRI_NONE);
203 1.1 thorpej }
204 1.1 thorpej
205 1.1 thorpej void
206 1.1 thorpej rumptest_threadpool_job_cancel(void)
207 1.1 thorpej {
208 1.1 thorpej struct test_job_data data;
209 1.1 thorpej threadpool_t *pool;
210 1.1 thorpej int error;
211 1.1 thorpej bool rv;
212 1.1 thorpej
213 1.1 thorpej error = threadpool_get(&pool, PRI_NONE);
214 1.1 thorpej KASSERT(error == 0);
215 1.1 thorpej
216 1.1 thorpej init_test_job_data(&data, test_job_func_cancel);
217 1.1 thorpej
218 1.1 thorpej mutex_enter(&data.mutex);
219 1.1 thorpej threadpool_schedule_job(pool, &data.job);
220 1.1 thorpej while (data.count == 0)
221 1.1 thorpej cv_wait(&data.cond, &data.mutex);
222 1.1 thorpej KASSERT(data.count == 1);
223 1.1 thorpej
224 1.1 thorpej /* Job is already running (and is not finished); this shold fail. */
225 1.1 thorpej rv = threadpool_cancel_job_async(pool, &data.job);
226 1.1 thorpej KASSERT(rv == false);
227 1.1 thorpej
228 1.1 thorpej data.count = FINAL_COUNT - 1;
229 1.1 thorpej cv_broadcast(&data.cond);
230 1.1 thorpej
231 1.1 thorpej /* Now wait for the job to finish. */
232 1.1 thorpej threadpool_cancel_job(pool, &data.job);
233 1.1 thorpej KASSERT(data.count == FINAL_COUNT);
234 1.2 thorpej mutex_exit(&data.mutex);
235 1.2 thorpej
236 1.2 thorpej fini_test_job_data(&data);
237 1.2 thorpej
238 1.2 thorpej threadpool_put(pool, PRI_NONE);
239 1.1 thorpej }
240