Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/test/threadtest.c
2091 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
/*
31
* Thread test code.
32
*/
33
#include <types.h>
34
#include <lib.h>
35
#include <thread.h>
36
#include <synch.h>
37
#include <test.h>
38
39
#define NTHREADS 8
40
41
static struct semaphore *tsem = NULL;
42
43
static
44
void
45
init_sem(void)
46
{
47
if (tsem==NULL) {
48
tsem = sem_create("tsem", 0);
49
if (tsem == NULL) {
50
panic("threadtest: sem_create failed\n");
51
}
52
}
53
}
54
55
static
56
void
57
loudthread(void *junk, unsigned long num)
58
{
59
int ch = '0' + num;
60
int i;
61
62
(void)junk;
63
64
for (i=0; i<120; i++) {
65
putch(ch);
66
}
67
V(tsem);
68
}
69
70
/*
71
* The idea with this is that you should see
72
*
73
* 01234567 <pause> 01234567
74
*
75
* (possibly with the numbers in different orders)
76
*
77
* The delay loop is supposed to be long enough that it should be clear
78
* if either timeslicing or the scheduler is not working right.
79
*/
80
static
81
void
82
quietthread(void *junk, unsigned long num)
83
{
84
int ch = '0' + num;
85
volatile int i;
86
87
(void)junk;
88
89
putch(ch);
90
for (i=0; i<200000; i++);
91
putch(ch);
92
93
V(tsem);
94
}
95
96
static
97
void
98
runthreads(int doloud)
99
{
100
char name[16];
101
int i, result;
102
103
for (i=0; i<NTHREADS; i++) {
104
snprintf(name, sizeof(name), "threadtest%d", i);
105
result = thread_fork(name,
106
doloud ? loudthread : quietthread,
107
NULL, i,
108
NULL);
109
if (result) {
110
panic("threadtest: thread_fork failed %s)\n",
111
strerror(result));
112
}
113
}
114
115
for (i=0; i<NTHREADS; i++) {
116
P(tsem);
117
}
118
}
119
120
121
int
122
threadtest(int nargs, char **args)
123
{
124
(void)nargs;
125
(void)args;
126
127
init_sem();
128
kprintf("Starting thread test...\n");
129
runthreads(1);
130
kprintf("\nThread test done.\n");
131
132
return 0;
133
}
134
135
int
136
threadtest2(int nargs, char **args)
137
{
138
(void)nargs;
139
(void)args;
140
141
init_sem();
142
kprintf("Starting thread test 2...\n");
143
runthreads(0);
144
kprintf("\nThread test 2 done.\n");
145
146
return 0;
147
}
148
149