t_mutex.c revision 1.15 1 1.15 christos /* $NetBSD: t_mutex.c,v 1.15 2017/01/16 16:23:41 christos Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1 jmmv * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * Redistribution and use in source and binary forms, with or without
8 1.1 jmmv * modification, are permitted provided that the following conditions
9 1.1 jmmv * are met:
10 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
11 1.1 jmmv * notice, this list of conditions and the following disclaimer.
12 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
14 1.1 jmmv * documentation and/or other materials provided with the distribution.
15 1.1 jmmv *
16 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmmv */
28 1.1 jmmv
29 1.1 jmmv #include <sys/cdefs.h>
30 1.1 jmmv __COPYRIGHT("@(#) Copyright (c) 2008\
31 1.1 jmmv The NetBSD Foundation, inc. All rights reserved.");
32 1.15 christos __RCSID("$NetBSD: t_mutex.c,v 1.15 2017/01/16 16:23:41 christos Exp $");
33 1.1 jmmv
34 1.15 christos #include <sys/time.h> /* For timespecadd */
35 1.15 christos #include <inttypes.h> /* For UINT16_MAX */
36 1.1 jmmv #include <pthread.h>
37 1.1 jmmv #include <stdio.h>
38 1.4 riz #include <string.h>
39 1.8 christos #include <errno.h>
40 1.12 christos #include <time.h>
41 1.1 jmmv #include <unistd.h>
42 1.8 christos #include <sys/sched.h>
43 1.8 christos #include <sys/param.h>
44 1.1 jmmv
45 1.1 jmmv #include <atf-c.h>
46 1.1 jmmv
47 1.1 jmmv #include "h_common.h"
48 1.1 jmmv
49 1.1 jmmv static pthread_mutex_t mutex;
50 1.3 jmmv static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
51 1.1 jmmv static int global_x;
52 1.1 jmmv
53 1.12 christos #ifdef TIMEDMUTEX
54 1.12 christos /* This code is used for verifying non-timed specific code */
55 1.12 christos static struct timespec ts_lengthy = {
56 1.12 christos .tv_sec = UINT16_MAX,
57 1.12 christos .tv_nsec = 0
58 1.12 christos };
59 1.12 christos /* This code is used for verifying timed-only specific code */
60 1.12 christos static struct timespec ts_shortlived = {
61 1.12 christos .tv_sec = 0,
62 1.12 christos .tv_nsec = 120
63 1.12 christos };
64 1.12 christos
65 1.12 christos static int
66 1.12 christos mutex_lock(pthread_mutex_t *m, const struct timespec *ts)
67 1.12 christos {
68 1.12 christos struct timespec ts_wait;
69 1.12 christos ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ts_wait) != -1);
70 1.12 christos timespecadd(&ts_wait, ts, &ts_wait);
71 1.12 christos
72 1.12 christos return pthread_mutex_timedlock(m, &ts_wait);
73 1.12 christos }
74 1.12 christos #else
75 1.12 christos #define mutex_lock(a, b) pthread_mutex_lock(a)
76 1.12 christos #endif
77 1.12 christos
78 1.1 jmmv static void *
79 1.1 jmmv mutex1_threadfunc(void *arg)
80 1.1 jmmv {
81 1.1 jmmv int *param;
82 1.1 jmmv
83 1.1 jmmv printf("2: Second thread.\n");
84 1.1 jmmv
85 1.1 jmmv param = arg;
86 1.1 jmmv printf("2: Locking mutex\n");
87 1.12 christos mutex_lock(&mutex, &ts_lengthy);
88 1.1 jmmv printf("2: Got mutex. *param = %d\n", *param);
89 1.1 jmmv ATF_REQUIRE_EQ(*param, 20);
90 1.1 jmmv (*param)++;
91 1.1 jmmv
92 1.1 jmmv pthread_mutex_unlock(&mutex);
93 1.1 jmmv
94 1.1 jmmv return param;
95 1.1 jmmv }
96 1.1 jmmv
97 1.1 jmmv ATF_TC(mutex1);
98 1.1 jmmv ATF_TC_HEAD(mutex1, tc)
99 1.1 jmmv {
100 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Checks mutexes");
101 1.1 jmmv }
102 1.1 jmmv ATF_TC_BODY(mutex1, tc)
103 1.1 jmmv {
104 1.1 jmmv int x;
105 1.1 jmmv pthread_t new;
106 1.1 jmmv void *joinval;
107 1.1 jmmv
108 1.1 jmmv printf("1: Mutex-test 1\n");
109 1.1 jmmv
110 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
111 1.1 jmmv x = 1;
112 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
113 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x));
114 1.1 jmmv printf("1: Before changing the value.\n");
115 1.1 jmmv sleep(2);
116 1.1 jmmv x = 20;
117 1.1 jmmv printf("1: Before releasing the mutex.\n");
118 1.1 jmmv sleep(2);
119 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
120 1.1 jmmv printf("1: After releasing the mutex.\n");
121 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
122 1.1 jmmv
123 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
124 1.1 jmmv printf("1: Thread joined. X was %d. Return value (int) was %d\n",
125 1.1 jmmv x, *(int *)joinval);
126 1.1 jmmv ATF_REQUIRE_EQ(x, 21);
127 1.1 jmmv ATF_REQUIRE_EQ(*(int *)joinval, 21);
128 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
129 1.1 jmmv }
130 1.1 jmmv
131 1.1 jmmv static void *
132 1.1 jmmv mutex2_threadfunc(void *arg)
133 1.1 jmmv {
134 1.1 jmmv long count = *(int *)arg;
135 1.1 jmmv
136 1.1 jmmv printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
137 1.1 jmmv
138 1.1 jmmv while (count--) {
139 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
140 1.1 jmmv global_x++;
141 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
142 1.1 jmmv }
143 1.1 jmmv
144 1.1 jmmv return (void *)count;
145 1.1 jmmv }
146 1.1 jmmv
147 1.1 jmmv ATF_TC(mutex2);
148 1.1 jmmv ATF_TC_HEAD(mutex2, tc)
149 1.1 jmmv {
150 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Checks mutexes");
151 1.6 jmmv #if defined(__powerpc__)
152 1.6 jmmv atf_tc_set_md_var(tc, "timeout", "40");
153 1.6 jmmv #endif
154 1.1 jmmv }
155 1.1 jmmv ATF_TC_BODY(mutex2, tc)
156 1.1 jmmv {
157 1.1 jmmv int count, count2;
158 1.1 jmmv pthread_t new;
159 1.1 jmmv void *joinval;
160 1.1 jmmv
161 1.1 jmmv printf("1: Mutex-test 2\n");
162 1.1 jmmv
163 1.6 jmmv #if defined(__powerpc__)
164 1.6 jmmv atf_tc_expect_timeout("PR port-powerpc/44387");
165 1.6 jmmv #endif
166 1.4 riz
167 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
168 1.4 riz
169 1.1 jmmv global_x = 0;
170 1.1 jmmv count = count2 = 10000000;
171 1.1 jmmv
172 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
173 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
174 1.1 jmmv
175 1.1 jmmv printf("1: Thread %p\n", pthread_self());
176 1.1 jmmv
177 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
178 1.1 jmmv
179 1.1 jmmv while (count--) {
180 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
181 1.1 jmmv global_x++;
182 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
183 1.1 jmmv }
184 1.1 jmmv
185 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
186 1.1 jmmv
187 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
188 1.1 jmmv printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
189 1.1 jmmv global_x, (long)joinval);
190 1.1 jmmv ATF_REQUIRE_EQ(global_x, 20000000);
191 1.4 riz
192 1.6 jmmv #if defined(__powerpc__)
193 1.4 riz /* XXX force a timeout in ppc case since an un-triggered race
194 1.4 riz otherwise looks like a "failure" */
195 1.6 jmmv /* We sleep for longer than the timeout to make ATF not
196 1.6 jmmv complain about unexpected success */
197 1.6 jmmv sleep(41);
198 1.6 jmmv #endif
199 1.1 jmmv }
200 1.1 jmmv
201 1.1 jmmv static void *
202 1.1 jmmv mutex3_threadfunc(void *arg)
203 1.1 jmmv {
204 1.1 jmmv long count = *(int *)arg;
205 1.1 jmmv
206 1.1 jmmv printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
207 1.1 jmmv
208 1.1 jmmv while (count--) {
209 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
210 1.1 jmmv global_x++;
211 1.3 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
212 1.1 jmmv }
213 1.1 jmmv
214 1.1 jmmv return (void *)count;
215 1.1 jmmv }
216 1.1 jmmv
217 1.1 jmmv ATF_TC(mutex3);
218 1.1 jmmv ATF_TC_HEAD(mutex3, tc)
219 1.1 jmmv {
220 1.3 jmmv atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
221 1.3 jmmv "initializer");
222 1.6 jmmv #if defined(__powerpc__)
223 1.6 jmmv atf_tc_set_md_var(tc, "timeout", "40");
224 1.6 jmmv #endif
225 1.1 jmmv }
226 1.1 jmmv ATF_TC_BODY(mutex3, tc)
227 1.1 jmmv {
228 1.1 jmmv int count, count2;
229 1.1 jmmv pthread_t new;
230 1.1 jmmv void *joinval;
231 1.1 jmmv
232 1.1 jmmv printf("1: Mutex-test 3\n");
233 1.1 jmmv
234 1.6 jmmv #if defined(__powerpc__)
235 1.6 jmmv atf_tc_expect_timeout("PR port-powerpc/44387");
236 1.6 jmmv #endif
237 1.4 riz
238 1.1 jmmv global_x = 0;
239 1.1 jmmv count = count2 = 10000000;
240 1.1 jmmv
241 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
242 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
243 1.1 jmmv
244 1.1 jmmv printf("1: Thread %p\n", pthread_self());
245 1.3 jmmv
246 1.3 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
247 1.1 jmmv
248 1.1 jmmv while (count--) {
249 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
250 1.1 jmmv global_x++;
251 1.3 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
252 1.1 jmmv }
253 1.1 jmmv
254 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
255 1.1 jmmv
256 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
257 1.1 jmmv printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
258 1.1 jmmv global_x, (long)joinval);
259 1.1 jmmv ATF_REQUIRE_EQ(global_x, 20000000);
260 1.4 riz
261 1.6 jmmv #if defined(__powerpc__)
262 1.4 riz /* XXX force a timeout in ppc case since an un-triggered race
263 1.4 riz otherwise looks like a "failure" */
264 1.6 jmmv /* We sleep for longer than the timeout to make ATF not
265 1.6 jmmv complain about unexpected success */
266 1.6 jmmv sleep(41);
267 1.6 jmmv #endif
268 1.1 jmmv }
269 1.1 jmmv
270 1.1 jmmv static void *
271 1.1 jmmv mutex4_threadfunc(void *arg)
272 1.1 jmmv {
273 1.1 jmmv int *param;
274 1.1 jmmv
275 1.1 jmmv printf("2: Second thread.\n");
276 1.1 jmmv
277 1.1 jmmv param = arg;
278 1.1 jmmv printf("2: Locking mutex\n");
279 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
280 1.1 jmmv printf("2: Got mutex. *param = %d\n", *param);
281 1.1 jmmv (*param)++;
282 1.1 jmmv
283 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
284 1.1 jmmv
285 1.1 jmmv return param;
286 1.1 jmmv }
287 1.1 jmmv
288 1.1 jmmv ATF_TC(mutex4);
289 1.1 jmmv ATF_TC_HEAD(mutex4, tc)
290 1.1 jmmv {
291 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Checks mutexes");
292 1.1 jmmv }
293 1.1 jmmv ATF_TC_BODY(mutex4, tc)
294 1.1 jmmv {
295 1.1 jmmv int x;
296 1.1 jmmv pthread_t new;
297 1.1 jmmv pthread_mutexattr_t mattr;
298 1.1 jmmv void *joinval;
299 1.1 jmmv
300 1.1 jmmv printf("1: Mutex-test 4\n");
301 1.1 jmmv
302 1.1 jmmv PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
303 1.1 jmmv PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
304 1.1 jmmv
305 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
306 1.1 jmmv
307 1.1 jmmv PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
308 1.1 jmmv
309 1.1 jmmv x = 1;
310 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
311 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
312 1.1 jmmv
313 1.1 jmmv printf("1: Before recursively acquiring the mutex.\n");
314 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
315 1.1 jmmv
316 1.1 jmmv printf("1: Before releasing the mutex once.\n");
317 1.1 jmmv sleep(2);
318 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
319 1.1 jmmv printf("1: After releasing the mutex once.\n");
320 1.1 jmmv
321 1.1 jmmv x = 20;
322 1.1 jmmv
323 1.1 jmmv printf("1: Before releasing the mutex twice.\n");
324 1.1 jmmv sleep(2);
325 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
326 1.1 jmmv printf("1: After releasing the mutex twice.\n");
327 1.1 jmmv
328 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
329 1.1 jmmv
330 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
331 1.1 jmmv printf("1: Thread joined. X was %d. Return value (int) was %d\n",
332 1.1 jmmv x, *(int *)joinval);
333 1.1 jmmv ATF_REQUIRE_EQ(x, 21);
334 1.1 jmmv ATF_REQUIRE_EQ(*(int *)joinval, 21);
335 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
336 1.1 jmmv }
337 1.1 jmmv
338 1.8 christos static pthread_mutexattr_t attr5;
339 1.8 christos static pthread_mutex_t mutex5;
340 1.8 christos static int min_fifo_prio, max_fifo_prio;
341 1.8 christos
342 1.8 christos static void *
343 1.8 christos child_func(void* arg)
344 1.8 christos {
345 1.8 christos int res;
346 1.8 christos
347 1.8 christos printf("child is waiting\n");
348 1.8 christos res = _sched_protect(-2);
349 1.10 christos ATF_REQUIRE_EQ_MSG(res, -1, "sched_protect returned %d", res);
350 1.8 christos ATF_REQUIRE_EQ(errno, ENOENT);
351 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
352 1.8 christos printf("child is owning resource\n");
353 1.8 christos res = _sched_protect(-2);
354 1.8 christos ATF_REQUIRE_EQ(res, max_fifo_prio);
355 1.8 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
356 1.8 christos printf("child is done\n");
357 1.8 christos
358 1.8 christos return 0;
359 1.8 christos }
360 1.8 christos
361 1.8 christos ATF_TC(mutex5);
362 1.8 christos ATF_TC_HEAD(mutex5, tc)
363 1.8 christos {
364 1.8 christos atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting");
365 1.10 christos atf_tc_set_md_var(tc, "require.user", "root");
366 1.8 christos }
367 1.8 christos
368 1.8 christos ATF_TC_BODY(mutex5, tc)
369 1.8 christos {
370 1.8 christos int res;
371 1.8 christos struct sched_param param;
372 1.8 christos pthread_t child;
373 1.8 christos
374 1.8 christos min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
375 1.8 christos max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
376 1.8 christos printf("min prio for FIFO = %d\n", min_fifo_prio);
377 1.8 christos param.sched_priority = min_fifo_prio;
378 1.9 christos
379 1.8 christos /* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
380 1.8 christos res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
381 1.8 christos printf("previous policy used = %d\n", res);
382 1.8 christos
383 1.8 christos res = sched_getscheduler(getpid());
384 1.10 christos ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res,
385 1.10 christos SCHED_FIFO);
386 1.8 christos
387 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5));
388 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5,
389 1.8 christos PTHREAD_PRIO_PROTECT));
390 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5,
391 1.8 christos max_fifo_prio));
392 1.8 christos
393 1.8 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
394 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
395 1.8 christos printf("enter critical section for main\n");
396 1.8 christos PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
397 1.8 christos printf("main starts to sleep\n");
398 1.8 christos sleep(10);
399 1.8 christos printf("main completes\n");
400 1.8 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
401 1.8 christos PTHREAD_REQUIRE(pthread_join(child, NULL));
402 1.8 christos }
403 1.8 christos
404 1.8 christos static pthread_mutex_t mutex6;
405 1.8 christos static int start = 0;
406 1.8 christos static uintmax_t high_cnt = 0, low_cnt = 0, MAX_LOOP = 100000000;
407 1.8 christos
408 1.8 christos static void *
409 1.8 christos high_prio(void* arg)
410 1.8 christos {
411 1.8 christos struct sched_param param;
412 1.8 christos int policy;
413 1.8 christos param.sched_priority = min_fifo_prio + 10;
414 1.8 christos pthread_t childid = pthread_self();
415 1.8 christos
416 1.8 christos PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
417 1.8 christos PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
418 1.8 christos printf("high protect = %d, prio = %d\n",
419 1.8 christos _sched_protect(-2), param.sched_priority);
420 1.8 christos ATF_REQUIRE_EQ(policy, 1);
421 1.8 christos printf("high prio = %d\n", param.sched_priority);
422 1.8 christos sleep(1);
423 1.8 christos long tmp = 0;
424 1.9 christos for (int i = 0; i < 20; i++) {
425 1.8 christos while (high_cnt < MAX_LOOP) {
426 1.8 christos tmp += (123456789 % 1234) * (987654321 % 54321);
427 1.8 christos high_cnt += 1;
428 1.8 christos }
429 1.8 christos high_cnt = 0;
430 1.8 christos sleep(1);
431 1.8 christos }
432 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
433 1.8 christos if (start == 0) start = 2;
434 1.8 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
435 1.8 christos
436 1.8 christos return 0;
437 1.8 christos }
438 1.8 christos
439 1.8 christos static void *
440 1.8 christos low_prio(void* arg)
441 1.8 christos {
442 1.8 christos struct sched_param param;
443 1.8 christos int policy;
444 1.8 christos param.sched_priority = min_fifo_prio;
445 1.8 christos pthread_t childid = pthread_self();
446 1.8 christos int res = _sched_protect(max_fifo_prio);
447 1.8 christos ATF_REQUIRE_EQ(res, 0);
448 1.8 christos PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
449 1.8 christos PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
450 1.8 christos printf("low protect = %d, prio = %d\n", _sched_protect(-2),
451 1.8 christos param.sched_priority);
452 1.8 christos ATF_REQUIRE_EQ(policy, 1);
453 1.8 christos printf("low prio = %d\n", param.sched_priority);
454 1.8 christos sleep(1);
455 1.9 christos long tmp = 0;
456 1.8 christos for (int i = 0; i < 20; i++) {
457 1.8 christos while (low_cnt < MAX_LOOP) {
458 1.8 christos tmp += (123456789 % 1234) * (987654321 % 54321);
459 1.8 christos low_cnt += 1;
460 1.8 christos }
461 1.8 christos low_cnt = 0;
462 1.8 christos sleep(1);
463 1.8 christos }
464 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
465 1.8 christos if (start == 0)
466 1.8 christos start = 1;
467 1.8 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
468 1.8 christos
469 1.8 christos return 0;
470 1.8 christos }
471 1.8 christos
472 1.8 christos ATF_TC(mutex6);
473 1.8 christos ATF_TC_HEAD(mutex6, tc)
474 1.8 christos {
475 1.8 christos atf_tc_set_md_var(tc, "descr",
476 1.8 christos "Checks scheduling for priority ceiling");
477 1.10 christos atf_tc_set_md_var(tc, "require.user", "root");
478 1.8 christos }
479 1.8 christos
480 1.8 christos /*
481 1.8 christos * 1. main thread sets itself to be a realtime task and launched two tasks,
482 1.8 christos * one has higher priority and the other has lower priority.
483 1.8 christos * 2. each child thread(low and high priority thread) sets its scheduler and
484 1.8 christos * priority.
485 1.8 christos * 3. each child thread did several rounds of computation, after each round it
486 1.8 christos * sleep 1 second.
487 1.8 christos * 4. the child thread with low priority will call _sched_protect to increase
488 1.8 christos * its protect priority.
489 1.8 christos * 5. We verify the thread with low priority runs first.
490 1.8 christos *
491 1.8 christos * Why does it work? From the main thread, we launched the high
492 1.8 christos * priority thread first. This gives this thread the benefit of
493 1.8 christos * starting first. The low priority thread did not call _sched_protect(2).
494 1.8 christos * The high priority thread should finish the task first. After each
495 1.8 christos * round of computation, we call sleep, to put the task into the
496 1.8 christos * sleep queue, and wake up again after the timer expires. This
497 1.8 christos * gives the scheduler the chance to decide which task to run. So,
498 1.8 christos * the thread with real high priority will always block the thread
499 1.8 christos * with real low priority.
500 1.8 christos *
501 1.8 christos */
502 1.8 christos ATF_TC_BODY(mutex6, tc)
503 1.8 christos {
504 1.9 christos struct sched_param param;
505 1.8 christos int res;
506 1.8 christos pthread_t high, low;
507 1.9 christos
508 1.8 christos min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
509 1.8 christos max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
510 1.8 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
511 1.8 christos printf("min_fifo_prio = %d, max_fifo_info = %d\n", min_fifo_prio,
512 1.8 christos max_fifo_prio);
513 1.9 christos
514 1.8 christos param.sched_priority = min_fifo_prio;
515 1.8 christos res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
516 1.8 christos printf("previous policy used = %d\n", res);
517 1.8 christos
518 1.8 christos res = sched_getscheduler(getpid());
519 1.8 christos ATF_REQUIRE_EQ(res, 1);
520 1.8 christos PTHREAD_REQUIRE(pthread_create(&high, NULL, high_prio, NULL));
521 1.8 christos PTHREAD_REQUIRE(pthread_create(&low, NULL, low_prio, NULL));
522 1.8 christos sleep(5);
523 1.8 christos PTHREAD_REQUIRE(pthread_join(low, NULL));
524 1.8 christos PTHREAD_REQUIRE(pthread_join(high, NULL));
525 1.8 christos
526 1.8 christos ATF_REQUIRE_EQ(start, 1);
527 1.8 christos }
528 1.8 christos
529 1.8 christos ATF_TC(mutexattr1);
530 1.8 christos ATF_TC_HEAD(mutexattr1, tc)
531 1.8 christos {
532 1.8 christos atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
533 1.8 christos }
534 1.8 christos
535 1.8 christos ATF_TC_BODY(mutexattr1, tc)
536 1.8 christos {
537 1.8 christos pthread_mutexattr_t mattr;
538 1.8 christos int protocol, target;
539 1.8 christos
540 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
541 1.8 christos
542 1.8 christos target = PTHREAD_PRIO_NONE;
543 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
544 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
545 1.8 christos ATF_REQUIRE_EQ(protocol, target);
546 1.8 christos
547 1.8 christos /*
548 1.8 christos target = PTHREAD_PRIO_INHERIT;
549 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
550 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
551 1.8 christos ATF_REQUIRE_EQ(protocol, target);
552 1.8 christos */
553 1.8 christos
554 1.8 christos target = PTHREAD_PRIO_PROTECT;
555 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
556 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
557 1.8 christos ATF_REQUIRE_EQ(protocol, target);
558 1.8 christos }
559 1.8 christos
560 1.8 christos ATF_TC(mutexattr2);
561 1.8 christos ATF_TC_HEAD(mutexattr2, tc)
562 1.8 christos {
563 1.8 christos atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
564 1.8 christos }
565 1.8 christos
566 1.8 christos ATF_TC_BODY(mutexattr2, tc)
567 1.8 christos {
568 1.8 christos pthread_mutexattr_t mattr;
569 1.8 christos
570 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
571 1.8 christos int max_prio = sched_get_priority_max(SCHED_FIFO);
572 1.8 christos int min_prio = sched_get_priority_min(SCHED_FIFO);
573 1.8 christos for (int i = min_prio; i <= max_prio; i++) {
574 1.8 christos int prioceiling;
575 1.15 christos int protocol;
576 1.15 christos
577 1.15 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr,
578 1.15 christos &protocol));
579 1.15 christos
580 1.15 christos printf("priority: %d\nprotocol: %d\n", i, protocol);
581 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
582 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
583 1.8 christos &prioceiling));
584 1.15 christos printf("prioceiling: %d\n", prioceiling);
585 1.8 christos ATF_REQUIRE_EQ(i, prioceiling);
586 1.8 christos }
587 1.8 christos }
588 1.8 christos
589 1.12 christos #ifdef TIMEDMUTEX
590 1.12 christos ATF_TC(timedmutex1);
591 1.12 christos ATF_TC_HEAD(timedmutex1, tc)
592 1.12 christos {
593 1.12 christos atf_tc_set_md_var(tc, "descr", "Checks timeout on selflock");
594 1.12 christos }
595 1.12 christos
596 1.12 christos ATF_TC_BODY(timedmutex1, tc)
597 1.12 christos {
598 1.12 christos
599 1.12 christos printf("Timed mutex-test 1\n");
600 1.12 christos
601 1.12 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
602 1.12 christos
603 1.14 christos printf("Before acquiring mutex\n");
604 1.12 christos PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
605 1.12 christos
606 1.12 christos printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
607 1.12 christos PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
608 1.12 christos ETIMEDOUT);
609 1.12 christos
610 1.14 christos printf("Unlocking mutex\n");
611 1.12 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
612 1.12 christos }
613 1.12 christos
614 1.12 christos ATF_TC(timedmutex2);
615 1.12 christos ATF_TC_HEAD(timedmutex2, tc)
616 1.12 christos {
617 1.12 christos atf_tc_set_md_var(tc, "descr",
618 1.12 christos "Checks timeout on selflock with timedlock");
619 1.12 christos }
620 1.12 christos
621 1.12 christos ATF_TC_BODY(timedmutex2, tc)
622 1.12 christos {
623 1.12 christos
624 1.13 christos printf("Timed mutex-test 2\n");
625 1.12 christos
626 1.12 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
627 1.12 christos
628 1.14 christos printf("Before acquiring mutex with timedlock\n");
629 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
630 1.12 christos
631 1.13 christos printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
632 1.12 christos PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
633 1.12 christos ETIMEDOUT);
634 1.12 christos
635 1.14 christos printf("Unlocking mutex\n");
636 1.14 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
637 1.14 christos }
638 1.14 christos
639 1.14 christos ATF_TC(timedmutex3);
640 1.14 christos ATF_TC_HEAD(timedmutex3, tc)
641 1.14 christos {
642 1.14 christos atf_tc_set_md_var(tc, "descr",
643 1.14 christos "Checks timeout on selflock in a new thread");
644 1.14 christos }
645 1.14 christos
646 1.14 christos static void *
647 1.14 christos timedmtx_thrdfunc(void *arg)
648 1.14 christos {
649 1.14 christos printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
650 1.14 christos PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
651 1.14 christos ETIMEDOUT);
652 1.14 christos
653 1.14 christos return NULL;
654 1.14 christos }
655 1.14 christos
656 1.14 christos ATF_TC_BODY(timedmutex3, tc)
657 1.14 christos {
658 1.14 christos pthread_t new;
659 1.14 christos
660 1.14 christos printf("Timed mutex-test 3\n");
661 1.14 christos
662 1.14 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
663 1.14 christos
664 1.14 christos printf("Before acquiring mutex with timedlock\n");
665 1.14 christos PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
666 1.14 christos
667 1.14 christos printf("Before creating new thread\n");
668 1.14 christos PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
669 1.14 christos
670 1.14 christos printf("Before joining the mutex\n");
671 1.14 christos PTHREAD_REQUIRE(pthread_join(new, NULL));
672 1.14 christos
673 1.14 christos printf("Unlocking mutex\n");
674 1.14 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
675 1.14 christos }
676 1.14 christos
677 1.14 christos ATF_TC(timedmutex4);
678 1.14 christos ATF_TC_HEAD(timedmutex4, tc)
679 1.14 christos {
680 1.14 christos atf_tc_set_md_var(tc, "descr",
681 1.14 christos "Checks timeout on selflock with timedlock in a new thread");
682 1.14 christos }
683 1.14 christos
684 1.14 christos ATF_TC_BODY(timedmutex4, tc)
685 1.14 christos {
686 1.14 christos pthread_t new;
687 1.14 christos
688 1.14 christos printf("Timed mutex-test 4\n");
689 1.14 christos
690 1.14 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
691 1.14 christos
692 1.14 christos printf("Before acquiring mutex with timedlock\n");
693 1.14 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
694 1.14 christos
695 1.14 christos printf("Before creating new thread\n");
696 1.14 christos PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
697 1.14 christos
698 1.14 christos printf("Before joining the mutex\n");
699 1.14 christos PTHREAD_REQUIRE(pthread_join(new, NULL));
700 1.14 christos
701 1.14 christos printf("Unlocking mutex\n");
702 1.12 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
703 1.12 christos }
704 1.12 christos #endif
705 1.12 christos
706 1.1 jmmv ATF_TP_ADD_TCS(tp)
707 1.1 jmmv {
708 1.1 jmmv ATF_TP_ADD_TC(tp, mutex1);
709 1.1 jmmv ATF_TP_ADD_TC(tp, mutex2);
710 1.1 jmmv ATF_TP_ADD_TC(tp, mutex3);
711 1.1 jmmv ATF_TP_ADD_TC(tp, mutex4);
712 1.8 christos ATF_TP_ADD_TC(tp, mutex5);
713 1.8 christos ATF_TP_ADD_TC(tp, mutex6);
714 1.8 christos ATF_TP_ADD_TC(tp, mutexattr1);
715 1.8 christos ATF_TP_ADD_TC(tp, mutexattr2);
716 1.12 christos
717 1.12 christos #ifdef TIMEDMUTEX
718 1.12 christos ATF_TP_ADD_TC(tp, timedmutex1);
719 1.12 christos ATF_TP_ADD_TC(tp, timedmutex2);
720 1.14 christos ATF_TP_ADD_TC(tp, timedmutex3);
721 1.14 christos ATF_TP_ADD_TC(tp, timedmutex4);
722 1.12 christos #endif
723 1.12 christos
724 1.1 jmmv return atf_no_error();
725 1.1 jmmv }
726