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