1 | /*
|
---|
2 | * TOPPERS/ASP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Advanced Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2005-2016 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
8 | *
|
---|
9 | * ä¸è¨èä½æ¨©è
|
---|
10 | ã¯ï¼ä»¥ä¸ã®(1)ï½(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
11 | * ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
12 | * å¤ã»åé
|
---|
13 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
14 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
15 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
16 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
17 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
18 | * ç¨ã§ããå½¢ã§åé
|
---|
19 | å¸ããå ´åã«ã¯ï¼åé
|
---|
20 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
21 | * è
|
---|
22 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
23 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
24 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
25 | * ç¨ã§ããªãå½¢ã§åé
|
---|
26 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
27 | * ã¨ï¼
|
---|
28 | * (a) åé
|
---|
29 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
30 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
31 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
32 | * (b) åé
|
---|
33 | å¸ã®å½¢æ
|
---|
34 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
35 | * å ±åãããã¨ï¼
|
---|
36 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
37 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
38 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
39 | 責ãããã¨ï¼
|
---|
40 | * ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
41 | * ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
42 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
43 | * å
|
---|
44 | 責ãããã¨ï¼
|
---|
45 | *
|
---|
46 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
47 | ã
|
---|
48 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
49 | * ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
50 | * ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
51 | * ã®è²¬ä»»ãè² ããªãï¼
|
---|
52 | *
|
---|
53 | * $Id: mutex.c 270 2017-02-09 04:03:47Z coas-nagasima $
|
---|
54 | */
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * ãã¥ã¼ããã¯ã¹æ©è½
|
---|
58 | */
|
---|
59 |
|
---|
60 | #include "kernel_impl.h"
|
---|
61 | #include "check.h"
|
---|
62 | #include "task.h"
|
---|
63 | #include "taskhook.h"
|
---|
64 | #include "wait.h"
|
---|
65 | #include "mutex.h"
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * ãã¬ã¼ã¹ãã°ãã¯ãã®ããã©ã«ãå®ç¾©
|
---|
69 | */
|
---|
70 | #ifndef LOG_ACRE_MTX_ENTER
|
---|
71 | #define LOG_ACRE_MTX_ENTER(pk_cmtx)
|
---|
72 | #endif /* LOG_ACRE_MTX_ENTER */
|
---|
73 |
|
---|
74 | #ifndef LOG_ACRE_MTX_LEAVE
|
---|
75 | #define LOG_ACRE_MTX_LEAVE(ercd)
|
---|
76 | #endif /* LOG_ACRE_MTX_LEAVE */
|
---|
77 |
|
---|
78 | #ifndef LOG_DEL_MTX_ENTER
|
---|
79 | #define LOG_DEL_MTX_ENTER(mtxid)
|
---|
80 | #endif /* LOG_DEL_MTX_ENTER */
|
---|
81 |
|
---|
82 | #ifndef LOG_DEL_MTX_LEAVE
|
---|
83 | #define LOG_DEL_MTX_LEAVE(ercd)
|
---|
84 | #endif /* LOG_DEL_MTX_LEAVE */
|
---|
85 |
|
---|
86 | #ifndef LOG_LOC_MTX_ENTER
|
---|
87 | #define LOG_LOC_MTX_ENTER(mtxid)
|
---|
88 | #endif /* LOG_LOC_MTX_ENTER */
|
---|
89 |
|
---|
90 | #ifndef LOG_LOC_MTX_LEAVE
|
---|
91 | #define LOG_LOC_MTX_LEAVE(ercd)
|
---|
92 | #endif /* LOG_LOC_MTX_LEAVE */
|
---|
93 |
|
---|
94 | #ifndef LOG_PLOC_MTX_ENTER
|
---|
95 | #define LOG_PLOC_MTX_ENTER(mtxid)
|
---|
96 | #endif /* LOG_PLOC_MTX_ENTER */
|
---|
97 |
|
---|
98 | #ifndef LOG_PLOC_MTX_LEAVE
|
---|
99 | #define LOG_PLOC_MTX_LEAVE(ercd)
|
---|
100 | #endif /* LOG_PLOC_MTX_LEAVE */
|
---|
101 |
|
---|
102 | #ifndef LOG_TLOC_MTX_ENTER
|
---|
103 | #define LOG_TLOC_MTX_ENTER(mtxid, tmout)
|
---|
104 | #endif /* LOG_TLOC_MTX_ENTER */
|
---|
105 |
|
---|
106 | #ifndef LOG_TLOC_MTX_LEAVE
|
---|
107 | #define LOG_TLOC_MTX_LEAVE(ercd)
|
---|
108 | #endif /* LOG_TLOC_MTX_LEAVE */
|
---|
109 |
|
---|
110 | #ifndef LOG_UNL_MTX_ENTER
|
---|
111 | #define LOG_UNL_MTX_ENTER(mtxid)
|
---|
112 | #endif /* LOG_UNL_MTX_ENTER */
|
---|
113 |
|
---|
114 | #ifndef LOG_UNL_MTX_LEAVE
|
---|
115 | #define LOG_UNL_MTX_LEAVE(ercd)
|
---|
116 | #endif /* LOG_UNL_MTX_LEAVE */
|
---|
117 |
|
---|
118 | #ifndef LOG_INI_MTX_ENTER
|
---|
119 | #define LOG_INI_MTX_ENTER(mtxid)
|
---|
120 | #endif /* LOG_INI_MTX_ENTER */
|
---|
121 |
|
---|
122 | #ifndef LOG_INI_MTX_LEAVE
|
---|
123 | #define LOG_INI_MTX_LEAVE(ercd)
|
---|
124 | #endif /* LOG_INI_MTX_LEAVE */
|
---|
125 |
|
---|
126 | #ifndef LOG_REF_MTX_ENTER
|
---|
127 | #define LOG_REF_MTX_ENTER(mtxid, pk_rmtx)
|
---|
128 | #endif /* LOG_REF_MTX_ENTER */
|
---|
129 |
|
---|
130 | #ifndef LOG_REF_MTX_LEAVE
|
---|
131 | #define LOG_REF_MTX_LEAVE(ercd, pk_rmtx)
|
---|
132 | #endif /* LOG_REF_MTX_LEAVE */
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * ãã¥ã¼ããã¯ã¹ã®æ°
|
---|
136 | */
|
---|
137 | #define tnum_mtx ((uint_t)(tmax_mtxid - TMIN_MTXID + 1))
|
---|
138 | #define tnum_smtx ((uint_t)(tmax_smtxid - TMIN_MTXID + 1))
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * ãã¥ã¼ããã¯ã¹IDãããã¥ã¼ããã¯ã¹ç®¡çãããã¯ãåãåºãããã®ãã¯ã
|
---|
142 | */
|
---|
143 | #define INDEX_MTX(mtxid) ((uint_t)((mtxid) - TMIN_MTXID))
|
---|
144 | #define get_mtxcb(mtxid) (&(mtxcb_table[INDEX_MTX(mtxid)]))
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * ãã¥ã¼ããã¯ã¹ã®ãããã³ã«ãå¤æãããã¯ã
|
---|
148 | */
|
---|
149 | #define MTXPROTO_MASK 0x03U
|
---|
150 | #define MTXPROTO(p_mtxcb) ((p_mtxcb)->p_mtxinib->mtxatr & MTXPROTO_MASK)
|
---|
151 | #define MTX_CEILING(p_mtxcb) (MTXPROTO(p_mtxcb) == TA_CEILING)
|
---|
152 |
|
---|
153 | #ifdef TOPPERS_mtxini
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * 使ç¨ãã¦ããªããã¥ã¼ããã¯ã¹ç®¡çãããã¯ã®ãªã¹ã
|
---|
157 | */
|
---|
158 | QUEUE free_mtxcb;
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * ãã¥ã¼ããã¯ã¹æ©è½ã®åæå
|
---|
162 | */
|
---|
163 | void
|
---|
164 | initialize_mutex(void)
|
---|
165 | {
|
---|
166 | uint_t i, j;
|
---|
167 | MTXCB *p_mtxcb;
|
---|
168 | MTXINIB *p_mtxinib;
|
---|
169 |
|
---|
170 | mtxhook_check_ceilpri = mutex_check_ceilpri;
|
---|
171 | mtxhook_scan_ceilmtx = mutex_scan_ceilmtx;
|
---|
172 | mtxhook_release_all = mutex_release_all;
|
---|
173 |
|
---|
174 | for (i = 0; i < tnum_smtx; i++) {
|
---|
175 | p_mtxcb = &(mtxcb_table[i]);
|
---|
176 | queue_initialize(&(p_mtxcb->wait_queue));
|
---|
177 | p_mtxcb->p_mtxinib = &(mtxinib_table[i]);
|
---|
178 | p_mtxcb->p_loctsk = NULL;
|
---|
179 | }
|
---|
180 | queue_initialize(&free_mtxcb);
|
---|
181 | for (j = 0; i < tnum_mtx; i++, j++) {
|
---|
182 | p_mtxcb = &(mtxcb_table[i]);
|
---|
183 | p_mtxinib = &(amtxinib_table[j]);
|
---|
184 | p_mtxinib->mtxatr = TA_NOEXS;
|
---|
185 | p_mtxcb->p_mtxinib = ((const MTXINIB *) p_mtxinib);
|
---|
186 | queue_insert_prev(&free_mtxcb, &(p_mtxcb->wait_queue));
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | #endif /* TOPPERS_mtxini */
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * ä¸éåªå
|
---|
194 | 度éåã®ãã§ãã¯
|
---|
195 | */
|
---|
196 | #ifdef TOPPERS_mtxchk
|
---|
197 |
|
---|
198 | bool_t
|
---|
199 | mutex_check_ceilpri(TCB *p_tcb, uint_t bpriority)
|
---|
200 | {
|
---|
201 | MTXCB *p_mtxcb;
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * ã¿ã¹ã¯ãããã¯ãã¦ããåªå
|
---|
205 | 度ä¸éãã¥ã¼ããã¯ã¹ã®ä¸ã§ï¼ä¸éåªå
|
---|
206 |
|
---|
207 | * 度ãbpriorityãããä½ããã®ãããã°ï¼falseãè¿ãï¼
|
---|
208 | */
|
---|
209 | p_mtxcb = p_tcb->p_lastmtx;
|
---|
210 | while (p_mtxcb != NULL) {
|
---|
211 | if (MTX_CEILING(p_mtxcb) && bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
212 | return(false);
|
---|
213 | }
|
---|
214 | p_mtxcb = p_mtxcb->p_prevmtx;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * ã¿ã¹ã¯ãåªå
|
---|
219 | 度ä¸éãã¥ã¼ããã¯ã¹ã®ããã¯ãå¾
|
---|
220 | ã£ã¦ããå ´åã«ï¼ã
|
---|
221 | * ã®ä¸éåªå
|
---|
222 | 度ãbpriorityãããä½ãããã°ï¼falseãè¿ãï¼
|
---|
223 | */
|
---|
224 | if (TSTAT_WAIT_MTX(p_tcb->tstat)) {
|
---|
225 | p_mtxcb = ((WINFO_MTX *)(p_tcb->p_winfo))->p_mtxcb;
|
---|
226 | if (MTX_CEILING(p_mtxcb) && bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
227 | return(false);
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * ãããã®æ¡ä»¶ã«ãå½ã¦ã¯ã¾ããªããã°trueãè¿ãï¼
|
---|
233 | */
|
---|
234 | return(true);
|
---|
235 | }
|
---|
236 |
|
---|
237 | #endif /* TOPPERS_mtxchk */
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * åªå
|
---|
241 | 度ä¸éãã¥ã¼ããã¯ã¹ãããã¯ãã¦ãããã®ãã§ãã¯
|
---|
242 | */
|
---|
243 | #ifdef TOPPERS_mtxscan
|
---|
244 |
|
---|
245 | bool_t
|
---|
246 | mutex_scan_ceilmtx(TCB *p_tcb)
|
---|
247 | {
|
---|
248 | MTXCB *p_mtxcb;
|
---|
249 |
|
---|
250 | p_mtxcb = p_tcb->p_lastmtx;
|
---|
251 | while (p_mtxcb != NULL) {
|
---|
252 | if (MTX_CEILING(p_mtxcb)) {
|
---|
253 | return(true);
|
---|
254 | }
|
---|
255 | p_mtxcb = p_mtxcb->p_prevmtx;
|
---|
256 | }
|
---|
257 | return(false);
|
---|
258 | }
|
---|
259 |
|
---|
260 | #endif /* TOPPERS_mtxscan */
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * ã¿ã¹ã¯ã®ç¾å¨åªå
|
---|
264 | 度ã®è¨ç®
|
---|
265 | *
|
---|
266 | * p_tcbã§æå®ãããã¿ã¹ã¯ã®ç¾å¨åªå
|
---|
267 | 度ï¼ã«è¨å®ãã¹ãå¤ï¼ãè¨ç®ããï¼
|
---|
268 | */
|
---|
269 | Inline uint_t
|
---|
270 | mutex_calc_priority(TCB *p_tcb)
|
---|
271 | {
|
---|
272 | uint_t priority;
|
---|
273 | MTXCB *p_mtxcb;
|
---|
274 |
|
---|
275 | priority = p_tcb->bpriority;
|
---|
276 | p_mtxcb = p_tcb->p_lastmtx;
|
---|
277 | while (p_mtxcb != NULL) {
|
---|
278 | if (MTX_CEILING(p_mtxcb) && p_mtxcb->p_mtxinib->ceilpri < priority) {
|
---|
279 | priority = p_mtxcb->p_mtxinib->ceilpri;
|
---|
280 | }
|
---|
281 | p_mtxcb = p_mtxcb->p_prevmtx;
|
---|
282 | }
|
---|
283 | return(priority);
|
---|
284 | }
|
---|
285 |
|
---|
286 | /*
|
---|
287 | * ãã¥ã¼ããã¯ã¹ã解æ¾ããå ´åã®ç¾å¨åªå
|
---|
288 | 度å¤æ´å¦ç
|
---|
289 | */
|
---|
290 | #ifdef TOPPERS_mtxdrop
|
---|
291 |
|
---|
292 | void
|
---|
293 | mutex_drop_priority(TCB *p_tcb, MTXCB *p_mtxcb)
|
---|
294 | {
|
---|
295 | uint_t newpri;
|
---|
296 |
|
---|
297 | if (MTX_CEILING(p_mtxcb)
|
---|
298 | && p_mtxcb->p_mtxinib->ceilpri == p_tcb->priority) {
|
---|
299 | newpri = mutex_calc_priority(p_tcb);
|
---|
300 | if (newpri != p_tcb->priority) {
|
---|
301 | change_priority(p_tcb, newpri, true);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | #endif /* TOPPERS_mtxdrop */
|
---|
307 |
|
---|
308 | /*
|
---|
309 | * ãã¥ã¼ããã¯ã¹ã®ããã¯
|
---|
310 | */
|
---|
311 | #ifdef TOPPERS_mtxacq
|
---|
312 |
|
---|
313 | void
|
---|
314 | mutex_acquire(TCB *p_tcb, MTXCB *p_mtxcb)
|
---|
315 | {
|
---|
316 | p_mtxcb->p_loctsk = p_tcb;
|
---|
317 | p_mtxcb->p_prevmtx = p_tcb->p_lastmtx;
|
---|
318 | p_tcb->p_lastmtx = p_mtxcb;
|
---|
319 | if (MTX_CEILING(p_mtxcb)
|
---|
320 | && p_mtxcb->p_mtxinib->ceilpri < p_tcb->priority) {
|
---|
321 | change_priority(p_tcb, p_mtxcb->p_mtxinib->ceilpri, true);
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | #endif /* TOPPERS_mtxacq */
|
---|
326 |
|
---|
327 | /*
|
---|
328 | * ãã¥ã¼ããã¯ã¹ã®ããã¯è§£é¤
|
---|
329 | */
|
---|
330 | #ifdef TOPPERS_mtxrel
|
---|
331 |
|
---|
332 | void
|
---|
333 | mutex_release(MTXCB *p_mtxcb)
|
---|
334 | {
|
---|
335 | TCB *p_tcb;
|
---|
336 |
|
---|
337 | if (queue_empty(&(p_mtxcb->wait_queue))) {
|
---|
338 | p_mtxcb->p_loctsk = NULL;
|
---|
339 | }
|
---|
340 | else {
|
---|
341 | /*
|
---|
342 | * ãã¥ã¼ããã¯ã¹å¾
|
---|
343 | ã¡ãã¥ã¼ã®å
|
---|
344 | é ã¿ã¹ã¯ï¼p_tcbï¼ã«ï¼ãã¥ã¼ãã
|
---|
345 | * ã¯ã¹ãããã¯ãããï¼
|
---|
346 | */
|
---|
347 | p_tcb = (TCB *) queue_delete_next(&(p_mtxcb->wait_queue));
|
---|
348 | wait_dequeue_tmevtb(p_tcb);
|
---|
349 | p_tcb->p_winfo->wercd = E_OK;
|
---|
350 |
|
---|
351 | p_mtxcb->p_loctsk = p_tcb;
|
---|
352 | p_mtxcb->p_prevmtx = p_tcb->p_lastmtx;
|
---|
353 | p_tcb->p_lastmtx = p_mtxcb;
|
---|
354 | if (MTX_CEILING(p_mtxcb)) {
|
---|
355 | if (p_mtxcb->p_mtxinib->ceilpri < p_tcb->priority) {
|
---|
356 | p_tcb->priority = p_mtxcb->p_mtxinib->ceilpri;
|
---|
357 | }
|
---|
358 | }
|
---|
359 | make_non_wait(p_tcb);
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | #endif /* TOPPERS_mtxrel */
|
---|
364 |
|
---|
365 | /*
|
---|
366 | * ã¿ã¹ã¯ãããã¯ãã¦ãããã¹ã¦ã®ãã¥ã¼ããã¯ã¹ã®ããã¯è§£é¤
|
---|
367 | */
|
---|
368 | #ifdef TOPPERS_mtxrela
|
---|
369 |
|
---|
370 | void
|
---|
371 | mutex_release_all(TCB *p_tcb)
|
---|
372 | {
|
---|
373 | MTXCB *p_mtxcb;
|
---|
374 |
|
---|
375 | while ((p_mtxcb = p_tcb->p_lastmtx) != NULL) {
|
---|
376 | p_tcb->p_lastmtx = p_mtxcb->p_prevmtx;
|
---|
377 | mutex_release(p_mtxcb);
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | #endif /* TOPPERS_mtxrela */
|
---|
382 |
|
---|
383 | /*
|
---|
384 | * ãã¥ã¼ããã¯ã¹ã®çæï¼»NGKI2022ï¼½
|
---|
385 | */
|
---|
386 | #ifdef TOPPERS_acre_mtx
|
---|
387 |
|
---|
388 | ER_UINT
|
---|
389 | acre_mtx(const T_CMTX *pk_cmtx)
|
---|
390 | {
|
---|
391 | MTXCB *p_mtxcb;
|
---|
392 | MTXINIB *p_mtxinib;
|
---|
393 | ATR mtxatr;
|
---|
394 | PRI ceilpri;
|
---|
395 | ER ercd;
|
---|
396 |
|
---|
397 | LOG_ACRE_MTX_ENTER(pk_cmtx);
|
---|
398 | CHECK_TSKCTX_UNL(); /*[NGKI2023][NGKI2024]*/
|
---|
399 |
|
---|
400 | mtxatr = pk_cmtx->mtxatr;
|
---|
401 | ceilpri = pk_cmtx->ceilpri;
|
---|
402 |
|
---|
403 | if (mtxatr == TA_CEILING) {
|
---|
404 | CHECK_PAR(VALID_TPRI(ceilpri)); /*ï¼»NGKI2037ï¼½*/
|
---|
405 | }
|
---|
406 | else {
|
---|
407 | CHECK_RSATR(mtxatr, TA_TPRI); /*ï¼»NGKI2025ï¼½*/
|
---|
408 | }
|
---|
409 |
|
---|
410 | lock_cpu();
|
---|
411 | if (tnum_mtx == 0 || queue_empty(&free_mtxcb)) {
|
---|
412 | ercd = E_NOID; /*ï¼»NGKI2031ï¼½*/
|
---|
413 | }
|
---|
414 | else {
|
---|
415 | p_mtxcb = ((MTXCB *) queue_delete_next(&free_mtxcb));
|
---|
416 | p_mtxinib = (MTXINIB *)(p_mtxcb->p_mtxinib);
|
---|
417 | p_mtxinib->mtxatr = mtxatr;
|
---|
418 | p_mtxinib->ceilpri = INT_PRIORITY(ceilpri);
|
---|
419 |
|
---|
420 | queue_initialize(&(p_mtxcb->wait_queue));
|
---|
421 | p_mtxcb->p_loctsk = NULL; /*ï¼»NGKI2033ï¼½*/
|
---|
422 | ercd = MTXID(p_mtxcb);
|
---|
423 | }
|
---|
424 | unlock_cpu();
|
---|
425 |
|
---|
426 | error_exit:
|
---|
427 | LOG_ACRE_MTX_LEAVE(ercd);
|
---|
428 | return(ercd);
|
---|
429 | }
|
---|
430 |
|
---|
431 | #endif /* TOPPERS_acre_mtx */
|
---|
432 |
|
---|
433 | /*
|
---|
434 | * ãã¥ã¼ããã¯ã¹ã®åé¤ï¼»NGKI2056ï¼½
|
---|
435 | */
|
---|
436 | #ifdef TOPPERS_del_mtx
|
---|
437 |
|
---|
438 | ER
|
---|
439 | del_mtx(ID mtxid)
|
---|
440 | {
|
---|
441 | MTXCB *p_mtxcb, **pp_prevmtx;
|
---|
442 | MTXINIB *p_mtxinib;
|
---|
443 | TCB *p_loctsk;
|
---|
444 | ER ercd;
|
---|
445 |
|
---|
446 | LOG_DEL_MTX_ENTER(mtxid);
|
---|
447 | CHECK_TSKCTX_UNL(); /*[NGKI2057][NGKI2058]*/
|
---|
448 | CHECK_ID(VALID_MTXID(mtxid)); /*ï¼»NGKI2059ï¼½*/
|
---|
449 | p_mtxcb = get_mtxcb(mtxid);
|
---|
450 |
|
---|
451 | lock_cpu();
|
---|
452 | if (p_mtxcb->p_mtxinib->mtxatr == TA_NOEXS) {
|
---|
453 | ercd = E_NOEXS; /*ï¼»NGKI2060ï¼½*/
|
---|
454 | }
|
---|
455 | else if (mtxid <= tmax_smtxid) {
|
---|
456 | ercd = E_OBJ; /*ï¼»NGKI2062ï¼½*/
|
---|
457 | }
|
---|
458 | else {
|
---|
459 | init_wait_queue(&(p_mtxcb->wait_queue)); /*ï¼»NGKI2065ï¼½*/
|
---|
460 | p_loctsk = p_mtxcb->p_loctsk;
|
---|
461 | if (p_loctsk != NULL) {
|
---|
462 | /*
|
---|
463 | * p_loctskãããã¯ãã¦ãããã¥ã¼ããã¯ã¹ã®ãªã¹ããã対象
|
---|
464 | * ãã¥ã¼ã¯ã¹ãåé¤ãï¼å¿
|
---|
465 | è¦ãªå ´åã«ã¯p_loctskã®ç¾å¨åªå
|
---|
466 | 度
|
---|
467 | * ãå¤æ´ããï¼»NGKI2064ï¼½ï¼
|
---|
468 | */
|
---|
469 | pp_prevmtx = &(p_loctsk->p_lastmtx);
|
---|
470 | while (*pp_prevmtx != NULL) {
|
---|
471 | if (*pp_prevmtx == p_mtxcb) {
|
---|
472 | *pp_prevmtx = p_mtxcb->p_prevmtx;
|
---|
473 | break;
|
---|
474 | }
|
---|
475 | pp_prevmtx = &((*pp_prevmtx)->p_prevmtx);
|
---|
476 | }
|
---|
477 | mutex_drop_priority(p_loctsk, p_mtxcb);
|
---|
478 | }
|
---|
479 | p_mtxinib = (MTXINIB *)(p_mtxcb->p_mtxinib);
|
---|
480 | p_mtxinib->mtxatr = TA_NOEXS; /*ï¼»NGKI2063ï¼½*/
|
---|
481 | queue_insert_prev(&free_mtxcb, &(p_mtxcb->wait_queue));
|
---|
482 | if (p_runtsk != p_schedtsk) {
|
---|
483 | dispatch();
|
---|
484 | }
|
---|
485 | ercd = E_OK;
|
---|
486 | }
|
---|
487 | unlock_cpu();
|
---|
488 |
|
---|
489 | error_exit:
|
---|
490 | LOG_DEL_MTX_LEAVE(ercd);
|
---|
491 | return(ercd);
|
---|
492 | }
|
---|
493 |
|
---|
494 | #endif /* TOPPERS_del_mtx */
|
---|
495 |
|
---|
496 | /*
|
---|
497 | * ãã¥ã¼ããã¯ã¹ã®ããã¯
|
---|
498 | */
|
---|
499 | #ifdef TOPPERS_loc_mtx
|
---|
500 |
|
---|
501 | ER
|
---|
502 | loc_mtx(ID mtxid)
|
---|
503 | {
|
---|
504 | MTXCB *p_mtxcb;
|
---|
505 | WINFO_MTX winfo_mtx;
|
---|
506 | ER ercd;
|
---|
507 |
|
---|
508 | LOG_LOC_MTX_ENTER(mtxid);
|
---|
509 | CHECK_DISPATCH();
|
---|
510 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
511 | p_mtxcb = get_mtxcb(mtxid);
|
---|
512 |
|
---|
513 | lock_cpu_dsp();
|
---|
514 | if (p_mtxcb->p_mtxinib->mtxatr == TA_NOEXS) {
|
---|
515 | ercd = E_NOEXS;
|
---|
516 | }
|
---|
517 | else if (p_runtsk->raster) {
|
---|
518 | ercd = E_RASTER;
|
---|
519 | }
|
---|
520 | else if (MTX_CEILING(p_mtxcb)
|
---|
521 | && p_runtsk->bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
522 | ercd = E_ILUSE;
|
---|
523 | }
|
---|
524 | else if (p_mtxcb->p_loctsk == NULL) {
|
---|
525 | mutex_acquire(p_runtsk, p_mtxcb);
|
---|
526 | /*
|
---|
527 | * åªå
|
---|
528 | 度ä¸éãã¥ã¼ããã¯ã¹ãããã¯ããå ´åï¼p_runtskã®åªå
|
---|
529 | 度
|
---|
530 | * ãä¸ããå¯è½æ§ããããï¼ãã£ã¹ããããå¿
|
---|
531 | è¦ã«ãªããã¨ã¯ãªãï¼
|
---|
532 | */
|
---|
533 | assert(p_runtsk == p_schedtsk);
|
---|
534 | ercd = E_OK;
|
---|
535 | }
|
---|
536 | else if (p_mtxcb->p_loctsk == p_runtsk) {
|
---|
537 | ercd = E_OBJ;
|
---|
538 | }
|
---|
539 | else {
|
---|
540 | p_runtsk->tstat = TS_WAITING_MTX;
|
---|
541 | wobj_make_wait((WOBJCB *) p_mtxcb, (WINFO_WOBJ *) &winfo_mtx);
|
---|
542 | dispatch();
|
---|
543 | ercd = winfo_mtx.winfo.wercd;
|
---|
544 | }
|
---|
545 | unlock_cpu_dsp();
|
---|
546 |
|
---|
547 | error_exit:
|
---|
548 | LOG_LOC_MTX_LEAVE(ercd);
|
---|
549 | return(ercd);
|
---|
550 | }
|
---|
551 |
|
---|
552 | #endif /* TOPPERS_loc_mtx */
|
---|
553 |
|
---|
554 | /*
|
---|
555 | * ãã¥ã¼ããã¯ã¹ã®ããã¯ï¼ãã¼ãªã³ã°ï¼
|
---|
556 | */
|
---|
557 | #ifdef TOPPERS_ploc_mtx
|
---|
558 |
|
---|
559 | ER
|
---|
560 | ploc_mtx(ID mtxid)
|
---|
561 | {
|
---|
562 | MTXCB *p_mtxcb;
|
---|
563 | ER ercd;
|
---|
564 |
|
---|
565 | LOG_PLOC_MTX_ENTER(mtxid);
|
---|
566 | CHECK_TSKCTX_UNL();
|
---|
567 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
568 | p_mtxcb = get_mtxcb(mtxid);
|
---|
569 |
|
---|
570 | lock_cpu();
|
---|
571 | if (p_mtxcb->p_mtxinib->mtxatr == TA_NOEXS) {
|
---|
572 | ercd = E_NOEXS;
|
---|
573 | }
|
---|
574 | else if (MTX_CEILING(p_mtxcb)
|
---|
575 | && p_runtsk->bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
576 | ercd = E_ILUSE;
|
---|
577 | }
|
---|
578 | else if (p_mtxcb->p_loctsk == NULL) {
|
---|
579 | mutex_acquire(p_runtsk, p_mtxcb);
|
---|
580 | /*
|
---|
581 | * åªå
|
---|
582 | 度ä¸éãã¥ã¼ããã¯ã¹ãããã¯ããå ´åï¼p_runtskã®åªå
|
---|
583 | 度
|
---|
584 | * ãä¸ããå¯è½æ§ããããï¼ãã£ã¹ããããå¿
|
---|
585 | è¦ã«ãªããã¨ã¯ãªãï¼
|
---|
586 | */
|
---|
587 | assert(p_runtsk == p_schedtsk);
|
---|
588 | ercd = E_OK;
|
---|
589 | }
|
---|
590 | else if (p_mtxcb->p_loctsk == p_runtsk) {
|
---|
591 | ercd = E_OBJ;
|
---|
592 | }
|
---|
593 | else {
|
---|
594 | ercd = E_TMOUT;
|
---|
595 | }
|
---|
596 | unlock_cpu();
|
---|
597 |
|
---|
598 | error_exit:
|
---|
599 | LOG_PLOC_MTX_LEAVE(ercd);
|
---|
600 | return(ercd);
|
---|
601 | }
|
---|
602 |
|
---|
603 | #endif /* TOPPERS_ploc_mtx */
|
---|
604 |
|
---|
605 | /*
|
---|
606 | * ãã¥ã¼ããã¯ã¹ã®ããã¯ï¼ã¿ã¤ã ã¢ã¦ãããï¼
|
---|
607 | */
|
---|
608 | #ifdef TOPPERS_tloc_mtx
|
---|
609 |
|
---|
610 | ER
|
---|
611 | tloc_mtx(ID mtxid, TMO tmout)
|
---|
612 | {
|
---|
613 | MTXCB *p_mtxcb;
|
---|
614 | WINFO_MTX winfo_mtx;
|
---|
615 | TMEVTB tmevtb;
|
---|
616 | ER ercd;
|
---|
617 |
|
---|
618 | LOG_TLOC_MTX_ENTER(mtxid, tmout);
|
---|
619 | CHECK_DISPATCH();
|
---|
620 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
621 | CHECK_PAR(VALID_TMOUT(tmout));
|
---|
622 | p_mtxcb = get_mtxcb(mtxid);
|
---|
623 |
|
---|
624 | lock_cpu_dsp();
|
---|
625 | if (p_mtxcb->p_mtxinib->mtxatr == TA_NOEXS) {
|
---|
626 | ercd = E_NOEXS;
|
---|
627 | }
|
---|
628 | else if (p_runtsk->raster) {
|
---|
629 | ercd = E_RASTER;
|
---|
630 | }
|
---|
631 | else if (MTX_CEILING(p_mtxcb)
|
---|
632 | && p_runtsk->bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
633 | ercd = E_ILUSE;
|
---|
634 | }
|
---|
635 | else if (p_mtxcb->p_loctsk == NULL) {
|
---|
636 | mutex_acquire(p_runtsk, p_mtxcb);
|
---|
637 | /*
|
---|
638 | * åªå
|
---|
639 | 度ä¸éãã¥ã¼ããã¯ã¹ãããã¯ããå ´åï¼p_runtskã®åªå
|
---|
640 | 度
|
---|
641 | * ãä¸ããå¯è½æ§ããããï¼ãã£ã¹ããããå¿
|
---|
642 | è¦ã«ãªããã¨ã¯ãªãï¼
|
---|
643 | */
|
---|
644 | assert(p_runtsk == p_schedtsk);
|
---|
645 | ercd = E_OK;
|
---|
646 | }
|
---|
647 | else if (p_mtxcb->p_loctsk == p_runtsk) {
|
---|
648 | ercd = E_OBJ;
|
---|
649 | }
|
---|
650 | else if (tmout == TMO_POL) {
|
---|
651 | ercd = E_TMOUT;
|
---|
652 | }
|
---|
653 | else {
|
---|
654 | p_runtsk->tstat = TS_WAITING_MTX;
|
---|
655 | wobj_make_wait_tmout((WOBJCB *) p_mtxcb, (WINFO_WOBJ *) &winfo_mtx,
|
---|
656 | &tmevtb, tmout);
|
---|
657 | dispatch();
|
---|
658 | ercd = winfo_mtx.winfo.wercd;
|
---|
659 | }
|
---|
660 | unlock_cpu_dsp();
|
---|
661 |
|
---|
662 | error_exit:
|
---|
663 | LOG_TLOC_MTX_LEAVE(ercd);
|
---|
664 | return(ercd);
|
---|
665 | }
|
---|
666 |
|
---|
667 | #endif /* TOPPERS_tloc_mtx */
|
---|
668 |
|
---|
669 | /*
|
---|
670 | * ãã¥ã¼ããã¯ã¹ã®ããã¯è§£é¤
|
---|
671 | */
|
---|
672 | #ifdef TOPPERS_unl_mtx
|
---|
673 |
|
---|
674 | ER
|
---|
675 | unl_mtx(ID mtxid)
|
---|
676 | {
|
---|
677 | MTXCB *p_mtxcb;
|
---|
678 | ER ercd;
|
---|
679 |
|
---|
680 | LOG_UNL_MTX_ENTER(mtxid);
|
---|
681 | CHECK_TSKCTX_UNL();
|
---|
682 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
683 | p_mtxcb = get_mtxcb(mtxid);
|
---|
684 |
|
---|
685 | lock_cpu();
|
---|
686 | if (p_mtxcb->p_mtxinib->mtxatr == TA_NOEXS) {
|
---|
687 | ercd = E_NOEXS;
|
---|
688 | }
|
---|
689 | else if (p_mtxcb != p_runtsk->p_lastmtx) {
|
---|
690 | ercd = E_OBJ;
|
---|
691 | }
|
---|
692 | else {
|
---|
693 | p_runtsk->p_lastmtx = p_mtxcb->p_prevmtx;
|
---|
694 | mutex_drop_priority(p_runtsk, p_mtxcb);
|
---|
695 | mutex_release(p_mtxcb);
|
---|
696 | if (p_runtsk != p_schedtsk) {
|
---|
697 | dispatch();
|
---|
698 | }
|
---|
699 | ercd = E_OK;
|
---|
700 | }
|
---|
701 | unlock_cpu();
|
---|
702 |
|
---|
703 | error_exit:
|
---|
704 | LOG_UNL_MTX_LEAVE(ercd);
|
---|
705 | return(ercd);
|
---|
706 | }
|
---|
707 |
|
---|
708 | #endif /* TOPPERS_unl_mtx */
|
---|
709 |
|
---|
710 | /*
|
---|
711 | * ãã¥ã¼ããã¯ã¹ã®ååæå
|
---|
712 | */
|
---|
713 | #ifdef TOPPERS_ini_mtx
|
---|
714 |
|
---|
715 | ER
|
---|
716 | ini_mtx(ID mtxid)
|
---|
717 | {
|
---|
718 | MTXCB *p_mtxcb, **pp_prevmtx;
|
---|
719 | TCB *p_loctsk;
|
---|
720 | ER ercd;
|
---|
721 |
|
---|
722 | LOG_INI_MTX_ENTER(mtxid);
|
---|
723 | CHECK_TSKCTX_UNL();
|
---|
724 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
725 | p_mtxcb = get_mtxcb(mtxid);
|
---|
726 |
|
---|
727 | lock_cpu();
|
---|
728 | if (p_mtxcb->p_mtxinib->mtxatr == TA_NOEXS) {
|
---|
729 | ercd = E_NOEXS;
|
---|
730 | }
|
---|
731 | else {
|
---|
732 | init_wait_queue(&(p_mtxcb->wait_queue));
|
---|
733 | p_loctsk = p_mtxcb->p_loctsk;
|
---|
734 | if (p_loctsk != NULL) {
|
---|
735 | p_mtxcb->p_loctsk = NULL;
|
---|
736 | pp_prevmtx = &(p_loctsk->p_lastmtx);
|
---|
737 | while (*pp_prevmtx != NULL) {
|
---|
738 | if (*pp_prevmtx == p_mtxcb) {
|
---|
739 | *pp_prevmtx = p_mtxcb->p_prevmtx;
|
---|
740 | break;
|
---|
741 | }
|
---|
742 | pp_prevmtx = &((*pp_prevmtx)->p_prevmtx);
|
---|
743 | }
|
---|
744 | mutex_drop_priority(p_loctsk, p_mtxcb);
|
---|
745 | }
|
---|
746 | if (p_runtsk != p_schedtsk) {
|
---|
747 | dispatch();
|
---|
748 | }
|
---|
749 | ercd = E_OK;
|
---|
750 | }
|
---|
751 | unlock_cpu();
|
---|
752 |
|
---|
753 | error_exit:
|
---|
754 | LOG_INI_MTX_LEAVE(ercd);
|
---|
755 | return(ercd);
|
---|
756 | }
|
---|
757 |
|
---|
758 | #endif /* TOPPERS_ini_mtx */
|
---|
759 |
|
---|
760 | /*
|
---|
761 | * ãã¥ã¼ããã¯ã¹ã®ç¶æ
|
---|
762 | åç
|
---|
763 | §
|
---|
764 | */
|
---|
765 | #ifdef TOPPERS_ref_mtx
|
---|
766 |
|
---|
767 | ER
|
---|
768 | ref_mtx(ID mtxid, T_RMTX *pk_rmtx)
|
---|
769 | {
|
---|
770 | MTXCB *p_mtxcb;
|
---|
771 | ER ercd;
|
---|
772 |
|
---|
773 | LOG_REF_MTX_ENTER(mtxid, pk_rmtx);
|
---|
774 | CHECK_TSKCTX_UNL();
|
---|
775 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
776 | p_mtxcb = get_mtxcb(mtxid);
|
---|
777 |
|
---|
778 | lock_cpu();
|
---|
779 | if (p_mtxcb->p_mtxinib->mtxatr == TA_NOEXS) {
|
---|
780 | ercd = E_NOEXS;
|
---|
781 | }
|
---|
782 | else {
|
---|
783 | pk_rmtx->htskid = (p_mtxcb->p_loctsk != NULL) ? TSKID(p_mtxcb->p_loctsk)
|
---|
784 | : TSK_NONE;
|
---|
785 | pk_rmtx->wtskid = wait_tskid(&(p_mtxcb->wait_queue));
|
---|
786 | ercd = E_OK;
|
---|
787 | }
|
---|
788 | unlock_cpu();
|
---|
789 |
|
---|
790 | error_exit:
|
---|
791 | LOG_REF_MTX_LEAVE(ercd, pk_rmtx);
|
---|
792 | return(ercd);
|
---|
793 | }
|
---|
794 |
|
---|
795 | #endif /* TOPPERS_ref_mtx */
|
---|