1 | /*
|
---|
2 | * TOPPERS/ASP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Advanced Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2005-2015 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 471 2015-12-30 10:03:16Z ertl-hiro $
|
---|
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_LOC_MTX_ENTER
|
---|
71 | #define LOG_LOC_MTX_ENTER(mtxid)
|
---|
72 | #endif /* LOG_LOC_MTX_ENTER */
|
---|
73 |
|
---|
74 | #ifndef LOG_LOC_MTX_LEAVE
|
---|
75 | #define LOG_LOC_MTX_LEAVE(ercd)
|
---|
76 | #endif /* LOG_LOC_MTX_LEAVE */
|
---|
77 |
|
---|
78 | #ifndef LOG_PLOC_MTX_ENTER
|
---|
79 | #define LOG_PLOC_MTX_ENTER(mtxid)
|
---|
80 | #endif /* LOG_PLOC_MTX_ENTER */
|
---|
81 |
|
---|
82 | #ifndef LOG_PLOC_MTX_LEAVE
|
---|
83 | #define LOG_PLOC_MTX_LEAVE(ercd)
|
---|
84 | #endif /* LOG_PLOC_MTX_LEAVE */
|
---|
85 |
|
---|
86 | #ifndef LOG_TLOC_MTX_ENTER
|
---|
87 | #define LOG_TLOC_MTX_ENTER(mtxid, tmout)
|
---|
88 | #endif /* LOG_TLOC_MTX_ENTER */
|
---|
89 |
|
---|
90 | #ifndef LOG_TLOC_MTX_LEAVE
|
---|
91 | #define LOG_TLOC_MTX_LEAVE(ercd)
|
---|
92 | #endif /* LOG_TLOC_MTX_LEAVE */
|
---|
93 |
|
---|
94 | #ifndef LOG_UNL_MTX_ENTER
|
---|
95 | #define LOG_UNL_MTX_ENTER(mtxid)
|
---|
96 | #endif /* LOG_UNL_MTX_ENTER */
|
---|
97 |
|
---|
98 | #ifndef LOG_UNL_MTX_LEAVE
|
---|
99 | #define LOG_UNL_MTX_LEAVE(ercd)
|
---|
100 | #endif /* LOG_UNL_MTX_LEAVE */
|
---|
101 |
|
---|
102 | #ifndef LOG_INI_MTX_ENTER
|
---|
103 | #define LOG_INI_MTX_ENTER(mtxid)
|
---|
104 | #endif /* LOG_INI_MTX_ENTER */
|
---|
105 |
|
---|
106 | #ifndef LOG_INI_MTX_LEAVE
|
---|
107 | #define LOG_INI_MTX_LEAVE(ercd)
|
---|
108 | #endif /* LOG_INI_MTX_LEAVE */
|
---|
109 |
|
---|
110 | #ifndef LOG_REF_MTX_ENTER
|
---|
111 | #define LOG_REF_MTX_ENTER(mtxid, pk_rmtx)
|
---|
112 | #endif /* LOG_REF_MTX_ENTER */
|
---|
113 |
|
---|
114 | #ifndef LOG_REF_MTX_LEAVE
|
---|
115 | #define LOG_REF_MTX_LEAVE(ercd, pk_rmtx)
|
---|
116 | #endif /* LOG_REF_MTX_LEAVE */
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * ãã¥ã¼ããã¯ã¹ã®æ°
|
---|
120 | */
|
---|
121 | #define tnum_mtx ((uint_t)(tmax_mtxid - TMIN_MTXID + 1))
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * ãã¥ã¼ããã¯ã¹IDãããã¥ã¼ããã¯ã¹ç®¡çãããã¯ãåãåºãããã®ãã¯ã
|
---|
125 | */
|
---|
126 | #define INDEX_MTX(mtxid) ((uint_t)((mtxid) - TMIN_MTXID))
|
---|
127 | #define get_mtxcb(mtxid) (&(mtxcb_table[INDEX_MTX(mtxid)]))
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * ãã¥ã¼ããã¯ã¹ã®ãããã³ã«ãå¤æãããã¯ã
|
---|
131 | */
|
---|
132 | #define MTXPROTO_MASK 0x03U
|
---|
133 | #define MTXPROTO(p_mtxcb) ((p_mtxcb)->p_mtxinib->mtxatr & MTXPROTO_MASK)
|
---|
134 | #define MTX_CEILING(p_mtxcb) (MTXPROTO(p_mtxcb) == TA_CEILING)
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * ãã¥ã¼ããã¯ã¹æ©è½ã®åæå
|
---|
138 | */
|
---|
139 | #ifdef TOPPERS_mtxini
|
---|
140 |
|
---|
141 | void
|
---|
142 | initialize_mutex(void)
|
---|
143 | {
|
---|
144 | uint_t i;
|
---|
145 | MTXCB *p_mtxcb;
|
---|
146 |
|
---|
147 | mtxhook_check_ceilpri = mutex_check_ceilpri;
|
---|
148 | mtxhook_scan_ceilmtx = mutex_scan_ceilmtx;
|
---|
149 | mtxhook_release_all = mutex_release_all;
|
---|
150 |
|
---|
151 | for (i = 0; i < tnum_mtx; i++) {
|
---|
152 | p_mtxcb = &(mtxcb_table[i]);
|
---|
153 | queue_initialize(&(p_mtxcb->wait_queue));
|
---|
154 | p_mtxcb->p_mtxinib = &(mtxinib_table[i]);
|
---|
155 | p_mtxcb->p_loctsk = NULL;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | #endif /* TOPPERS_mtxini */
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * ä¸éåªå
|
---|
163 | 度éåã®ãã§ãã¯
|
---|
164 | */
|
---|
165 | #ifdef TOPPERS_mtxchk
|
---|
166 |
|
---|
167 | bool_t
|
---|
168 | mutex_check_ceilpri(TCB *p_tcb, uint_t bpriority)
|
---|
169 | {
|
---|
170 | MTXCB *p_mtxcb;
|
---|
171 |
|
---|
172 | /*
|
---|
173 | * ã¿ã¹ã¯ãããã¯ãã¦ããåªå
|
---|
174 | 度ä¸éãã¥ã¼ããã¯ã¹ã®ä¸ã§ï¼ä¸éåªå
|
---|
175 |
|
---|
176 | * 度ãbpriorityãããä½ããã®ãããã°ï¼falseãè¿ãï¼
|
---|
177 | */
|
---|
178 | p_mtxcb = p_tcb->p_lastmtx;
|
---|
179 | while (p_mtxcb != NULL) {
|
---|
180 | if (MTX_CEILING(p_mtxcb) && bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
181 | return(false);
|
---|
182 | }
|
---|
183 | p_mtxcb = p_mtxcb->p_prevmtx;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * ã¿ã¹ã¯ãåªå
|
---|
188 | 度ä¸éãã¥ã¼ããã¯ã¹ã®ããã¯ãå¾
|
---|
189 | ã£ã¦ããå ´åã«ï¼ã
|
---|
190 | * ã®ä¸éåªå
|
---|
191 | 度ãbpriorityãããä½ãããã°ï¼falseãè¿ãï¼
|
---|
192 | */
|
---|
193 | if (TSTAT_WAIT_MTX(p_tcb->tstat)) {
|
---|
194 | p_mtxcb = ((WINFO_MTX *)(p_tcb->p_winfo))->p_mtxcb;
|
---|
195 | if (MTX_CEILING(p_mtxcb) && bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
196 | return(false);
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * ãããã®æ¡ä»¶ã«ãå½ã¦ã¯ã¾ããªããã°trueãè¿ãï¼
|
---|
202 | */
|
---|
203 | return(true);
|
---|
204 | }
|
---|
205 |
|
---|
206 | #endif /* TOPPERS_mtxchk */
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * åªå
|
---|
210 | 度ä¸éãã¥ã¼ããã¯ã¹ãããã¯ãã¦ãããã®ãã§ãã¯
|
---|
211 | */
|
---|
212 | #ifdef TOPPERS_mtxscan
|
---|
213 |
|
---|
214 | bool_t
|
---|
215 | mutex_scan_ceilmtx(TCB *p_tcb)
|
---|
216 | {
|
---|
217 | MTXCB *p_mtxcb;
|
---|
218 |
|
---|
219 | p_mtxcb = p_tcb->p_lastmtx;
|
---|
220 | while (p_mtxcb != NULL) {
|
---|
221 | if (MTX_CEILING(p_mtxcb)) {
|
---|
222 | return(true);
|
---|
223 | }
|
---|
224 | p_mtxcb = p_mtxcb->p_prevmtx;
|
---|
225 | }
|
---|
226 | return(false);
|
---|
227 | }
|
---|
228 |
|
---|
229 | #endif /* TOPPERS_mtxscan */
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * ã¿ã¹ã¯ã®ç¾å¨åªå
|
---|
233 | 度ã®è¨ç®
|
---|
234 | *
|
---|
235 | * p_tcbã§æå®ãããã¿ã¹ã¯ã®ç¾å¨åªå
|
---|
236 | 度ï¼ã«è¨å®ãã¹ãå¤ï¼ãè¨ç®ããï¼
|
---|
237 | */
|
---|
238 | Inline uint_t
|
---|
239 | mutex_calc_priority(TCB *p_tcb)
|
---|
240 | {
|
---|
241 | uint_t priority;
|
---|
242 | MTXCB *p_mtxcb;
|
---|
243 |
|
---|
244 | priority = p_tcb->bpriority;
|
---|
245 | p_mtxcb = p_tcb->p_lastmtx;
|
---|
246 | while (p_mtxcb != NULL) {
|
---|
247 | if (MTX_CEILING(p_mtxcb) && p_mtxcb->p_mtxinib->ceilpri < priority) {
|
---|
248 | priority = p_mtxcb->p_mtxinib->ceilpri;
|
---|
249 | }
|
---|
250 | p_mtxcb = p_mtxcb->p_prevmtx;
|
---|
251 | }
|
---|
252 | return(priority);
|
---|
253 | }
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * ãã¥ã¼ããã¯ã¹ã解æ¾ããå ´åã®ç¾å¨åªå
|
---|
257 | 度å¤æ´å¦ç
|
---|
258 | */
|
---|
259 | #ifdef TOPPERS_mtxdrop
|
---|
260 |
|
---|
261 | void
|
---|
262 | mutex_drop_priority(TCB *p_tcb, MTXCB *p_mtxcb)
|
---|
263 | {
|
---|
264 | uint_t newpri;
|
---|
265 |
|
---|
266 | if (MTX_CEILING(p_mtxcb)
|
---|
267 | && p_mtxcb->p_mtxinib->ceilpri == p_tcb->priority) {
|
---|
268 | newpri = mutex_calc_priority(p_tcb);
|
---|
269 | if (newpri != p_tcb->priority) {
|
---|
270 | change_priority(p_tcb, newpri, true);
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | #endif /* TOPPERS_mtxdrop */
|
---|
276 |
|
---|
277 | /*
|
---|
278 | * ãã¥ã¼ããã¯ã¹ã®ããã¯
|
---|
279 | */
|
---|
280 | #ifdef TOPPERS_mtxacq
|
---|
281 |
|
---|
282 | void
|
---|
283 | mutex_acquire(TCB *p_tcb, MTXCB *p_mtxcb)
|
---|
284 | {
|
---|
285 | p_mtxcb->p_loctsk = p_tcb;
|
---|
286 | p_mtxcb->p_prevmtx = p_tcb->p_lastmtx;
|
---|
287 | p_tcb->p_lastmtx = p_mtxcb;
|
---|
288 | if (MTX_CEILING(p_mtxcb)
|
---|
289 | && p_mtxcb->p_mtxinib->ceilpri < p_tcb->priority) {
|
---|
290 | change_priority(p_tcb, p_mtxcb->p_mtxinib->ceilpri, true);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | #endif /* TOPPERS_mtxacq */
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * ãã¥ã¼ããã¯ã¹ã®ããã¯è§£é¤
|
---|
298 | */
|
---|
299 | #ifdef TOPPERS_mtxrel
|
---|
300 |
|
---|
301 | void
|
---|
302 | mutex_release(MTXCB *p_mtxcb)
|
---|
303 | {
|
---|
304 | TCB *p_tcb;
|
---|
305 |
|
---|
306 | if (queue_empty(&(p_mtxcb->wait_queue))) {
|
---|
307 | p_mtxcb->p_loctsk = NULL;
|
---|
308 | }
|
---|
309 | else {
|
---|
310 | /*
|
---|
311 | * ãã¥ã¼ããã¯ã¹å¾
|
---|
312 | ã¡ãã¥ã¼ã®å
|
---|
313 | é ã¿ã¹ã¯ï¼p_tcbï¼ã«ï¼ãã¥ã¼ãã
|
---|
314 | * ã¯ã¹ãããã¯ãããï¼
|
---|
315 | */
|
---|
316 | p_tcb = (TCB *) queue_delete_next(&(p_mtxcb->wait_queue));
|
---|
317 | wait_dequeue_tmevtb(p_tcb);
|
---|
318 | p_tcb->p_winfo->wercd = E_OK;
|
---|
319 |
|
---|
320 | p_mtxcb->p_loctsk = p_tcb;
|
---|
321 | p_mtxcb->p_prevmtx = p_tcb->p_lastmtx;
|
---|
322 | p_tcb->p_lastmtx = p_mtxcb;
|
---|
323 | if (MTX_CEILING(p_mtxcb)) {
|
---|
324 | if (p_mtxcb->p_mtxinib->ceilpri < p_tcb->priority) {
|
---|
325 | p_tcb->priority = p_mtxcb->p_mtxinib->ceilpri;
|
---|
326 | }
|
---|
327 | }
|
---|
328 | make_non_wait(p_tcb);
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | #endif /* TOPPERS_mtxrel */
|
---|
333 |
|
---|
334 | /*
|
---|
335 | * ã¿ã¹ã¯ãããã¯ãã¦ãããã¹ã¦ã®ãã¥ã¼ããã¯ã¹ã®ããã¯è§£é¤
|
---|
336 | */
|
---|
337 | #ifdef TOPPERS_mtxrela
|
---|
338 |
|
---|
339 | void
|
---|
340 | mutex_release_all(TCB *p_tcb)
|
---|
341 | {
|
---|
342 | MTXCB *p_mtxcb;
|
---|
343 |
|
---|
344 | while ((p_mtxcb = p_tcb->p_lastmtx) != NULL) {
|
---|
345 | p_tcb->p_lastmtx = p_mtxcb->p_prevmtx;
|
---|
346 | mutex_release(p_mtxcb);
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | #endif /* TOPPERS_mtxrela */
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * ãã¥ã¼ããã¯ã¹ã®ããã¯
|
---|
354 | */
|
---|
355 | #ifdef TOPPERS_loc_mtx
|
---|
356 |
|
---|
357 | ER
|
---|
358 | loc_mtx(ID mtxid)
|
---|
359 | {
|
---|
360 | MTXCB *p_mtxcb;
|
---|
361 | WINFO_MTX winfo_mtx;
|
---|
362 | ER ercd;
|
---|
363 |
|
---|
364 | LOG_LOC_MTX_ENTER(mtxid);
|
---|
365 | CHECK_DISPATCH();
|
---|
366 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
367 | p_mtxcb = get_mtxcb(mtxid);
|
---|
368 |
|
---|
369 | lock_cpu_dsp();
|
---|
370 | if (p_runtsk->raster) {
|
---|
371 | ercd = E_RASTER;
|
---|
372 | }
|
---|
373 | else if (MTX_CEILING(p_mtxcb)
|
---|
374 | && p_runtsk->bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
375 | ercd = E_ILUSE;
|
---|
376 | }
|
---|
377 | else if (p_mtxcb->p_loctsk == NULL) {
|
---|
378 | mutex_acquire(p_runtsk, p_mtxcb);
|
---|
379 | /*
|
---|
380 | * åªå
|
---|
381 | 度ä¸éãã¥ã¼ããã¯ã¹ãããã¯ããå ´åï¼p_runtskã®åªå
|
---|
382 | 度
|
---|
383 | * ãä¸ããå¯è½æ§ããããï¼ãã£ã¹ããããå¿
|
---|
384 | è¦ã«ãªããã¨ã¯ãªãï¼
|
---|
385 | */
|
---|
386 | assert(p_runtsk == p_schedtsk);
|
---|
387 | ercd = E_OK;
|
---|
388 | }
|
---|
389 | else if (p_mtxcb->p_loctsk == p_runtsk) {
|
---|
390 | ercd = E_OBJ;
|
---|
391 | }
|
---|
392 | else {
|
---|
393 | p_runtsk->tstat = TS_WAITING_MTX;
|
---|
394 | wobj_make_wait((WOBJCB *) p_mtxcb, (WINFO_WOBJ *) &winfo_mtx);
|
---|
395 | dispatch();
|
---|
396 | ercd = winfo_mtx.winfo.wercd;
|
---|
397 | }
|
---|
398 | unlock_cpu_dsp();
|
---|
399 |
|
---|
400 | error_exit:
|
---|
401 | LOG_LOC_MTX_LEAVE(ercd);
|
---|
402 | return(ercd);
|
---|
403 | }
|
---|
404 |
|
---|
405 | #endif /* TOPPERS_loc_mtx */
|
---|
406 |
|
---|
407 | /*
|
---|
408 | * ãã¥ã¼ããã¯ã¹ã®ããã¯ï¼ãã¼ãªã³ã°ï¼
|
---|
409 | */
|
---|
410 | #ifdef TOPPERS_ploc_mtx
|
---|
411 |
|
---|
412 | ER
|
---|
413 | ploc_mtx(ID mtxid)
|
---|
414 | {
|
---|
415 | MTXCB *p_mtxcb;
|
---|
416 | ER ercd;
|
---|
417 |
|
---|
418 | LOG_PLOC_MTX_ENTER(mtxid);
|
---|
419 | CHECK_TSKCTX_UNL();
|
---|
420 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
421 | p_mtxcb = get_mtxcb(mtxid);
|
---|
422 |
|
---|
423 | lock_cpu();
|
---|
424 | if (MTX_CEILING(p_mtxcb)
|
---|
425 | && p_runtsk->bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
426 | ercd = E_ILUSE;
|
---|
427 | }
|
---|
428 | else if (p_mtxcb->p_loctsk == NULL) {
|
---|
429 | mutex_acquire(p_runtsk, p_mtxcb);
|
---|
430 | /*
|
---|
431 | * åªå
|
---|
432 | 度ä¸éãã¥ã¼ããã¯ã¹ãããã¯ããå ´åï¼p_runtskã®åªå
|
---|
433 | 度
|
---|
434 | * ãä¸ããå¯è½æ§ããããï¼ãã£ã¹ããããå¿
|
---|
435 | è¦ã«ãªããã¨ã¯ãªãï¼
|
---|
436 | */
|
---|
437 | assert(p_runtsk == p_schedtsk);
|
---|
438 | ercd = E_OK;
|
---|
439 | }
|
---|
440 | else if (p_mtxcb->p_loctsk == p_runtsk) {
|
---|
441 | ercd = E_OBJ;
|
---|
442 | }
|
---|
443 | else {
|
---|
444 | ercd = E_TMOUT;
|
---|
445 | }
|
---|
446 | unlock_cpu();
|
---|
447 |
|
---|
448 | error_exit:
|
---|
449 | LOG_PLOC_MTX_LEAVE(ercd);
|
---|
450 | return(ercd);
|
---|
451 | }
|
---|
452 |
|
---|
453 | #endif /* TOPPERS_ploc_mtx */
|
---|
454 |
|
---|
455 | /*
|
---|
456 | * ãã¥ã¼ããã¯ã¹ã®ããã¯ï¼ã¿ã¤ã ã¢ã¦ãããï¼
|
---|
457 | */
|
---|
458 | #ifdef TOPPERS_tloc_mtx
|
---|
459 |
|
---|
460 | ER
|
---|
461 | tloc_mtx(ID mtxid, TMO tmout)
|
---|
462 | {
|
---|
463 | MTXCB *p_mtxcb;
|
---|
464 | WINFO_MTX winfo_mtx;
|
---|
465 | TMEVTB tmevtb;
|
---|
466 | ER ercd;
|
---|
467 |
|
---|
468 | LOG_TLOC_MTX_ENTER(mtxid, tmout);
|
---|
469 | CHECK_DISPATCH();
|
---|
470 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
471 | CHECK_PAR(VALID_TMOUT(tmout));
|
---|
472 | p_mtxcb = get_mtxcb(mtxid);
|
---|
473 |
|
---|
474 | lock_cpu_dsp();
|
---|
475 | if (p_runtsk->raster) {
|
---|
476 | ercd = E_RASTER;
|
---|
477 | }
|
---|
478 | else if (MTX_CEILING(p_mtxcb)
|
---|
479 | && p_runtsk->bpriority < p_mtxcb->p_mtxinib->ceilpri) {
|
---|
480 | ercd = E_ILUSE;
|
---|
481 | }
|
---|
482 | else if (p_mtxcb->p_loctsk == NULL) {
|
---|
483 | mutex_acquire(p_runtsk, p_mtxcb);
|
---|
484 | /*
|
---|
485 | * åªå
|
---|
486 | 度ä¸éãã¥ã¼ããã¯ã¹ãããã¯ããå ´åï¼p_runtskã®åªå
|
---|
487 | 度
|
---|
488 | * ãä¸ããå¯è½æ§ããããï¼ãã£ã¹ããããå¿
|
---|
489 | è¦ã«ãªããã¨ã¯ãªãï¼
|
---|
490 | */
|
---|
491 | assert(p_runtsk == p_schedtsk);
|
---|
492 | ercd = E_OK;
|
---|
493 | }
|
---|
494 | else if (p_mtxcb->p_loctsk == p_runtsk) {
|
---|
495 | ercd = E_OBJ;
|
---|
496 | }
|
---|
497 | else if (tmout == TMO_POL) {
|
---|
498 | ercd = E_TMOUT;
|
---|
499 | }
|
---|
500 | else {
|
---|
501 | p_runtsk->tstat = TS_WAITING_MTX;
|
---|
502 | wobj_make_wait_tmout((WOBJCB *) p_mtxcb, (WINFO_WOBJ *) &winfo_mtx,
|
---|
503 | &tmevtb, tmout);
|
---|
504 | dispatch();
|
---|
505 | ercd = winfo_mtx.winfo.wercd;
|
---|
506 | }
|
---|
507 | unlock_cpu_dsp();
|
---|
508 |
|
---|
509 | error_exit:
|
---|
510 | LOG_TLOC_MTX_LEAVE(ercd);
|
---|
511 | return(ercd);
|
---|
512 | }
|
---|
513 |
|
---|
514 | #endif /* TOPPERS_tloc_mtx */
|
---|
515 |
|
---|
516 | /*
|
---|
517 | * ãã¥ã¼ããã¯ã¹ã®ããã¯è§£é¤
|
---|
518 | */
|
---|
519 | #ifdef TOPPERS_unl_mtx
|
---|
520 |
|
---|
521 | ER
|
---|
522 | unl_mtx(ID mtxid)
|
---|
523 | {
|
---|
524 | MTXCB *p_mtxcb;
|
---|
525 | ER ercd;
|
---|
526 |
|
---|
527 | LOG_UNL_MTX_ENTER(mtxid);
|
---|
528 | CHECK_TSKCTX_UNL();
|
---|
529 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
530 | p_mtxcb = get_mtxcb(mtxid);
|
---|
531 |
|
---|
532 | lock_cpu();
|
---|
533 | if (p_mtxcb != p_runtsk->p_lastmtx) {
|
---|
534 | ercd = E_OBJ;
|
---|
535 | }
|
---|
536 | else {
|
---|
537 | p_runtsk->p_lastmtx = p_mtxcb->p_prevmtx;
|
---|
538 | mutex_drop_priority(p_runtsk, p_mtxcb);
|
---|
539 | mutex_release(p_mtxcb);
|
---|
540 | if (p_runtsk != p_schedtsk) {
|
---|
541 | dispatch();
|
---|
542 | }
|
---|
543 | ercd = E_OK;
|
---|
544 | }
|
---|
545 | unlock_cpu();
|
---|
546 |
|
---|
547 | error_exit:
|
---|
548 | LOG_UNL_MTX_LEAVE(ercd);
|
---|
549 | return(ercd);
|
---|
550 | }
|
---|
551 |
|
---|
552 | #endif /* TOPPERS_unl_mtx */
|
---|
553 |
|
---|
554 | /*
|
---|
555 | * ãã¥ã¼ããã¯ã¹ã®ååæå
|
---|
556 | */
|
---|
557 | #ifdef TOPPERS_ini_mtx
|
---|
558 |
|
---|
559 | ER
|
---|
560 | ini_mtx(ID mtxid)
|
---|
561 | {
|
---|
562 | MTXCB *p_mtxcb, **pp_prevmtx;
|
---|
563 | TCB *p_loctsk;
|
---|
564 | ER ercd;
|
---|
565 |
|
---|
566 | LOG_INI_MTX_ENTER(mtxid);
|
---|
567 | CHECK_TSKCTX_UNL();
|
---|
568 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
569 | p_mtxcb = get_mtxcb(mtxid);
|
---|
570 |
|
---|
571 | lock_cpu();
|
---|
572 | init_wait_queue(&(p_mtxcb->wait_queue));
|
---|
573 | p_loctsk = p_mtxcb->p_loctsk;
|
---|
574 | if (p_loctsk != NULL) {
|
---|
575 | p_mtxcb->p_loctsk = NULL;
|
---|
576 | pp_prevmtx = &(p_loctsk->p_lastmtx);
|
---|
577 | while (*pp_prevmtx != NULL) {
|
---|
578 | if (*pp_prevmtx == p_mtxcb) {
|
---|
579 | *pp_prevmtx = p_mtxcb->p_prevmtx;
|
---|
580 | break;
|
---|
581 | }
|
---|
582 | pp_prevmtx = &((*pp_prevmtx)->p_prevmtx);
|
---|
583 | }
|
---|
584 | mutex_drop_priority(p_loctsk, p_mtxcb);
|
---|
585 | }
|
---|
586 | if (p_runtsk != p_schedtsk) {
|
---|
587 | dispatch();
|
---|
588 | }
|
---|
589 | ercd = E_OK;
|
---|
590 | unlock_cpu();
|
---|
591 |
|
---|
592 | error_exit:
|
---|
593 | LOG_INI_MTX_LEAVE(ercd);
|
---|
594 | return(ercd);
|
---|
595 | }
|
---|
596 |
|
---|
597 | #endif /* TOPPERS_ini_mtx */
|
---|
598 |
|
---|
599 | /*
|
---|
600 | * ãã¥ã¼ããã¯ã¹ã®ç¶æ
|
---|
601 | åç
|
---|
602 | §
|
---|
603 | */
|
---|
604 | #ifdef TOPPERS_ref_mtx
|
---|
605 |
|
---|
606 | ER
|
---|
607 | ref_mtx(ID mtxid, T_RMTX *pk_rmtx)
|
---|
608 | {
|
---|
609 | MTXCB *p_mtxcb;
|
---|
610 | ER ercd;
|
---|
611 |
|
---|
612 | LOG_REF_MTX_ENTER(mtxid, pk_rmtx);
|
---|
613 | CHECK_TSKCTX_UNL();
|
---|
614 | CHECK_ID(VALID_MTXID(mtxid));
|
---|
615 | p_mtxcb = get_mtxcb(mtxid);
|
---|
616 |
|
---|
617 | lock_cpu();
|
---|
618 | pk_rmtx->htskid = (p_mtxcb->p_loctsk != NULL) ? TSKID(p_mtxcb->p_loctsk)
|
---|
619 | : TSK_NONE;
|
---|
620 | pk_rmtx->wtskid = wait_tskid(&(p_mtxcb->wait_queue));
|
---|
621 | ercd = E_OK;
|
---|
622 | unlock_cpu();
|
---|
623 |
|
---|
624 | error_exit:
|
---|
625 | LOG_REF_MTX_LEAVE(ercd, pk_rmtx);
|
---|
626 | return(ercd);
|
---|
627 | }
|
---|
628 |
|
---|
629 | #endif /* TOPPERS_ref_mtx */
|
---|