jammesg.c
2.54 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
/**************************************************************************
* *
* Copyright (C) 1994, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
**************************************************************************/
#include "osint.h"
/* #include "assert.h" */
/*
* Name: osJamMesg
*
* Description:
* This routine jams a message to the front of a given message queue.
* It also wakes up any thread waiting on this message queue.
* If flags is set to M_BLOCK, the routine will block on a full queue;
* that is, the current running thread is yielded and put back to the
* message thread queue behind other threads of the same priority.
* Normally, flags should be set to OS_MESG_NOBLOCK so that the routine
* can return when the queue is full.
*
* Upon successful completion, a value of 0 is returned. Otherwise, a
* value of -1 is returned.
*
* Globals Referenced:
* None
*/
s32
osJamMesg(OSMesgQueue *mq, OSMesg msg, s32 flags)
{
register u32 saveMask;
/* Ensure that message queue is not null */
/* assert(mq != NULL); */
#ifdef _DEBUG
/* Ensure that flags contain valid data */
if ((flags != OS_MESG_NOBLOCK) && (flags != OS_MESG_BLOCK)) {
__osError(ERR_OSJAMMESG, 1, flags);
return(-1);
}
#endif
/* Disable interrupts */
saveMask = __osDisableInt();
/* Check for full queue */
while (MQ_IS_FULL(mq)) {
if (flags == OS_MESG_BLOCK) {
/* Yield processing and enqueue the calling thread */
__osRunningThread->state = OS_STATE_WAITING;
__osEnqueueAndYield(&mq->fullqueue);
} else {
/* Restore interrupts */
__osRestoreInt(saveMask);
return(-1);
}
}
/* Decrement the first ptr for next available slot */
mq->first = (mq->first + mq->msgCount - 1) % mq->msgCount;
/* Copy message into front of the queue buffer */
mq->msg[mq->first] = msg;
/* Increment valid message counter */
mq->validCount++;
/*
* Now, check to see if there's any thread blocked because the
* queue was empty. If there is, we simply start it again.
*/
if (mq->mtqueue->next != NULL) {
osStartThread(__osPopThread(&mq->mtqueue));
}
/* Restore interrupts */
__osRestoreInt(saveMask);
return(0);
} /* end of osJamMesg */