source: UsbWattMeter/trunk/asp_dcre/kernel/task_sync.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 10.2 KB
RevLine 
[164]1/*
2 * TOPPERS/ASP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Advanced Standard Profile Kernel
5 *
6 * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
7 * Toyohashi Univ. of Technology, JAPAN
8 * Copyright (C) 2005-2010 by Embedded and Real-Time Systems Laboratory
9 * Graduate School of Information Science, Nagoya Univ., JAPAN
10 *
11 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
12 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
13 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
14 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
15 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
16 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
17 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
18 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
19ƒƒ“ƒgi—˜—p
20 * ŽÒƒ}ƒjƒ…
21ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
22 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
23 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
24 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
25 * ‚ƁD
26 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
27ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
28ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
29 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
30 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
31 * •ñ‚·‚邱‚ƁD
32 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
33 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
34 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
35 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
36 * –Ɛӂ·‚邱‚ƁD
37 *
38 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
39 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
40 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
41 * ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
42 * ‚̐ӔC‚𕉂í‚È‚¢D
43 *
44 * @(#) $Id: task_sync.c 164 2016-03-07 11:33:50Z coas-nagasima $
45 */
46
47/*
48 * ƒ^ƒXƒN•t‘®“¯Šú‹@”\
49 */
50
51#include "kernel_impl.h"
52#include "check.h"
53#include "task.h"
54#include "wait.h"
55
56/*
57 * ƒgƒŒ[ƒXƒƒOƒ}ƒNƒ‚̃fƒtƒHƒ‹ƒg’è‹`
58 */
59#ifndef LOG_SLP_TSK_ENTER
60#define LOG_SLP_TSK_ENTER()
61#endif /* LOG_SLP_TSK_ENTER */
62
63#ifndef LOG_SLP_TSK_LEAVE
64#define LOG_SLP_TSK_LEAVE(ercd)
65#endif /* LOG_SLP_TSK_LEAVE */
66
67#ifndef LOG_TSLP_TSK_ENTER
68#define LOG_TSLP_TSK_ENTER(tmout)
69#endif /* LOG_TSLP_TSK_ENTER */
70
71#ifndef LOG_TSLP_TSK_LEAVE
72#define LOG_TSLP_TSK_LEAVE(ercd)
73#endif /* LOG_TSLP_TSK_LEAVE */
74
75#ifndef LOG_WUP_TSK_ENTER
76#define LOG_WUP_TSK_ENTER(tskid)
77#endif /* LOG_WUP_TSK_ENTER */
78
79#ifndef LOG_WUP_TSK_LEAVE
80#define LOG_WUP_TSK_LEAVE(ercd)
81#endif /* LOG_WUP_TSK_LEAVE */
82
83#ifndef LOG_IWUP_TSK_ENTER
84#define LOG_IWUP_TSK_ENTER(tskid)
85#endif /* LOG_IWUP_TSK_ENTER */
86
87#ifndef LOG_IWUP_TSK_LEAVE
88#define LOG_IWUP_TSK_LEAVE(ercd)
89#endif /* LOG_IWUP_TSK_LEAVE */
90
91#ifndef LOG_CAN_WUP_ENTER
92#define LOG_CAN_WUP_ENTER(tskid)
93#endif /* LOG_CAN_WUP_ENTER */
94
95#ifndef LOG_CAN_WUP_LEAVE
96#define LOG_CAN_WUP_LEAVE(ercd)
97#endif /* LOG_CAN_WUP_LEAVE */
98
99#ifndef LOG_REL_WAI_ENTER
100#define LOG_REL_WAI_ENTER(tskid)
101#endif /* LOG_REL_WAI_ENTER */
102
103#ifndef LOG_REL_WAI_LEAVE
104#define LOG_REL_WAI_LEAVE(ercd)
105#endif /* LOG_REL_WAI_LEAVE */
106
107#ifndef LOG_IREL_WAI_ENTER
108#define LOG_IREL_WAI_ENTER(tskid)
109#endif /* LOG_IREL_WAI_ENTER */
110
111#ifndef LOG_IREL_WAI_LEAVE
112#define LOG_IREL_WAI_LEAVE(ercd)
113#endif /* LOG_IREL_WAI_LEAVE */
114
115#ifndef LOG_SUS_TSK_ENTER
116#define LOG_SUS_TSK_ENTER(tskid)
117#endif /* LOG_SUS_TSK_ENTER */
118
119#ifndef LOG_SUS_TSK_LEAVE
120#define LOG_SUS_TSK_LEAVE(ercd)
121#endif /* LOG_SUS_TSK_LEAVE */
122
123#ifndef LOG_RSM_TSK_ENTER
124#define LOG_RSM_TSK_ENTER(tskid)
125#endif /* LOG_RSM_TSK_ENTER */
126
127#ifndef LOG_RSM_TSK_LEAVE
128#define LOG_RSM_TSK_LEAVE(ercd)
129#endif /* LOG_RSM_TSK_LEAVE */
130
131#ifndef LOG_DLY_TSK_ENTER
132#define LOG_DLY_TSK_ENTER(dlytim)
133#endif /* LOG_DLY_TSK_ENTER */
134
135#ifndef LOG_DLY_TSK_LEAVE
136#define LOG_DLY_TSK_LEAVE(ercd)
137#endif /* LOG_DLY_TSK_LEAVE */
138
139/*
140 * ‹N°‘Ò‚¿
141 */
142#ifdef TOPPERS_slp_tsk
143
144ER
145slp_tsk(void)
146{
147 WINFO winfo;
148 ER ercd;
149
150 LOG_SLP_TSK_ENTER();
151 CHECK_DISPATCH();
152
153 t_lock_cpu();
154 if (p_runtsk->wupque) {
155 p_runtsk->wupque = false;
156 ercd = E_OK;
157 }
158 else {
159 p_runtsk->tstat = (TS_WAITING | TS_WAIT_SLP);
160 make_wait(&winfo);
161 LOG_TSKSTAT(p_runtsk);
162 dispatch();
163 ercd = winfo.wercd;
164 }
165 t_unlock_cpu();
166
167 error_exit:
168 LOG_SLP_TSK_LEAVE(ercd);
169 return(ercd);
170}
171
172#endif /* TOPPERS_slp_tsk */
173
174/*
175 * ‹N°‘Ò‚¿iƒ^ƒCƒ€ƒAƒEƒg‚ ‚èj
176 */
177#ifdef TOPPERS_tslp_tsk
178
179ER
180tslp_tsk(TMO tmout)
181{
182 WINFO winfo;
183 TMEVTB tmevtb;
184 ER ercd;
185
186 LOG_TSLP_TSK_ENTER(tmout);
187 CHECK_DISPATCH();
188 CHECK_TMOUT(tmout);
189
190 t_lock_cpu();
191 if (p_runtsk->wupque) {
192 p_runtsk->wupque = false;
193 ercd = E_OK;
194 }
195 else if (tmout == TMO_POL) {
196 ercd = E_TMOUT;
197 }
198 else {
199 p_runtsk->tstat = (TS_WAITING | TS_WAIT_SLP);
200 make_wait_tmout(&winfo, &tmevtb, tmout);
201 LOG_TSKSTAT(p_runtsk);
202 dispatch();
203 ercd = winfo.wercd;
204 }
205 t_unlock_cpu();
206
207 error_exit:
208 LOG_TSLP_TSK_LEAVE(ercd);
209 return(ercd);
210}
211
212#endif /* TOPPERS_tslp_tsk */
213
214/*
215 * ƒ^ƒXƒN‚Ì‹N°
216 */
217#ifdef TOPPERS_wup_tsk
218
219ER
220wup_tsk(ID tskid)
221{
222 TCB *p_tcb;
223 ER ercd;
224
225 LOG_WUP_TSK_ENTER(tskid);
226 CHECK_TSKCTX_UNL();
227 CHECK_TSKID_SELF(tskid);
228 p_tcb = get_tcb_self(tskid);
229
230 t_lock_cpu();
231 if (p_tcb->p_tinib->tskatr == TA_NOEXS) {
232 ercd = E_NOEXS;
233 }
234 else if (TSTAT_DORMANT(p_tcb->tstat)) {
235 ercd = E_OBJ;
236 }
237 else if (TSTAT_WAIT_SLP(p_tcb->tstat)) {
238 if (wait_complete(p_tcb)) {
239 dispatch();
240 }
241 ercd = E_OK;
242 }
243 else if (!(p_tcb->wupque)) {
244 p_tcb->wupque = true;
245 ercd = E_OK;
246 }
247 else {
248 ercd = E_QOVR;
249 }
250 t_unlock_cpu();
251
252 error_exit:
253 LOG_WUP_TSK_LEAVE(ercd);
254 return(ercd);
255}
256
257#endif /* TOPPERS_wup_tsk */
258
259/*
260 * ƒ^ƒXƒN‚Ì‹N°i”ñƒ^ƒXƒNƒRƒ“ƒeƒLƒXƒg—pj
261 */
262#ifdef TOPPERS_iwup_tsk
263
264ER
265iwup_tsk(ID tskid)
266{
267 TCB *p_tcb;
268 ER ercd;
269
270 LOG_IWUP_TSK_ENTER(tskid);
271 CHECK_INTCTX_UNL();
272 CHECK_TSKID(tskid);
273 p_tcb = get_tcb(tskid);
274
275 i_lock_cpu();
276 if (p_tcb->p_tinib->tskatr == TA_NOEXS) {
277 ercd = E_NOEXS;
278 }
279 else if (TSTAT_DORMANT(p_tcb->tstat)) {
280 ercd = E_OBJ;
281 }
282 else if (TSTAT_WAIT_SLP(p_tcb->tstat)) {
283 if (wait_complete(p_tcb)) {
284 reqflg = true;
285 }
286 ercd = E_OK;
287 }
288 else if (!(p_tcb->wupque)) {
289 p_tcb->wupque = true;
290 ercd = E_OK;
291 }
292 else {
293 ercd = E_QOVR;
294 }
295 i_unlock_cpu();
296
297 error_exit:
298 LOG_IWUP_TSK_LEAVE(ercd);
299 return(ercd);
300}
301
302#endif /* TOPPERS_iwup_tsk */
303
304/*
305 * ƒ^ƒXƒN‹N°—v‹‚̃Lƒƒƒ“ƒZƒ‹
306 */
307#ifdef TOPPERS_can_wup
308
309ER_UINT
310can_wup(ID tskid)
311{
312 TCB *p_tcb;
313 ER_UINT ercd;
314
315 LOG_CAN_WUP_ENTER(tskid);
316 CHECK_TSKCTX_UNL();
317 CHECK_TSKID_SELF(tskid);
318 p_tcb = get_tcb_self(tskid);
319
320 t_lock_cpu();
321 if (p_tcb->p_tinib->tskatr == TA_NOEXS) {
322 ercd = E_NOEXS;
323 }
324 else if (TSTAT_DORMANT(p_tcb->tstat)) {
325 ercd = E_OBJ;
326 }
327 else {
328 ercd = p_tcb->wupque ? 1 : 0;
329 p_tcb->wupque = false;
330 }
331 t_unlock_cpu();
332
333 error_exit:
334 LOG_CAN_WUP_LEAVE(ercd);
335 return(ercd);
336}
337
338#endif /* TOPPERS_can_wup */
339
340/*
341 * ‘Ò‚¿ó‘Ô‚Ì‹­§‰ðœ
342 */
343#ifdef TOPPERS_rel_wai
344
345ER
346rel_wai(ID tskid)
347{
348 TCB *p_tcb;
349 ER ercd;
350
351 LOG_REL_WAI_ENTER(tskid);
352 CHECK_TSKCTX_UNL();
353 CHECK_TSKID(tskid);
354 p_tcb = get_tcb(tskid);
355
356 t_lock_cpu();
357 if (p_tcb->p_tinib->tskatr == TA_NOEXS) {
358 ercd = E_NOEXS;
359 }
360 else if (!TSTAT_WAITING(p_tcb->tstat)) {
361 ercd = E_OBJ;
362 }
363 else {
364 if (wait_release(p_tcb)) {
365 dispatch();
366 }
367 ercd = E_OK;
368 }
369 t_unlock_cpu();
370
371 error_exit:
372 LOG_REL_WAI_LEAVE(ercd);
373 return(ercd);
374}
375
376#endif /* TOPPERS_rel_wai */
377
378/*
379 * ‘Ò‚¿ó‘Ô‚Ì‹­§‰ðœi”ñƒ^ƒXƒNƒRƒ“ƒeƒLƒXƒg—pj
380 */
381#ifdef TOPPERS_irel_wai
382
383ER
384irel_wai(ID tskid)
385{
386 TCB *p_tcb;
387 ER ercd;
388
389 LOG_IREL_WAI_ENTER(tskid);
390 CHECK_INTCTX_UNL();
391 CHECK_TSKID(tskid);
392 p_tcb = get_tcb(tskid);
393
394 i_lock_cpu();
395 if (p_tcb->p_tinib->tskatr == TA_NOEXS) {
396 ercd = E_NOEXS;
397 }
398 else if (!TSTAT_WAITING(p_tcb->tstat)) {
399 ercd = E_OBJ;
400 }
401 else {
402 if (wait_release(p_tcb)) {
403 reqflg = true;
404 }
405 ercd = E_OK;
406 }
407 i_unlock_cpu();
408
409 error_exit:
410 LOG_IREL_WAI_LEAVE(ercd);
411 return(ercd);
412}
413
414#endif /* TOPPERS_irel_wai */
415
416/*
417 * ‹­§‘Ò‚¿ó‘Ԃւ̈ڍs
418 */
419#ifdef TOPPERS_sus_tsk
420
421ER
422sus_tsk(ID tskid)
423{
424 TCB *p_tcb;
425 ER ercd;
426
427 LOG_SUS_TSK_ENTER(tskid);
428 CHECK_TSKCTX_UNL();
429 CHECK_TSKID_SELF(tskid);
430 p_tcb = get_tcb_self(tskid);
431
432 t_lock_cpu();
433 if (p_tcb == p_runtsk && !dspflg) {
434 ercd = E_CTX;
435 }
436 else if (p_tcb->p_tinib->tskatr == TA_NOEXS) {
437 ercd = E_NOEXS;
438 }
439 else if (TSTAT_DORMANT(p_tcb->tstat)) {
440 ercd = E_OBJ;
441 }
442 else if (TSTAT_RUNNABLE(p_tcb->tstat)) {
443 /*
444 * ŽÀs‚Å‚«‚éó‘Ô‚©‚ç‹­§‘Ò‚¿ó‘Ô‚Ö‚Ì‘JˆÚ
445 */
446 p_tcb->tstat = TS_SUSPENDED;
447 LOG_TSKSTAT(p_tcb);
448 if (make_non_runnable(p_tcb)) {
449 dispatch();
450 }
451 ercd = E_OK;
452 }
453 else if (TSTAT_SUSPENDED(p_tcb->tstat)) {
454 ercd = E_QOVR;
455 }
456 else {
457 /*
458 * ‘Ò‚¿ó‘Ô‚©‚ç“ñd‘Ò‚¿ó‘Ô‚Ö‚Ì‘JˆÚ
459 */
460 p_tcb->tstat |= TS_SUSPENDED;
461 LOG_TSKSTAT(p_tcb);
462 ercd = E_OK;
463 }
464 t_unlock_cpu();
465
466 error_exit:
467 LOG_SUS_TSK_LEAVE(ercd);
468 return(ercd);
469}
470
471#endif /* TOPPERS_sus_tsk */
472
473/*
474 * ‹­§‘Ò‚¿ó‘Ô‚©‚ç‚̍ĊJ
475 */
476#ifdef TOPPERS_rsm_tsk
477
478ER
479rsm_tsk(ID tskid)
480{
481 TCB *p_tcb;
482 ER ercd;
483
484 LOG_RSM_TSK_ENTER(tskid);
485 CHECK_TSKCTX_UNL();
486 CHECK_TSKID(tskid);
487 p_tcb = get_tcb(tskid);
488
489 t_lock_cpu();
490 if (p_tcb->p_tinib->tskatr == TA_NOEXS) {
491 ercd = E_NOEXS;
492 }
493 else if (!TSTAT_SUSPENDED(p_tcb->tstat)) {
494 ercd = E_OBJ;
495 }
496 else if (!TSTAT_WAITING(p_tcb->tstat)) {
497 /*
498 * ‹­§‘Ò‚¿ó‘Ô‚©‚çŽÀs‚Å‚«‚éó‘Ô‚Ö‚Ì‘JˆÚ
499 */
500 p_tcb->tstat = TS_RUNNABLE;
501 LOG_TSKSTAT(p_tcb);
502 if (make_runnable(p_tcb)) {
503 dispatch();
504 }
505 ercd = E_OK;
506 }
507 else {
508 /*
509 * “ñd‘Ò‚¿ó‘Ô‚©‚ç‘Ò‚¿ó‘Ô‚Ö‚Ì‘JˆÚ
510 */
511 p_tcb->tstat &= ~TS_SUSPENDED;
512 LOG_TSKSTAT(p_tcb);
513 ercd = E_OK;
514 }
515 t_unlock_cpu();
516
517 error_exit:
518 LOG_RSM_TSK_LEAVE(ercd);
519 return(ercd);
520}
521
522#endif /* TOPPERS_rsm_tsk */
523
524/*
525 * Ž©ƒ^ƒXƒN‚Ì’x‰„
526 */
527#ifdef TOPPERS_dly_tsk
528
529ER
530dly_tsk(RELTIM dlytim)
531{
532 WINFO winfo;
533 TMEVTB tmevtb;
534 ER ercd;
535
536 LOG_DLY_TSK_ENTER(dlytim);
537 CHECK_DISPATCH();
538 CHECK_PAR(dlytim <= TMAX_RELTIM);
539
540 t_lock_cpu();
541 p_runtsk->tstat = (TS_WAITING | TS_WAIT_DLY);
542 (void) make_non_runnable(p_runtsk);
543 p_runtsk->p_winfo = &winfo;
544 winfo.p_tmevtb = &tmevtb;
545 tmevtb_enqueue(&tmevtb, dlytim, (CBACK) wait_tmout_ok, (void *) p_runtsk);
546 LOG_TSKSTAT(p_runtsk);
547 dispatch();
548 ercd = winfo.wercd;
549 t_unlock_cpu();
550
551 error_exit:
552 LOG_DLY_TSK_LEAVE(ercd);
553 return(ercd);
554}
555
556#endif /* TOPPERS_dly_tsk */
Note: See TracBrowser for help on using the repository browser.