thread6.c
9.34 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <os.h>
#include <rmon.h>
#include "corefunc.h"
/*
* thread6
*
* Tests for:
* osDestroyThread (on blocked threads)
*
* Assumed working:
* osCreateThread
* osStartThread
* osSetThreadPri
* osYieldThread
* osCreateMesgQueue
* osJamMesg
* osSendMesg
* osRecvMesg
* osGetIntMask
* osSetIntMask
*
* Description:
* Check that osDestroyThread applied on thread blocked on message
* receive does not corrupt queue.
* Check that osDestroyThread applied on thread blocked on message
* send does not corrupt queue.
* Check that osDestroyThread does not affect interrupt mask.
*/
#define NUM_LOG 42
#define NUM_SLAVES 2
#define MAX_MESSAGES 2
static void slave1(void *);
static void slave2(void *);
static void slave3(void *);
static void slave4(void *);
static int logExecution(int);
static OSMesgQueue mesgQueue;
static OSMesg mesgBuf[MAX_MESSAGES];
static OSMesgQueue slaveMesgQueue;
static OSMesg slaveMesgBuf[NUM_SLAVES];
static OSThread slave1Thread, slave2Thread, slave3Thread, slave4Thread;
static char slave1Stack[STACKSIZE];
static char slave2Stack[STACKSIZE];
static char slave3Stack[STACKSIZE];
static char slave4Stack[STACKSIZE];
static int logTab[NUM_LOG];
static int *logPtr = logTab;
static int expLogTab[NUM_LOG] = {
0, 1, 2, 0, 0, 0, 2,
0, 1, 2, 0, 0, 0, 2,
0, 1, 2, 0, 0, 0, 2,
0, 3, 4, 0, 0, 4, 0,
0, 3, 4, 0, 0, 4, 0,
0, 3, 4, 0, 0, 4, 0 };
int
thread6(void)
{
OSIntMask savedMask, currentMask;
OSMesg actualMesg, expectedMesg;
int i, j, k;
int addFailures, numFailures = 0;
savedMask = osSetIntMask(OS_IM_ALL);
/*
* Set thread priority to the same as slaves.
*/
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 a thread that will block on a message receive.
*/
osCreateThread(&slave1Thread, 101, slave1, (void *)0,
slave1Stack+STACKSIZE, 10);
osStartThread(&slave1Thread);
/*
* Create and start up a thread that will also do a blocking
* receive (later).
*/
osCreateThread(&slave2Thread, 102, slave2, (void *)i,
slave2Stack+STACKSIZE, 10);
osStartThread(&slave2Thread);
if (logExecution(0)) /* 0, 7, 14 */
numFailures++;
osYieldThread(); /* allow slaves to run */
if (logExecution(0)) /* 3, 10, 17 */
numFailures++;
/*
* Destroy the first thread that is blocked.
*/
osDestroyThread(&slave1Thread);
if (logExecution(0)) /* 4, 11, 18 */
numFailures++;
currentMask = osGetIntMask();
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread6: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
/*
* Send a message that will unblock the second thread.
*/
expectedMesg = (OSMesg)((i<<24)|(i<<16)|(i<<8)|i);
(void)osSendMesg(&mesgQueue, expectedMesg, OS_MESG_BLOCK);
if (logExecution(0)) /* 5, 12, 19 */
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 a thread that will block on a message send.
*/
osCreateThread(&slave3Thread, 103, slave3, (void *)0,
slave3Stack+STACKSIZE, 10);
osStartThread(&slave3Thread);
osCreateThread(&slave4Thread, 104, slave4, (void *)MAX_MESSAGES,
slave4Stack+STACKSIZE, 10);
/*
* Create and start up a thread that will also do a blocking
* send (later).
*/
osStartThread(&slave4Thread);
if (logExecution(0)) /* 21, 28, 35 */
numFailures++;
osYieldThread(); /* allow slaves to run */
if (logExecution(0)) /* 24, 31, 38 */
numFailures++;
/*
* Destroy the first thread that is blocked.
*/
osDestroyThread(&slave3Thread);
if (logExecution(0)) /* 25, 32, 39 */
numFailures++;
currentMask = osGetIntMask();
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread6: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
/*
* Receive and check all messages, which should unblock the second
* thread.
*/
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("thread6: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
}
expectedMesg = (OSMesg)((MAX_MESSAGES<<24)|(MAX_MESSAGES<<16)|
(MAX_MESSAGES<<8)|MAX_MESSAGES);
(void)osRecvMesg(&mesgQueue, &actualMesg, OS_MESG_BLOCK);
if (logExecution(0)) /* 27, 34, 41 */
numFailures++;
if (expectedMesg != actualMesg) {
osSyncPrintf("thread6: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
for (k = 0; k < NUM_SLAVES; k++) {
(void)osRecvMesg(&slaveMesgQueue, (OSMesg *)&addFailures,
OS_MESG_BLOCK);
numFailures += addFailures;
}
}
/*
* Verify everybody ran when they were supposed to.
*/
if (logPtr != &logTab[NUM_LOG]) {
osSyncPrintf("thread6: log table underflow\n");
numFailures++;
}
for (i = 0; i < logPtr - logTab; i++) {
if (expLogTab[i] != logTab[i]) {
osSyncPrintf("thread6: 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("thread6: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
return(numFailures);
}
static void
slave1(void *arg)
{
int numFailures = 0;
OSMesg dummyMesg;
if (logExecution(1)) /* 1, 8, 15 */
numFailures++;
/*
* Have to send this now, since we shouldn't live beyond this.
*/
osSendMesg(&slaveMesgQueue, (OSMesg)numFailures, OS_MESG_BLOCK);
/*
* Receive a message, which will block.
*/
(void)osRecvMesg(&mesgQueue, &dummyMesg, OS_MESG_BLOCK);
(void)logExecution(1); /* should not get here */
}
static void
slave2(void *arg)
{
OSIntMask currentMask;
OSMesg expectedMesg, actualMesg;
int i;
int numFailures = 0;
if (logExecution(2)); /* 2, 9, 16 */
numFailures;
/*
* Receive a message, which will block. This will eventually complete.
*/
i = (int)arg;
expectedMesg = (OSMesg)((i<<24)|(i<<16)|(i<<8)|i);
(void)osRecvMesg(&mesgQueue, &actualMesg, OS_MESG_BLOCK);
if (logExecution(2)) /* 6, 13, 20 */
numFailures;
if (expectedMesg != actualMesg) {
osSyncPrintf("thread6: expected message 0x%x, actual 0x%x\n",
expectedMesg, actualMesg);
numFailures++;
}
currentMask = osGetIntMask();
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread6: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
osSendMesg(&slaveMesgQueue, (OSMesg)numFailures, OS_MESG_BLOCK);
}
static void
slave3(void *arg)
{
int numFailures = 0;
if (logExecution(3)) /* 22, 29, 36 */
numFailures;
/*
* Have to send this now, since we shouldn't live beyond this.
*/
(void)osSendMesg(&slaveMesgQueue, (OSMesg)numFailures, OS_MESG_BLOCK);
/*
* Send a message, which will block.
*/
(void)osJamMesg(&mesgQueue, (OSMesg)0xaaaaaaaa, OS_MESG_BLOCK);
(void)logExecution(3); /* should not get here */
}
static void
slave4(void *arg)
{
OSIntMask currentMask;
OSMesg expectedMesg;
int i;
int numFailures = 0;
if (logExecution(4)) /* 23, 30, 37 */
numFailures;
i = (int)arg;
expectedMesg = (OSMesg)((i<<24)|(i<<16)|(i<<8)|i);
(void)osSendMesg(&mesgQueue, expectedMesg, OS_MESG_BLOCK);
if (logExecution(4)) /* 26, 33, 40 */
numFailures++;
currentMask = osGetIntMask();
if (currentMask != OS_IM_ALL) {
osSyncPrintf("thread6: expected interrupt mask 0x%x, actual 0x%x\n",
OS_IM_ALL, currentMask);
numFailures++;
}
/*
* Send a message, which will block. This will eventually complete.
*/
osSendMesg(&slaveMesgQueue, (OSMesg)numFailures, OS_MESG_BLOCK);
}
static int
logExecution(int id)
{
if (logPtr >= &logTab[NUM_LOG]) {
osSyncPrintf("thread6: log table overflow\n");
return(1);
}
/*
osSyncPrintf("%d. log %d\n", logPtr-logTab, id);
*/
*logPtr++ = id;
return(0);
}