thread7.c
9.22 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <os.h>
#include <rmon.h>
#include "corefunc.h"
/*
* thread7
*
* Tests for:
* osSetThreadPri (on blocked threads)
*
* Assumed working:
* osCreateThread
* osStartThread
* osSetThreadPri
* osYieldThread
* osCreateMesgQueue
* osSendMesg
* osRecvMesg
* osGetIntMask
* osSetIntMask
*
* Description:
* Check that osThreadPri applied on threads blocked on message
* send/receive have no effect when priorities do not change.
* Check that osThreadPri applied on threads blocked on message receive
* change queue order when relative priorities change.
* Check that osThreadPri applied on threads blocked on message send
* change queue order when relative priorities change.
* Check that osSetThreadPri does not affect interrupt mask.
*/
#define NUM_SLAVES 2
#define MAX_MESSAGES 2
static void slaveRecv(void *);
static void slaveSend(void *);
static OSMesgQueue mesgQueue;
static OSMesg mesgBuf[MAX_MESSAGES];
static OSMesgQueue slaveMesgQueue;
static OSMesg slaveMesgBuf[NUM_SLAVES];
static OSThread slave1Thread, slave2Thread;
static char slave1Stack[STACKSIZE];
static char slave2Stack[STACKSIZE];
int
thread7(void)
{
OSIntMask savedMask, currentMask;
OSMesg actualMesg, expectedMesg;
int i, j, k;
int addFailures, numFailures = 0;
savedMask = osSetIntMask(OS_IM_ALL);
/*
* Set master priority to the same as the slaves' initial priority.
*/
osSetThreadPri(NULL, 10);
/*
* Create a message queue of fixed size, and push the read/write
* pointers to various locations in the circular buffer, but
* leave the message queue empty.
*/
for (i = 0; i < MAX_MESSAGES+1; i++) {
osCreateMesgQueue(&mesgQueue, mesgBuf, MAX_MESSAGES);
for (j = 0; j < i; j++) {
(void)osSendMesg(&mesgQueue, (OSMesg)NULL, OS_MESG_BLOCK);
(void)osRecvMesg(&mesgQueue, (OSMesg *)NULL, OS_MESG_BLOCK);
}
osCreateMesgQueue(&slaveMesgQueue, slaveMesgBuf, NUM_SLAVES);
/*
* Create and start up two threads of the same priority
* that will block on a message receive.
*/
osCreateThread(&slave1Thread, 101, slaveRecv, (void *)0x11111111,
slave1Stack+STACKSIZE, 10);
osStartThread(&slave1Thread);
osCreateThread(&slave2Thread, 102, slaveRecv, (void *)0x22222222,
slave2Stack+STACKSIZE, 10);
osStartThread(&slave2Thread);
osYieldThread();
/*
* Send two messages to unblock the slaves.
*/
expectedMesg = (OSMesg)0x11111111;
(void)osSendMesg(&mesgQueue, expectedMesg, OS_MESG_BLOCK);
expectedMesg = (OSMesg)0x22222222;
(void)osSendMesg(&mesgQueue, expectedMesg, OS_MESG_BLOCK);
/*
* Now change the priority of the first thread -
* this should have no effect.
*/
osSetThreadPri(&slave1Thread, 10);
currentMask = osGetIntMask();
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread7: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
osYieldThread();
/*
* Send two more messages.
*/
expectedMesg = (OSMesg)0x22222222;
(void)osSendMesg(&mesgQueue, expectedMesg, OS_MESG_BLOCK);
expectedMesg = (OSMesg)0x11111111;
(void)osSendMesg(&mesgQueue, expectedMesg, OS_MESG_BLOCK);
/*
* This time, up the priority of the second slave. This should switch
* the relative order of the threads in the queue of blocked threads.
*/
osSetThreadPri(&slave2Thread, 20);
currentMask = osGetIntMask();
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread7: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
/*
* Reap results.
*/
for (k = 0; k < NUM_SLAVES; k++) {
(void)osRecvMesg(&slaveMesgQueue, (OSMesg *)&addFailures,
OS_MESG_BLOCK);
numFailures += addFailures;
}
}
/*
* Create a message queue of fixed size, and push the read/write
* pointers to various locations in the circular buffer, but
* leave the message queue full.
*/
for (i = 0; i < MAX_MESSAGES+1; i++) {
osCreateMesgQueue(&mesgQueue, mesgBuf, MAX_MESSAGES);
for (j = 0; j < i; j++) {
(void)osSendMesg(&mesgQueue, (OSMesg)NULL, OS_MESG_BLOCK);
(void)osRecvMesg(&mesgQueue, (OSMesg *)NULL, OS_MESG_BLOCK);
}
for (j = 0; j < MAX_MESSAGES; j++) {
expectedMesg = (OSMesg)((j<<24)|(j<<16)|(j<<8)|j);
(void)osSendMesg(&mesgQueue, expectedMesg, OS_MESG_BLOCK);
}
osCreateMesgQueue(&slaveMesgQueue, slaveMesgBuf, NUM_SLAVES);
/*
* Create and start up two threads of the same priority
* that will block on a message send.
*/
osCreateThread(&slave1Thread, 101, slaveSend, (void *)0x11111111,
slave1Stack+STACKSIZE, 10);
osStartThread(&slave1Thread);
osCreateThread(&slave2Thread, 102, slaveSend, (void *)0x22222222,
slave2Stack+STACKSIZE, 10);
osStartThread(&slave2Thread);
osYieldThread();
/*
* The following call should have no effect.
*/
osSetThreadPri(&slave1Thread, 10);
currentMask = osGetIntMask();
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread7: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
/*
* Empty out (and check) message queue.
*/
for (j = 0; j < MAX_MESSAGES; j++) {
expectedMesg = (OSMesg)((j<<24)|(j<<16)|(j<<8)|j);
(void)osRecvMesg(&mesgQueue, &actualMesg, OS_MESG_NOBLOCK);
if (expectedMesg != actualMesg) {
osSyncPrintf("thread7: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
}
/*
* Yield to let slaves send messages: first slave1, then slave 2.
*/
osYieldThread();
expectedMesg = (OSMesg)0x11111111;
(void)osRecvMesg(&mesgQueue, &actualMesg, OS_MESG_NOBLOCK);
if (expectedMesg != actualMesg) {
osSyncPrintf("thread7: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
expectedMesg = (OSMesg)0x22222222;
(void)osRecvMesg(&mesgQueue, &actualMesg, OS_MESG_NOBLOCK);
if (expectedMesg != actualMesg) {
osSyncPrintf("thread7: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
/*
* Fill up the message queue again.
*/
for (j = 0; j < MAX_MESSAGES; j++) {
expectedMesg = (OSMesg)((j<<24)|(j<<16)|(j<<8)|j);
(void)osSendMesg(&mesgQueue, expectedMesg, OS_MESG_BLOCK);
}
osYieldThread();
/*
* This time, up the priority of the second slave. This should switch
* the relative order of the threads in the queue of blocked threads.
*/
osSetThreadPri(&slave2Thread, 20);
currentMask = osGetIntMask();
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread7: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
/*
* Empty out (and check) message queue.
*/
for (j = 0; j < MAX_MESSAGES; j++) {
expectedMesg = (OSMesg)((j<<24)|(j<<16)|(j<<8)|j);
(void)osRecvMesg(&mesgQueue, &actualMesg, OS_MESG_NOBLOCK);
if (expectedMesg != actualMesg) {
osSyncPrintf("thread7: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
}
/*
* This time we should get slave2's message, then slave1.
*/
osYieldThread();
expectedMesg = (OSMesg)0x22222222;
(void)osRecvMesg(&mesgQueue, &actualMesg, OS_MESG_NOBLOCK);
if (expectedMesg != actualMesg) {
osSyncPrintf("thread7: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
expectedMesg = (OSMesg)0x11111111;
(void)osRecvMesg(&mesgQueue, &actualMesg, OS_MESG_NOBLOCK);
if (expectedMesg != actualMesg) {
osSyncPrintf("thread7: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
/*
* Reap results.
*/
for (k = 0; k < NUM_SLAVES; k++) {
(void)osRecvMesg(&slaveMesgQueue, (OSMesg *)&addFailures,
OS_MESG_BLOCK);
numFailures += addFailures;
}
}
/*
* Restore original interrupt mask and check it once more.
*/
currentMask = osSetIntMask(savedMask);
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread7: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
return(numFailures);
}
static void
slaveRecv(void *arg)
{
OSMesg actualMesg, expectedMesg;
int numFailures = 0;
expectedMesg = (OSMesg)arg;
/*
* Receive a message, which will block.
*/
(void)osRecvMesg(&mesgQueue, &actualMesg, OS_MESG_BLOCK);
if (expectedMesg != actualMesg) {
osSyncPrintf("thread7: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
osYieldThread();
/*
* Receive a second message, which will also block.
*/
(void)osRecvMesg(&mesgQueue, &actualMesg, OS_MESG_BLOCK);
if (expectedMesg != actualMesg) {
osSyncPrintf("thread7: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
(void)osSendMesg(&slaveMesgQueue, (OSMesg)numFailures, OS_MESG_BLOCK);
}
static void
slaveSend(void *arg)
{
OSMesg expectedMesg;
int numFailures = 0;
expectedMesg = (OSMesg)arg;
/*
* Send a message, which will block.
*/
(void)osSendMesg(&mesgQueue, expectedMesg, OS_MESG_BLOCK);
osYieldThread();
/*
* Send a second message, which will also block.
*/
(void)osSendMesg(&mesgQueue, expectedMesg, OS_MESG_BLOCK);
(void)osSendMesg(&slaveMesgQueue, (OSMesg)numFailures, OS_MESG_BLOCK);
}