thread3.c
6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <os.h>
#include <rmon.h>
#include "corefunc.h"
/*
* thread3
*
* Tests for:
* osCreateThread (priority argument)
* osSetThreadPri (on runnable threads)
*
* Assumed working:
* osSetIntMask
* osGetIntMask
* osStartThread
* osCreateMesgQueue
* osSendMesg
* osRecvMesg
*
* Description:
* Check that osSetPriority does not affect interrupt mask
* Check that threads created with a higher priority run first
* Check that threads that lower their own priority cause a higher
* priority thread to run
* Check that raising a running thread's own priority does not cause
* a context switch
* Check that raising a thread's priority over that of the running
* thread allow them to run.
* Check that setting a priority equal to that of another thread
* of the same priority does not cause a yield
* Check that setting the priority of a thread to the same value
* has no effect.
*
*/
#define NUM_SLAVES 10
#define NUM_LOG 46
typedef struct {
int id;
void (*entry)(void *);
OSPri pri;
u64 stack[STACKSIZE/sizeof(u64)];
OSThread thread;
} ThreadTabEntry;
static void slaveA(void *);
static void slaveB(void *);
static int logExecution(int);
static ThreadTabEntry threadTab[NUM_SLAVES] = {
{ 101, slaveA, 21 },
{ 102, slaveA, 22 },
{ 103, slaveA, 23 },
{ 104, slaveA, 24 },
{ 105, slaveA, 25 },
{ 106, slaveA, 26 },
{ 107, slaveA, 27 },
{ 108, slaveA, 28 },
{ 109, slaveA, 29 },
{ 110, slaveA, 30 },
};
static OSMesgQueue slaveMesgQueue;
static OSMesg slaveMesgBuf[NUM_SLAVES];
static OSThread slave1Thread;
static char slave1Stack[STACKSIZE];
static OSThread slave2Thread;
static char slave2Stack[STACKSIZE];
static int logTab[NUM_LOG];
static int *logPtr = logTab;
static int expLogTab[NUM_LOG] = {
100, 110, 109, 108, 107, 106, 105,
104, 103, 102, 101, 100, 100, 100,
101, 101, 100, 102, 102, 100, 103, 103, 100,
104, 104, 100, 105, 105, 100, 106, 106, 100,
107, 107, 100, 108, 108, 100, 109, 109, 100, 110, 110, 100,
201, 202 };
int
thread3(void)
{
OSIntMask savedMask, currentMask;
ThreadTabEntry *t;
int i;
int addFailures, numFailures = 0;
savedMask = osSetIntMask(OS_IM_ALL);
/*
* Set thread priority higher than any of the threads
* the master is about to spawn.
*/
osSetThreadPri(NULL, 40);
currentMask = osGetIntMask();
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread3: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
osCreateMesgQueue(&slaveMesgQueue, slaveMesgBuf, NUM_SLAVES);
for (t = threadTab; t < &threadTab[NUM_SLAVES]; t++) {
osCreateThread(&t->thread, t->id, t->entry, t,
t->stack+STACKSIZE/sizeof(u64), t->pri);
}
for (t = threadTab; t < &threadTab[NUM_SLAVES]; t++) {
osStartThread(&t->thread);
}
if (logExecution(100)) /* 0 */
numFailures++;
/*
* Lower master's priority and allow slaves to run.
*/
osSetThreadPri(NULL, 20);
if (logExecution(100))
numFailures++;
/*
* Raise master's priority and check that it still runs.
*/
osSetThreadPri(NULL, 30);
if (logExecution(100))
numFailures++;
/*
* Lower priority so it is lower than any of the slave priorities
* in the thread table.
*/
osSetThreadPri(NULL, 20);
if (logExecution(100))
numFailures++;
/*
* Raise slave priorities and check that they run.
*/
for (t = threadTab; t < &threadTab[NUM_SLAVES]; t++) {
osSetThreadPri(&t->thread, t->pri);
if (logExecution(100))
numFailures++;
}
for (i = 0; i < NUM_SLAVES; i++) {
(void)osRecvMesg(&slaveMesgQueue, (OSMesg *)&addFailures,
OS_MESG_BLOCK);
numFailures += addFailures;
}
/*
* Now check that setting the priority of a thread to the same value
* has no effect.
*/
osCreateThread(&slave1Thread, 201, slaveB, (void *)201,
slave1Stack+STACKSIZE, 10);
osStartThread(&slave1Thread);
osCreateThread(&slave2Thread, 202, slaveB, (void *)202,
slave2Stack+STACKSIZE, 10);
osStartThread(&slave2Thread);
osSetThreadPri(&slave1Thread, 10);
for (i = 0; i < 2; i++) {
(void)osRecvMesg(&slaveMesgQueue, (OSMesg *)&addFailures,
OS_MESG_BLOCK);
numFailures += addFailures;
}
/*
* Verify everybody ran when they were supposed to.
*/
if (logPtr != &logTab[NUM_LOG]) {
osSyncPrintf("thread3: log table underflow\n");
numFailures++;
}
for (i = 0; i < logPtr - logTab; i++) {
if (expLogTab[i] != logTab[i]) {
osSyncPrintf("thread3: expected log id %d, actual %d at entry %d\n",
expLogTab[i], logTab[i], i);
numFailures++;
}
}
/*
* Restore original interrupt mask and check it once more.
*/
currentMask = osSetIntMask(savedMask);
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread3: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
return(numFailures);
}
static void
slaveA(void *arg)
{
ThreadTabEntry *t = arg;
OSIntMask currentMask;
int numFailures = 0;
/*
* As a sanity check, check interrupt mask
*/
currentMask = osGetIntMask();
if (OS_IM_ALL != currentMask) {
osSyncPrintf("thread3: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
if (logExecution(t->id))
numFailures++;
/*
* Lower itself (for check that higher priority threads run)
*/
osSetThreadPri(NULL, 10);
/*
* Check the setting of priority did not affect interrupt mask
*/
currentMask = osGetIntMask();
if (OS_IM_ALL != currentMask) {
osSyncPrintf("thread3: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
if (logExecution(t->id))
numFailures++;
/*
* Lower priority again to that of master
*/
osSetThreadPri(NULL, 20);
if (logExecution(t->id))
numFailures++;
/*
* Check interrupt one more time for paranoia's sake.
*/
currentMask = osGetIntMask();
if (OS_IM_ALL != currentMask) {
osSyncPrintf("thread3: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
osSendMesg(&slaveMesgQueue, (OSMesg)numFailures, OS_MESG_BLOCK);
}
static void
slaveB(void *arg)
{
int id;
int numFailures = 0;
id = (int)arg;
if (logExecution(id))
numFailures++;
osSendMesg(&slaveMesgQueue, (OSMesg)numFailures, OS_MESG_BLOCK);
}
static int
logExecution(int id)
{
if (logPtr >= &logTab[NUM_LOG]) {
osSyncPrintf("thread3: log table overflow\n");
return(1);
}
/*
osSyncPrintf("%d. log %d\n", logPtr-logTab, id);
*/
*logPtr++ = id;
return(0);
}