t_mutex.c revision 1.17 1 1.17 martin /* $NetBSD: t_mutex.c,v 1.17 2017/03/23 08:31:00 martin 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.17 martin __RCSID("$NetBSD: t_mutex.c,v 1.17 2017/03/23 08:31:00 martin 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.1 jmmv }
152 1.1 jmmv ATF_TC_BODY(mutex2, tc)
153 1.1 jmmv {
154 1.1 jmmv int count, count2;
155 1.1 jmmv pthread_t new;
156 1.1 jmmv void *joinval;
157 1.1 jmmv
158 1.1 jmmv printf("1: Mutex-test 2\n");
159 1.1 jmmv
160 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
161 1.4 riz
162 1.1 jmmv global_x = 0;
163 1.1 jmmv count = count2 = 10000000;
164 1.1 jmmv
165 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
166 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
167 1.1 jmmv
168 1.1 jmmv printf("1: Thread %p\n", pthread_self());
169 1.1 jmmv
170 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
171 1.1 jmmv
172 1.1 jmmv while (count--) {
173 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
174 1.1 jmmv global_x++;
175 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
176 1.1 jmmv }
177 1.1 jmmv
178 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
179 1.1 jmmv
180 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
181 1.1 jmmv printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
182 1.1 jmmv global_x, (long)joinval);
183 1.1 jmmv ATF_REQUIRE_EQ(global_x, 20000000);
184 1.1 jmmv }
185 1.1 jmmv
186 1.1 jmmv static void *
187 1.1 jmmv mutex3_threadfunc(void *arg)
188 1.1 jmmv {
189 1.1 jmmv long count = *(int *)arg;
190 1.1 jmmv
191 1.1 jmmv printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
192 1.1 jmmv
193 1.1 jmmv while (count--) {
194 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
195 1.1 jmmv global_x++;
196 1.3 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
197 1.1 jmmv }
198 1.1 jmmv
199 1.1 jmmv return (void *)count;
200 1.1 jmmv }
201 1.1 jmmv
202 1.1 jmmv ATF_TC(mutex3);
203 1.1 jmmv ATF_TC_HEAD(mutex3, tc)
204 1.1 jmmv {
205 1.3 jmmv atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
206 1.3 jmmv "initializer");
207 1.1 jmmv }
208 1.1 jmmv ATF_TC_BODY(mutex3, tc)
209 1.1 jmmv {
210 1.1 jmmv int count, count2;
211 1.1 jmmv pthread_t new;
212 1.1 jmmv void *joinval;
213 1.1 jmmv
214 1.1 jmmv printf("1: Mutex-test 3\n");
215 1.1 jmmv
216 1.1 jmmv global_x = 0;
217 1.1 jmmv count = count2 = 10000000;
218 1.1 jmmv
219 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
220 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
221 1.1 jmmv
222 1.1 jmmv printf("1: Thread %p\n", pthread_self());
223 1.3 jmmv
224 1.3 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
225 1.1 jmmv
226 1.1 jmmv while (count--) {
227 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
228 1.1 jmmv global_x++;
229 1.3 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
230 1.1 jmmv }
231 1.1 jmmv
232 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
233 1.1 jmmv
234 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
235 1.1 jmmv printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
236 1.1 jmmv global_x, (long)joinval);
237 1.1 jmmv ATF_REQUIRE_EQ(global_x, 20000000);
238 1.1 jmmv }
239 1.1 jmmv
240 1.1 jmmv static void *
241 1.1 jmmv mutex4_threadfunc(void *arg)
242 1.1 jmmv {
243 1.1 jmmv int *param;
244 1.1 jmmv
245 1.1 jmmv printf("2: Second thread.\n");
246 1.1 jmmv
247 1.1 jmmv param = arg;
248 1.1 jmmv printf("2: Locking mutex\n");
249 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
250 1.1 jmmv printf("2: Got mutex. *param = %d\n", *param);
251 1.1 jmmv (*param)++;
252 1.1 jmmv
253 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
254 1.1 jmmv
255 1.1 jmmv return param;
256 1.1 jmmv }
257 1.1 jmmv
258 1.1 jmmv ATF_TC(mutex4);
259 1.1 jmmv ATF_TC_HEAD(mutex4, tc)
260 1.1 jmmv {
261 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Checks mutexes");
262 1.1 jmmv }
263 1.1 jmmv ATF_TC_BODY(mutex4, tc)
264 1.1 jmmv {
265 1.1 jmmv int x;
266 1.1 jmmv pthread_t new;
267 1.1 jmmv pthread_mutexattr_t mattr;
268 1.1 jmmv void *joinval;
269 1.1 jmmv
270 1.1 jmmv printf("1: Mutex-test 4\n");
271 1.1 jmmv
272 1.1 jmmv PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
273 1.1 jmmv PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
274 1.1 jmmv
275 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
276 1.1 jmmv
277 1.1 jmmv PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
278 1.1 jmmv
279 1.1 jmmv x = 1;
280 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
281 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
282 1.1 jmmv
283 1.1 jmmv printf("1: Before recursively acquiring the mutex.\n");
284 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
285 1.1 jmmv
286 1.1 jmmv printf("1: Before releasing the mutex once.\n");
287 1.1 jmmv sleep(2);
288 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
289 1.1 jmmv printf("1: After releasing the mutex once.\n");
290 1.1 jmmv
291 1.1 jmmv x = 20;
292 1.1 jmmv
293 1.1 jmmv printf("1: Before releasing the mutex twice.\n");
294 1.1 jmmv sleep(2);
295 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
296 1.1 jmmv printf("1: After releasing the mutex twice.\n");
297 1.1 jmmv
298 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
299 1.1 jmmv
300 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
301 1.1 jmmv printf("1: Thread joined. X was %d. Return value (int) was %d\n",
302 1.1 jmmv x, *(int *)joinval);
303 1.1 jmmv ATF_REQUIRE_EQ(x, 21);
304 1.1 jmmv ATF_REQUIRE_EQ(*(int *)joinval, 21);
305 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
306 1.1 jmmv }
307 1.1 jmmv
308 1.8 christos static pthread_mutexattr_t attr5;
309 1.8 christos static pthread_mutex_t mutex5;
310 1.8 christos static int min_fifo_prio, max_fifo_prio;
311 1.8 christos
312 1.8 christos static void *
313 1.8 christos child_func(void* arg)
314 1.8 christos {
315 1.8 christos int res;
316 1.8 christos
317 1.8 christos printf("child is waiting\n");
318 1.8 christos res = _sched_protect(-2);
319 1.10 christos ATF_REQUIRE_EQ_MSG(res, -1, "sched_protect returned %d", res);
320 1.8 christos ATF_REQUIRE_EQ(errno, ENOENT);
321 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
322 1.8 christos printf("child is owning resource\n");
323 1.8 christos res = _sched_protect(-2);
324 1.8 christos ATF_REQUIRE_EQ(res, max_fifo_prio);
325 1.8 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
326 1.8 christos printf("child is done\n");
327 1.8 christos
328 1.8 christos return 0;
329 1.8 christos }
330 1.8 christos
331 1.8 christos ATF_TC(mutex5);
332 1.8 christos ATF_TC_HEAD(mutex5, tc)
333 1.8 christos {
334 1.8 christos atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting");
335 1.10 christos atf_tc_set_md_var(tc, "require.user", "root");
336 1.8 christos }
337 1.8 christos
338 1.8 christos ATF_TC_BODY(mutex5, tc)
339 1.8 christos {
340 1.8 christos int res;
341 1.8 christos struct sched_param param;
342 1.8 christos pthread_t child;
343 1.8 christos
344 1.8 christos min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
345 1.8 christos max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
346 1.8 christos printf("min prio for FIFO = %d\n", min_fifo_prio);
347 1.8 christos param.sched_priority = min_fifo_prio;
348 1.9 christos
349 1.8 christos /* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
350 1.8 christos res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
351 1.8 christos printf("previous policy used = %d\n", res);
352 1.8 christos
353 1.8 christos res = sched_getscheduler(getpid());
354 1.10 christos ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res,
355 1.10 christos SCHED_FIFO);
356 1.8 christos
357 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5));
358 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5,
359 1.8 christos PTHREAD_PRIO_PROTECT));
360 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5,
361 1.8 christos max_fifo_prio));
362 1.8 christos
363 1.8 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
364 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
365 1.8 christos printf("enter critical section for main\n");
366 1.8 christos PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
367 1.8 christos printf("main starts to sleep\n");
368 1.8 christos sleep(10);
369 1.8 christos printf("main completes\n");
370 1.8 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
371 1.8 christos PTHREAD_REQUIRE(pthread_join(child, NULL));
372 1.8 christos }
373 1.8 christos
374 1.8 christos static int start = 0;
375 1.8 christos static uintmax_t high_cnt = 0, low_cnt = 0, MAX_LOOP = 100000000;
376 1.8 christos
377 1.8 christos static void *
378 1.8 christos high_prio(void* arg)
379 1.8 christos {
380 1.8 christos struct sched_param param;
381 1.8 christos int policy;
382 1.8 christos param.sched_priority = min_fifo_prio + 10;
383 1.8 christos pthread_t childid = pthread_self();
384 1.8 christos
385 1.8 christos PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
386 1.8 christos PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
387 1.8 christos printf("high protect = %d, prio = %d\n",
388 1.8 christos _sched_protect(-2), param.sched_priority);
389 1.8 christos ATF_REQUIRE_EQ(policy, 1);
390 1.8 christos printf("high prio = %d\n", param.sched_priority);
391 1.8 christos sleep(1);
392 1.8 christos long tmp = 0;
393 1.9 christos for (int i = 0; i < 20; i++) {
394 1.8 christos while (high_cnt < MAX_LOOP) {
395 1.8 christos tmp += (123456789 % 1234) * (987654321 % 54321);
396 1.8 christos high_cnt += 1;
397 1.8 christos }
398 1.8 christos high_cnt = 0;
399 1.8 christos sleep(1);
400 1.8 christos }
401 1.17 martin PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
402 1.8 christos if (start == 0) start = 2;
403 1.17 martin PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
404 1.8 christos
405 1.8 christos return 0;
406 1.8 christos }
407 1.8 christos
408 1.8 christos static void *
409 1.8 christos low_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;
414 1.8 christos pthread_t childid = pthread_self();
415 1.8 christos int res = _sched_protect(max_fifo_prio);
416 1.8 christos ATF_REQUIRE_EQ(res, 0);
417 1.8 christos PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
418 1.8 christos PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
419 1.8 christos printf("low protect = %d, prio = %d\n", _sched_protect(-2),
420 1.8 christos param.sched_priority);
421 1.8 christos ATF_REQUIRE_EQ(policy, 1);
422 1.8 christos printf("low prio = %d\n", param.sched_priority);
423 1.8 christos sleep(1);
424 1.9 christos long tmp = 0;
425 1.8 christos for (int i = 0; i < 20; i++) {
426 1.8 christos while (low_cnt < MAX_LOOP) {
427 1.8 christos tmp += (123456789 % 1234) * (987654321 % 54321);
428 1.8 christos low_cnt += 1;
429 1.8 christos }
430 1.8 christos low_cnt = 0;
431 1.8 christos sleep(1);
432 1.8 christos }
433 1.17 martin PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
434 1.8 christos if (start == 0)
435 1.8 christos start = 1;
436 1.17 martin PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
437 1.8 christos
438 1.8 christos return 0;
439 1.8 christos }
440 1.8 christos
441 1.8 christos ATF_TC(mutex6);
442 1.8 christos ATF_TC_HEAD(mutex6, tc)
443 1.8 christos {
444 1.8 christos atf_tc_set_md_var(tc, "descr",
445 1.8 christos "Checks scheduling for priority ceiling");
446 1.10 christos atf_tc_set_md_var(tc, "require.user", "root");
447 1.8 christos }
448 1.8 christos
449 1.8 christos /*
450 1.8 christos * 1. main thread sets itself to be a realtime task and launched two tasks,
451 1.8 christos * one has higher priority and the other has lower priority.
452 1.8 christos * 2. each child thread(low and high priority thread) sets its scheduler and
453 1.8 christos * priority.
454 1.8 christos * 3. each child thread did several rounds of computation, after each round it
455 1.8 christos * sleep 1 second.
456 1.8 christos * 4. the child thread with low priority will call _sched_protect to increase
457 1.8 christos * its protect priority.
458 1.8 christos * 5. We verify the thread with low priority runs first.
459 1.8 christos *
460 1.8 christos * Why does it work? From the main thread, we launched the high
461 1.8 christos * priority thread first. This gives this thread the benefit of
462 1.8 christos * starting first. The low priority thread did not call _sched_protect(2).
463 1.8 christos * The high priority thread should finish the task first. After each
464 1.8 christos * round of computation, we call sleep, to put the task into the
465 1.8 christos * sleep queue, and wake up again after the timer expires. This
466 1.8 christos * gives the scheduler the chance to decide which task to run. So,
467 1.8 christos * the thread with real high priority will always block the thread
468 1.8 christos * with real low priority.
469 1.8 christos *
470 1.8 christos */
471 1.8 christos ATF_TC_BODY(mutex6, tc)
472 1.8 christos {
473 1.9 christos struct sched_param param;
474 1.8 christos int res;
475 1.8 christos pthread_t high, low;
476 1.9 christos
477 1.8 christos min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
478 1.8 christos max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
479 1.8 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
480 1.8 christos printf("min_fifo_prio = %d, max_fifo_info = %d\n", min_fifo_prio,
481 1.8 christos max_fifo_prio);
482 1.9 christos
483 1.8 christos param.sched_priority = min_fifo_prio;
484 1.8 christos res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
485 1.8 christos printf("previous policy used = %d\n", res);
486 1.8 christos
487 1.8 christos res = sched_getscheduler(getpid());
488 1.8 christos ATF_REQUIRE_EQ(res, 1);
489 1.8 christos PTHREAD_REQUIRE(pthread_create(&high, NULL, high_prio, NULL));
490 1.8 christos PTHREAD_REQUIRE(pthread_create(&low, NULL, low_prio, NULL));
491 1.8 christos sleep(5);
492 1.8 christos PTHREAD_REQUIRE(pthread_join(low, NULL));
493 1.8 christos PTHREAD_REQUIRE(pthread_join(high, NULL));
494 1.8 christos
495 1.8 christos ATF_REQUIRE_EQ(start, 1);
496 1.8 christos }
497 1.8 christos
498 1.8 christos ATF_TC(mutexattr1);
499 1.8 christos ATF_TC_HEAD(mutexattr1, tc)
500 1.8 christos {
501 1.8 christos atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
502 1.8 christos }
503 1.8 christos
504 1.8 christos ATF_TC_BODY(mutexattr1, tc)
505 1.8 christos {
506 1.8 christos pthread_mutexattr_t mattr;
507 1.8 christos int protocol, target;
508 1.8 christos
509 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
510 1.8 christos
511 1.8 christos target = PTHREAD_PRIO_NONE;
512 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
513 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
514 1.8 christos ATF_REQUIRE_EQ(protocol, target);
515 1.8 christos
516 1.8 christos /*
517 1.8 christos target = PTHREAD_PRIO_INHERIT;
518 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
519 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
520 1.8 christos ATF_REQUIRE_EQ(protocol, target);
521 1.8 christos */
522 1.8 christos
523 1.8 christos target = PTHREAD_PRIO_PROTECT;
524 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
525 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
526 1.8 christos ATF_REQUIRE_EQ(protocol, target);
527 1.8 christos }
528 1.8 christos
529 1.8 christos ATF_TC(mutexattr2);
530 1.8 christos ATF_TC_HEAD(mutexattr2, 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(mutexattr2, tc)
536 1.8 christos {
537 1.8 christos pthread_mutexattr_t mattr;
538 1.8 christos
539 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
540 1.8 christos int max_prio = sched_get_priority_max(SCHED_FIFO);
541 1.8 christos int min_prio = sched_get_priority_min(SCHED_FIFO);
542 1.8 christos for (int i = min_prio; i <= max_prio; i++) {
543 1.8 christos int prioceiling;
544 1.15 christos int protocol;
545 1.15 christos
546 1.15 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr,
547 1.15 christos &protocol));
548 1.15 christos
549 1.15 christos printf("priority: %d\nprotocol: %d\n", i, protocol);
550 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
551 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
552 1.8 christos &prioceiling));
553 1.15 christos printf("prioceiling: %d\n", prioceiling);
554 1.8 christos ATF_REQUIRE_EQ(i, prioceiling);
555 1.8 christos }
556 1.8 christos }
557 1.8 christos
558 1.12 christos #ifdef TIMEDMUTEX
559 1.12 christos ATF_TC(timedmutex1);
560 1.12 christos ATF_TC_HEAD(timedmutex1, tc)
561 1.12 christos {
562 1.12 christos atf_tc_set_md_var(tc, "descr", "Checks timeout on selflock");
563 1.12 christos }
564 1.12 christos
565 1.12 christos ATF_TC_BODY(timedmutex1, tc)
566 1.12 christos {
567 1.12 christos
568 1.12 christos printf("Timed mutex-test 1\n");
569 1.12 christos
570 1.12 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
571 1.12 christos
572 1.14 christos printf("Before acquiring mutex\n");
573 1.12 christos PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
574 1.12 christos
575 1.12 christos printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
576 1.12 christos PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
577 1.12 christos ETIMEDOUT);
578 1.12 christos
579 1.14 christos printf("Unlocking mutex\n");
580 1.12 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
581 1.12 christos }
582 1.12 christos
583 1.12 christos ATF_TC(timedmutex2);
584 1.12 christos ATF_TC_HEAD(timedmutex2, tc)
585 1.12 christos {
586 1.12 christos atf_tc_set_md_var(tc, "descr",
587 1.12 christos "Checks timeout on selflock with timedlock");
588 1.12 christos }
589 1.12 christos
590 1.12 christos ATF_TC_BODY(timedmutex2, tc)
591 1.12 christos {
592 1.12 christos
593 1.13 christos printf("Timed mutex-test 2\n");
594 1.12 christos
595 1.12 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
596 1.12 christos
597 1.14 christos printf("Before acquiring mutex with timedlock\n");
598 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
599 1.12 christos
600 1.13 christos printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
601 1.12 christos PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
602 1.12 christos ETIMEDOUT);
603 1.12 christos
604 1.14 christos printf("Unlocking mutex\n");
605 1.14 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
606 1.14 christos }
607 1.14 christos
608 1.14 christos ATF_TC(timedmutex3);
609 1.14 christos ATF_TC_HEAD(timedmutex3, tc)
610 1.14 christos {
611 1.14 christos atf_tc_set_md_var(tc, "descr",
612 1.14 christos "Checks timeout on selflock in a new thread");
613 1.14 christos }
614 1.14 christos
615 1.14 christos static void *
616 1.14 christos timedmtx_thrdfunc(void *arg)
617 1.14 christos {
618 1.14 christos printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
619 1.14 christos PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
620 1.14 christos ETIMEDOUT);
621 1.14 christos
622 1.14 christos return NULL;
623 1.14 christos }
624 1.14 christos
625 1.14 christos ATF_TC_BODY(timedmutex3, tc)
626 1.14 christos {
627 1.14 christos pthread_t new;
628 1.14 christos
629 1.14 christos printf("Timed mutex-test 3\n");
630 1.14 christos
631 1.14 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
632 1.14 christos
633 1.14 christos printf("Before acquiring mutex with timedlock\n");
634 1.14 christos PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
635 1.14 christos
636 1.14 christos printf("Before creating new thread\n");
637 1.14 christos PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
638 1.14 christos
639 1.14 christos printf("Before joining the mutex\n");
640 1.14 christos PTHREAD_REQUIRE(pthread_join(new, NULL));
641 1.14 christos
642 1.14 christos printf("Unlocking mutex\n");
643 1.14 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
644 1.14 christos }
645 1.14 christos
646 1.14 christos ATF_TC(timedmutex4);
647 1.14 christos ATF_TC_HEAD(timedmutex4, tc)
648 1.14 christos {
649 1.14 christos atf_tc_set_md_var(tc, "descr",
650 1.14 christos "Checks timeout on selflock with timedlock in a new thread");
651 1.14 christos }
652 1.14 christos
653 1.14 christos ATF_TC_BODY(timedmutex4, tc)
654 1.14 christos {
655 1.14 christos pthread_t new;
656 1.14 christos
657 1.14 christos printf("Timed mutex-test 4\n");
658 1.14 christos
659 1.14 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
660 1.14 christos
661 1.14 christos printf("Before acquiring mutex with timedlock\n");
662 1.14 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
663 1.14 christos
664 1.14 christos printf("Before creating new thread\n");
665 1.14 christos PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
666 1.14 christos
667 1.14 christos printf("Before joining the mutex\n");
668 1.14 christos PTHREAD_REQUIRE(pthread_join(new, NULL));
669 1.14 christos
670 1.14 christos printf("Unlocking mutex\n");
671 1.12 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
672 1.12 christos }
673 1.12 christos #endif
674 1.12 christos
675 1.1 jmmv ATF_TP_ADD_TCS(tp)
676 1.1 jmmv {
677 1.1 jmmv ATF_TP_ADD_TC(tp, mutex1);
678 1.1 jmmv ATF_TP_ADD_TC(tp, mutex2);
679 1.1 jmmv ATF_TP_ADD_TC(tp, mutex3);
680 1.1 jmmv ATF_TP_ADD_TC(tp, mutex4);
681 1.8 christos ATF_TP_ADD_TC(tp, mutex5);
682 1.8 christos ATF_TP_ADD_TC(tp, mutex6);
683 1.8 christos ATF_TP_ADD_TC(tp, mutexattr1);
684 1.8 christos ATF_TP_ADD_TC(tp, mutexattr2);
685 1.12 christos
686 1.12 christos #ifdef TIMEDMUTEX
687 1.12 christos ATF_TP_ADD_TC(tp, timedmutex1);
688 1.12 christos ATF_TP_ADD_TC(tp, timedmutex2);
689 1.14 christos ATF_TP_ADD_TC(tp, timedmutex3);
690 1.14 christos ATF_TP_ADD_TC(tp, timedmutex4);
691 1.12 christos #endif
692 1.12 christos
693 1.1 jmmv return atf_no_error();
694 1.1 jmmv }
695