1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: 17: 18: 19:
20:
21: #ifndef _READY_QUEUE_
22: #define _READY_QUEUE_
23:
24: #include <libstr.h>
25: #include <sys/bitop.h>
26:
27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
42:
43: #define BITMAPSZ ( sizeof(UW) * 8 )
44: #define NUM_BITMAP ( (NUM_PRI + BITMAPSZ - 1) / BITMAPSZ )
45:
46: typedef struct ready_queue {
47: INT top_priority;
48: QUEUE tskque[NUM_PRI];
49: TCB *null;
50: UW bitmap[NUM_BITMAP];
51: TCB *klocktsk;
52: } RDYQUE;
53:
54: IMPORT RDYQUE ready_queue;
55:
56: 57: 58:
59: Inline void ready_queue_initialize( RDYQUE *rq )
60: {
61: INT i;
62:
63: rq->top_priority = NUM_PRI;
64: for ( i = 0; i < NUM_PRI; i++ ) {
65: QueInit(&rq->tskque[i]);
66: }
67: rq->null = NULL;
68: rq->klocktsk = NULL;
69: MEMSET(rq->bitmap, 0, sizeof(rq->bitmap));
70: }
71:
72: 73: 74:
75: Inline TCB* ready_queue_top( RDYQUE *rq )
76: {
77:
78: if ( rq->klocktsk != NULL ) {
79: return rq->klocktsk;
80: }
81:
82: return (TCB*)rq->tskque[rq->top_priority].next;
83: }
84:
85: 86: 87:
88: Inline INT ready_queue_top_priority( const RDYQUE *rq )
89: {
90: return rq->top_priority;
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: Inline BOOL ready_queue_insert( RDYQUE *rq, TCB *tcb )
101: {
102: INT priority = tcb->priority;
103:
104: tcb->slicecnt = 0;
105:
106: QueInsert(&tcb->tskque, &rq->tskque[priority]);
107: BitSet(rq->bitmap, priority);
108:
109: if ( tcb->klocked ) {
110: rq->klocktsk = tcb;
111: }
112:
113: if ( priority < rq->top_priority ) {
114: rq->top_priority = priority;
115: return TRUE;
116: }
117: return FALSE;
118: }
119:
120: 121: 122:
123: Inline void ready_queue_insert_top( RDYQUE *rq, TCB *tcb )
124: {
125: INT priority = tcb->priority;
126:
127: QueInsert(&tcb->tskque, rq->tskque[priority].next);
128: BitSet(rq->bitmap, priority);
129:
130: if ( tcb->klocked ) {
131: rq->klocktsk = tcb;
132: }
133:
134: if ( priority < rq->top_priority ) {
135: rq->top_priority = priority;
136: }
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146:
147: Inline void ready_queue_delete( RDYQUE *rq, TCB *tcb )
148: {
149: INT priority = tcb->priority;
150: INT i;
151:
152: if ( rq->klocktsk == tcb ) {
153: rq->klocktsk = NULL;
154: }
155:
156: QueRemove(&tcb->tskque);
157: if ( tcb->klockwait ) {
158:
159: tcb->klockwait = FALSE;
160: return;
161: }
162: if ( !isQueEmpty(&rq->tskque[priority]) ) {
163: return;
164: }
165:
166: BitClr(rq->bitmap, priority);
167: if ( priority != rq->top_priority ) {
168: return;
169: }
170:
171: i = BitSearch1_w(rq->bitmap, priority, NUM_PRI - priority);
172: if ( i >= 0 ) {
173: rq->top_priority = priority + i;
174: } else {
175: rq->top_priority = NUM_PRI;
176: }
177: }
178:
179: 180: 181: 182:
183: Inline void ready_queue_rotate( RDYQUE *rq, INT priority )
184: {
185: QUEUE *tskque = &rq->tskque[priority];
186: TCB *tcb;
187:
188: tcb = (TCB*)QueRemoveNext(tskque);
189: if ( tcb != NULL ) {
190: QueInsert((QUEUE*)tcb, tskque);
191:
192: tcb->slicecnt = 0;
193: }
194: }
195:
196: 197: 198:
199: Inline TCB* ready_queue_move_last( RDYQUE *rq, TCB *tcb )
200: {
201: QUEUE *tskque = &rq->tskque[tcb->priority];
202:
203: QueRemove(&tcb->tskque);
204: QueInsert(&tcb->tskque, tskque);
205:
206: tcb->slicecnt = 0;
207:
208: return (TCB*)tskque->next;
209: }
210:
211: #endif