1 | /*
|
---|
2 | * TOPPERS/ASP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * High Reliable system Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2005-2008 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
8 | *
|
---|
9 | * ãLì ÒÍCȺÌ(1)`(4)Ìðð½·êÉÀèC{\tgEF
|
---|
10 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
11 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
12 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
13 | * \¦C±Ìpð¨æѺL̳ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
14 | * XR[hÉÜÜêÄ¢é±ÆD
|
---|
15 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
16 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
17 | gip
|
---|
18 | * Ò}j
|
---|
19 | AÈÇjÉCãLÌì \¦C±Ìpð¨æѺL
|
---|
20 | * ̳ÛØKèðfÚ·é±ÆD
|
---|
21 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
22 | * pÅ«È¢`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
23 | * ÆD
|
---|
24 | * (a) Äzzɺ¤hL
|
---|
25 | gipÒ}j
|
---|
26 | AÈÇjÉCãLÌ
|
---|
27 | * ì \¦C±Ìpð¨æѺL̳ÛØKèðfÚ·é±ÆD
|
---|
28 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
29 | * ñ·é±ÆD
|
---|
30 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
31 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
32 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
33 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
34 | * ÆÓ·é±ÆD
|
---|
35 | *
|
---|
36 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
37 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
38 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
39 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
40 | * ÌÓCðíÈ¢D
|
---|
41 | *
|
---|
42 | * @(#) $Id: bit_mutex.c 762 2008-03-07 23:51:41Z hiro $
|
---|
43 | */
|
---|
44 |
|
---|
45 | /*
|
---|
46 | * ~
|
---|
47 | [ebNX@\Ì®«¸
|
---|
48 | */
|
---|
49 |
|
---|
50 | #include "kernel/kernel_impl.h"
|
---|
51 | #include "kernel/task.h"
|
---|
52 | #include "kernel/mutex.h"
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * ~
|
---|
56 | [ebNXID©ç~
|
---|
57 | [ebNXÇubNðæèo·½ßÌ}N
|
---|
58 | * imutex.cæèj
|
---|
59 | */
|
---|
60 | #define INDEX_MTX(mtxid) ((uint_t)((mtxid) - TMIN_MTXID))
|
---|
61 | #define get_mtxcb(mtxid) (&(mtxcb_table[INDEX_MTX(mtxid)]))
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * ~
|
---|
65 | [ebNXÇubNÌmutex_queueÖÌ|C^©çC~
|
---|
66 | [eb
|
---|
67 | * NXÇubNÖÌ|C^ðæèo·½ßÌ}Nimutex.cæèj
|
---|
68 | */
|
---|
69 | #define MTXCB_QUEUE(p_queue) \
|
---|
70 | ((MTXCB *)(((char *) p_queue) - offsetof(MTXCB, mutex_queue)))
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * ~
|
---|
74 | [ebNXÌvgRð»f·é}Nimutex.cæèj
|
---|
75 | */
|
---|
76 | #define MTXPROTO_MASK 0x03U
|
---|
77 | #define MTXPROTO(p_mtxcb) ((p_mtxcb)->p_mtxinib->mtxatr & MTXPROTO_MASK)
|
---|
78 | #define MTX_CEILING(p_mtxcb) (MTXPROTO(p_mtxcb) == TA_CEILING)
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * G[R[hÌè`
|
---|
82 | */
|
---|
83 | #define E_SYS_LINENO ERCD(E_SYS, -(__LINE__))
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * ÇubNÌAhX̳«Ì`FbN
|
---|
87 | */
|
---|
88 | #define VALID_TCB(p_tcb) \
|
---|
89 | ((((char *) p_tcb) - ((char *) tcb_table)) % sizeof(TCB) == 0 \
|
---|
90 | && TMIN_TSKID <= TSKID(p_tcb) && TSKID(p_tcb) <= tmax_tskid)
|
---|
91 |
|
---|
92 | #define VALID_MTXCB(p_mtxcb) \
|
---|
93 | ((((char *) p_mtxcb) - ((char *) mtxcb_table)) % sizeof(MTXCB) == 0 \
|
---|
94 | && TMIN_MTXID <= MTXID(p_mtxcb) && MTXID(p_mtxcb) <= tmax_mtxid)
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * L
|
---|
98 | [Ì`FbN̽ßÌÖ
|
---|
99 | *
|
---|
100 | * p_queueÉp_entryªÜÜêÄ¢é©ð²×éDÜÜêÄ¢êÎtrueCÜÜ
|
---|
101 | * êĢȢêÉÍfalseðÔ·D_uNÌs®ÌêÉàC
|
---|
102 | * falseðÔ·D
|
---|
103 | */
|
---|
104 | static bool_t
|
---|
105 | in_queue(QUEUE *p_queue, QUEUE *p_entry)
|
---|
106 | {
|
---|
107 | QUEUE *p_current, *p_next;
|
---|
108 |
|
---|
109 | p_current = p_queue->p_next;
|
---|
110 | if (p_current->p_prev != p_queue) {
|
---|
111 | return(false); /* _uNÌs® */
|
---|
112 | }
|
---|
113 | while (p_current != p_queue) {
|
---|
114 | if (p_current == p_entry) {
|
---|
115 | return(true); /* p_entryªÜÜêÄ¢½ */
|
---|
116 | }
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * L
|
---|
120 | [ÌÌvfÉiÞ
|
---|
121 | */
|
---|
122 | p_next = p_current->p_next;
|
---|
123 | if (p_next->p_prev != p_current) {
|
---|
124 | return(false); /* _uNÌs® */
|
---|
125 | }
|
---|
126 | p_current = p_next;
|
---|
127 | }
|
---|
128 | return(false);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * ^XN̸
|
---|
133 | */
|
---|
134 | static ER
|
---|
135 | bit_mutex_task(ID tskid)
|
---|
136 | {
|
---|
137 | TCB *p_tcb;
|
---|
138 | MTXCB *p_mtxcb;
|
---|
139 | QUEUE *p_queue, *p_next;
|
---|
140 | uint_t priority;
|
---|
141 |
|
---|
142 | if (!(TMIN_TSKID <= (tskid) && (tskid) <= tmax_tskid)) {
|
---|
143 | return(E_ID);
|
---|
144 | }
|
---|
145 | p_tcb = get_tcb(tskid);
|
---|
146 | priority = p_tcb->bpriority;
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * ^XNªbNµÄ¢é~
|
---|
150 | [ebNXÌL
|
---|
151 | [̸
|
---|
152 | */
|
---|
153 | p_queue = p_tcb->mutex_queue.p_next;
|
---|
154 | if (p_queue->p_prev != &(p_tcb->mutex_queue)) {
|
---|
155 | return(E_SYS_LINENO);
|
---|
156 | }
|
---|
157 | while (p_queue != &(p_tcb->mutex_queue)) {
|
---|
158 | p_mtxcb = MTXCB_QUEUE(p_queue);
|
---|
159 | if (!VALID_MTXCB(p_mtxcb)) {
|
---|
160 | return(E_SYS_LINENO);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * ~
|
---|
165 | [ebNXðbNµÄ¢é^XNÌ`FbN
|
---|
166 | */
|
---|
167 | if (p_mtxcb->p_loctsk != p_tcb) {
|
---|
168 | return(E_SYS_LINENO);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * »ÝDæxÌvZ
|
---|
173 | */
|
---|
174 | if (MTXPROTO(p_mtxcb)) {
|
---|
175 | if (p_mtxcb->p_mtxinib->ceilpri < priority) {
|
---|
176 | priority = p_mtxcb->p_mtxinib->ceilpri;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * L
|
---|
182 | [ÌÌvfÉiÞ
|
---|
183 | */
|
---|
184 | p_next = p_queue->p_next;
|
---|
185 | if (p_next->p_prev != p_queue) {
|
---|
186 | return(E_SYS_LINENO);
|
---|
187 | }
|
---|
188 | p_queue = p_next;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * »ÝDæx̸
|
---|
193 | */
|
---|
194 | if (p_tcb->priority != p_tcb->priority) {
|
---|
195 | return(E_SYS_LINENO);
|
---|
196 | }
|
---|
197 |
|
---|
198 | /*
|
---|
199 | * ^XNªÒÁÄ¢é~
|
---|
200 | [ebNXÉÖ·é¸
|
---|
201 | */
|
---|
202 | if (TSTAT_WAIT_MTX(p_tcb->tstat)) {
|
---|
203 | p_mtxcb = ((WINFO_MTX *)(p_tcb->p_winfo))->p_mtxcb;
|
---|
204 | if (!VALID_MTXCB(p_mtxcb)) {
|
---|
205 | return(E_SYS_LINENO);
|
---|
206 | }
|
---|
207 | if (!in_queue(&(p_mtxcb->wait_queue), &(p_tcb->task_queue))) {
|
---|
208 | return(E_SYS_LINENO);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | return(E_OK);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * ~
|
---|
216 | [ebNX̸
|
---|
217 | */
|
---|
218 | static ER
|
---|
219 | bit_mutex_mutex(ID mtxid)
|
---|
220 | {
|
---|
221 | MTXCB *p_mtxcb;
|
---|
222 | TCB *p_tcb;
|
---|
223 | QUEUE *p_queue, *p_next;
|
---|
224 | uint_t priority;
|
---|
225 |
|
---|
226 | if (!(TMIN_MTXID <= (mtxid) && (mtxid) <= tmax_mtxid)) {
|
---|
227 | return(E_ID);
|
---|
228 | }
|
---|
229 | p_mtxcb = get_mtxcb(mtxid);
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * ú»ubNÖÌ|C^̸
|
---|
233 | */
|
---|
234 | if (p_mtxcb->p_mtxinib != &(mtxinib_table[INDEX_MTX(mtxid)])) {
|
---|
235 | return(E_SYS_LINENO);
|
---|
236 | }
|
---|
237 |
|
---|
238 | /*
|
---|
239 | * ~
|
---|
240 | [ebNXÒ¿L
|
---|
241 | [̸
|
---|
242 | */
|
---|
243 | p_queue = p_mtxcb->wait_queue.p_next;
|
---|
244 | if (p_queue->p_prev != &(p_mtxcb->wait_queue)) {
|
---|
245 | return(E_SYS_LINENO);
|
---|
246 | }
|
---|
247 | priority = TMIN_TPRI;
|
---|
248 | while (p_queue != &(p_mtxcb->wait_queue)) {
|
---|
249 | p_tcb = (TCB *) p_queue;
|
---|
250 | if (!VALID_TCB(p_tcb)) {
|
---|
251 | return(E_SYS_LINENO);
|
---|
252 | }
|
---|
253 |
|
---|
254 | /*
|
---|
255 | * L
|
---|
256 | [ª^XNDæxÉÈÁÄ¢é©Ì¸
|
---|
257 | */
|
---|
258 | if (MTXPROTO(p_mtxcb) != TA_NULL) {
|
---|
259 | if (p_tcb->priority < priority) {
|
---|
260 | return(E_SYS_LINENO);
|
---|
261 | }
|
---|
262 | }
|
---|
263 | priority = p_tcb->priority;
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * ^XNóÔ̸
|
---|
267 | *
|
---|
268 | * ~
|
---|
269 | [ebNXÒ¿óÔÌ^XN̸ÍC^XN̸ÅsÁ
|
---|
270 | * Ä¢é½ßC±±ÅÍsíÈ¢D
|
---|
271 | */
|
---|
272 | if (!TSTAT_WAIT_MTX(p_tcb->tstat)) {
|
---|
273 | return(E_SYS_LINENO);
|
---|
274 | }
|
---|
275 |
|
---|
276 | /*
|
---|
277 | * DæxãÀ̸
|
---|
278 | */
|
---|
279 | if (MTXPROTO(p_mtxcb) == TA_CEILING) {
|
---|
280 | if (p_tcb->bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
281 | return(E_SYS_LINENO);
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * L
|
---|
287 | [ÌÌvfÉiÞ
|
---|
288 | */
|
---|
289 | p_next = p_queue->p_next;
|
---|
290 | if (p_next->p_prev != p_queue) {
|
---|
291 | return(E_SYS_LINENO);
|
---|
292 | }
|
---|
293 | p_queue = p_next;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * ~
|
---|
298 | [ebNXðbNµÄ¢é^XN̸
|
---|
299 | */
|
---|
300 | p_tcb = p_mtxcb->p_loctsk;
|
---|
301 | if (p_tcb == NULL) {
|
---|
302 | /*
|
---|
303 | * ~
|
---|
304 | [ebNXªbN³êĢȢ
|
---|
305 | */
|
---|
306 | if (!queue_empty(&(p_mtxcb->wait_queue))) {
|
---|
307 | return(E_SYS_LINENO);
|
---|
308 | }
|
---|
309 | }
|
---|
310 | else {
|
---|
311 | /*
|
---|
312 | * ~
|
---|
313 | [ebNXªbN³êÄ¢é
|
---|
314 | *
|
---|
315 | * ~
|
---|
316 | [ebNXðbNµÄ¢é^XN̸ÍC^XNÌ
|
---|
317 | * ¸ÅsÁÄ¢é½ßC±±ÅÍsíÈ¢D
|
---|
318 | */
|
---|
319 | if (!VALID_TCB(p_tcb)) {
|
---|
320 | return(E_SYS_LINENO);
|
---|
321 | }
|
---|
322 | if (!in_queue(&(p_tcb->mutex_queue), &(p_mtxcb->mutex_queue))) {
|
---|
323 | return(E_SYS_LINENO);
|
---|
324 | }
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * DæxãÀ̸
|
---|
328 | */
|
---|
329 | if (MTXPROTO(p_mtxcb) == TA_CEILING) {
|
---|
330 | if (p_tcb->bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
331 | return(E_SYS_LINENO);
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|
335 | return(E_OK);
|
---|
336 | }
|
---|
337 |
|
---|
338 | /*
|
---|
339 | * ®«¸[`{Ì
|
---|
340 | */
|
---|
341 | ER
|
---|
342 | bit_mutex(void)
|
---|
343 | {
|
---|
344 | ID tskid, mtxid;
|
---|
345 | ER ercd;
|
---|
346 |
|
---|
347 | /*
|
---|
348 | * ^XN̸
|
---|
349 | */
|
---|
350 | for (tskid = TMIN_TSKID; tskid <= tmax_tskid; tskid++) {
|
---|
351 | ercd = bit_mutex_task(tskid);
|
---|
352 | if (ercd != E_OK) {
|
---|
353 | return(ercd);
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | /*
|
---|
358 | * ~
|
---|
359 | [ebNX̸
|
---|
360 | */
|
---|
361 | for (mtxid = TMIN_MTXID; mtxid <= tmax_mtxid; mtxid++) {
|
---|
362 | ercd = bit_mutex_mutex(mtxid);
|
---|
363 | if (ercd != E_OK) {
|
---|
364 | return(ercd);
|
---|
365 | }
|
---|
366 | }
|
---|
367 | return(E_OK);
|
---|
368 | }
|
---|