xref: /netbsd/tests/rump/kernspace/tsleep.c (revision 6550d01e)
1 /*	$NetBSD: tsleep.c,v 1.1 2010/05/31 23:32:51 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 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: tsleep.c,v 1.1 2010/05/31 23:32:51 pooka Exp $");
33 #endif /* !lint */
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/proc.h>
39 #include <sys/simplelock.h>
40 
41 #include "kernspace.h"
42 
43 #define NTHREADS 10
44 
45 /*
46  * kernel lock is interlock
47  */
48 static void
49 bigthread(void *arg)
50 {
51 	static int wakeups;
52 	struct simplelock slock;
53 	int i;
54 
55 	simple_lock_init(&slock);
56 	for (i = 0; i < 1000; i++) {
57 		wakeup(bigthread);
58 		if (wakeups >= NTHREADS-1)
59 			break;
60 		if (arg) {
61 			simple_lock(&slock);
62 			ltsleep(bigthread, PNORELOCK, "hii", 0, &slock);
63 		} else {
64 			tsleep(bigthread, 0, "hii", 0);
65 		}
66 	}
67 
68 	wakeup(bigthread);
69 	wakeups++;
70 
71 	kthread_exit(0);
72 }
73 
74 /*
75  * mpsafe thread.  need dedicated interlock
76  */
77 static kmutex_t mymtx;
78 
79 static void
80 tinythread(void *arg)
81 {
82 	static int wakeups;
83 	int i, rv;
84 	bool relock = ((uintptr_t)arg % 2) == 0;
85 
86 	for (i = 0; i < 1000; i++) {
87 		mutex_enter(&mymtx);
88 		wakeup(tinythread);
89 		if (wakeups >= NTHREADS-1) {
90 			mutex_exit(&mymtx);
91 			break;
92 		}
93 		rv = mtsleep(tinythread, relock ? 0 : PNORELOCK,
94 		    "haa", 0, &mymtx);
95 		if (relock)
96 			mutex_exit(&mymtx);
97 		if (rv != 0)
98 			panic("mtsleep failed");
99 	}
100 
101 	mutex_enter(&mymtx);
102 	wakeups++;
103 	wakeup(tinythread);
104 
105 	rv = mtsleep(rumptest_tsleep, PNORELOCK, "kepuli", 1, &mymtx);
106 	if (rv != EWOULDBLOCK)
107 		panic("mtsleep unexpected return value %d", rv);
108 
109 	kthread_exit(0);
110 }
111 
112 void
113 rumptest_tsleep()
114 {
115 	struct lwp *bigl[NTHREADS];
116 	struct lwp *notbigl[NTHREADS];
117 	int rv, i;
118 
119 	for (i = 0; i < NTHREADS; i++) {
120 		rv = kthread_create(PRI_NONE, KTHREAD_JOINABLE,
121 		    NULL, bigthread, (void *)(uintptr_t)i, &bigl[i], "b");
122 		if (rv)
123 			panic("thread create failed: %d", rv);
124 	}
125 
126 	mutex_init(&mymtx, MUTEX_DEFAULT, IPL_NONE);
127 
128 	for (i = 0; i < NTHREADS; i++) {
129 		rv = kthread_create(PRI_NONE, KTHREAD_JOINABLE | KTHREAD_MPSAFE,
130 		    NULL, tinythread, (void *)(uintptr_t)i, &notbigl[i], "nb");
131 		if (rv)
132 			panic("thread create failed: %d", rv);
133 	}
134 
135 	for (i = 0; i < NTHREADS; i++) {
136 		kthread_join(bigl[i]);
137 	}
138 
139 	for (i = 0; i < NTHREADS; i++) {
140 		kthread_join(notbigl[i]);
141 	}
142 }
143