source: UsbWattMeter/trunk/asp_dcre/kernel/semaphore.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: 11.0 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-2012 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: semaphore.c 164 2016-03-07 11:33:50Z coas-nagasima $
45 */
46
47/*
48 * ƒZƒ}ƒtƒH‹@”\
49 */
50
51#include "kernel_impl.h"
52#include "check.h"
53#include "task.h"
54#include "wait.h"
55#include "semaphore.h"
56
57/*
58 * ƒgƒŒ[ƒXƒƒOƒ}ƒNƒ‚̃fƒtƒHƒ‹ƒg’è‹`
59 */
60#ifndef LOG_ACRE_SEM_ENTER
61#define LOG_ACRE_SEM_ENTER(pk_csem)
62#endif /* LOG_ACRE_SEM_ENTER */
63
64#ifndef LOG_ACRE_SEM_LEAVE
65#define LOG_ACRE_SEM_LEAVE(ercd)
66#endif /* LOG_ACRE_SEM_LEAVE */
67
68#ifndef LOG_DEL_SEM_ENTER
69#define LOG_DEL_SEM_ENTER(semid)
70#endif /* LOG_DEL_SEM_ENTER */
71
72#ifndef LOG_DEL_SEM_LEAVE
73#define LOG_DEL_SEM_LEAVE(ercd)
74#endif /* LOG_DEL_SEM_LEAVE */
75
76#ifndef LOG_SIG_SEM_ENTER
77#define LOG_SIG_SEM_ENTER(semid)
78#endif /* LOG_SIG_SEM_ENTER */
79
80#ifndef LOG_SIG_SEM_LEAVE
81#define LOG_SIG_SEM_LEAVE(ercd)
82#endif /* LOG_SIG_SEM_LEAVE */
83
84#ifndef LOG_ISIG_SEM_ENTER
85#define LOG_ISIG_SEM_ENTER(semid)
86#endif /* LOG_ISIG_SEM_ENTER */
87
88#ifndef LOG_ISIG_SEM_LEAVE
89#define LOG_ISIG_SEM_LEAVE(ercd)
90#endif /* LOG_ISIG_SEM_LEAVE */
91
92#ifndef LOG_WAI_SEM_ENTER
93#define LOG_WAI_SEM_ENTER(semid)
94#endif /* LOG_WAI_SEM_ENTER */
95
96#ifndef LOG_WAI_SEM_LEAVE
97#define LOG_WAI_SEM_LEAVE(ercd)
98#endif /* LOG_WAI_SEM_LEAVE */
99
100#ifndef LOG_POL_SEM_ENTER
101#define LOG_POL_SEM_ENTER(semid)
102#endif /* LOG_POL_SEM_ENTER */
103
104#ifndef LOG_POL_SEM_LEAVE
105#define LOG_POL_SEM_LEAVE(ercd)
106#endif /* LOG_POL_SEM_LEAVE */
107
108#ifndef LOG_TWAI_SEM_ENTER
109#define LOG_TWAI_SEM_ENTER(semid, tmout)
110#endif /* LOG_TWAI_SEM_ENTER */
111
112#ifndef LOG_TWAI_SEM_LEAVE
113#define LOG_TWAI_SEM_LEAVE(ercd)
114#endif /* LOG_TWAI_SEM_LEAVE */
115
116#ifndef LOG_INI_SEM_ENTER
117#define LOG_INI_SEM_ENTER(semid)
118#endif /* LOG_INI_SEM_ENTER */
119
120#ifndef LOG_INI_SEM_LEAVE
121#define LOG_INI_SEM_LEAVE(ercd)
122#endif /* LOG_INI_SEM_LEAVE */
123
124#ifndef LOG_REF_SEM_ENTER
125#define LOG_REF_SEM_ENTER(semid, pk_rsem)
126#endif /* LOG_REF_SEM_ENTER */
127
128#ifndef LOG_REF_SEM_LEAVE
129#define LOG_REF_SEM_LEAVE(ercd, pk_rsem)
130#endif /* LOG_REF_SEM_LEAVE */
131
132/*
133 * ƒZƒ}ƒtƒH‚̐”
134 */
135#define tnum_sem ((uint_t)(tmax_semid - TMIN_SEMID + 1))
136#define tnum_ssem ((uint_t)(tmax_ssemid - TMIN_SEMID + 1))
137
138/*
139 * ƒZƒ}ƒtƒHID‚©‚çƒZƒ}ƒtƒHŠÇ—ƒuƒƒbƒN‚ðŽæ‚èo‚·‚½‚߂̃}ƒNƒ
140 */
141#define INDEX_SEM(semid) ((uint_t)((semid) - TMIN_SEMID))
142#define get_semcb(semid) (&(semcb_table[INDEX_SEM(semid)]))
143
144#ifdef TOPPERS_semini
145
146/*
147 * Žg—p‚µ‚Ä‚¢‚È‚¢ƒZƒ}ƒtƒHŠÇ—ƒuƒƒbƒN‚̃ŠƒXƒg
148 */
149QUEUE free_semcb;
150
151/*
152 * ƒZƒ}ƒtƒH‹@”\‚̏‰Šú‰»
153 */
154void
155initialize_semaphore(void)
156{
157 uint_t i, j;
158 SEMCB *p_semcb;
159 SEMINIB *p_seminib;
160
161 for (i = 0; i < tnum_ssem; i++) {
162 p_semcb = &(semcb_table[i]);
163 queue_initialize(&(p_semcb->wait_queue));
164 p_semcb->p_seminib = &(seminib_table[i]);
165 p_semcb->semcnt = p_semcb->p_seminib->isemcnt;
166 }
167 queue_initialize(&free_semcb);
168 for (j = 0; i < tnum_sem; i++, j++) {
169 p_semcb = &(semcb_table[i]);
170 p_seminib = &(aseminib_table[j]);
171 p_seminib->sematr = TA_NOEXS;
172 p_semcb->p_seminib = ((const SEMINIB *) p_seminib);
173 queue_insert_prev(&free_semcb, &(p_semcb->wait_queue));
174 }
175}
176
177#endif /* TOPPERS_semini */
178
179/*
180 * ƒZƒ}ƒtƒH‚̐¶¬
181 */
182#ifdef TOPPERS_acre_sem
183
184ER_UINT
185acre_sem(const T_CSEM *pk_csem)
186{
187 SEMCB *p_semcb;
188 SEMINIB *p_seminib;
189 ER ercd;
190
191 LOG_ACRE_SEM_ENTER(pk_csem);
192 CHECK_TSKCTX_UNL();
193 CHECK_RSATR(pk_csem->sematr, TA_TPRI);
194 CHECK_PAR(0 <= pk_csem->isemcnt && pk_csem->isemcnt <= pk_csem->maxsem);
195 CHECK_PAR(1 <= pk_csem->maxsem && pk_csem->maxsem <= TMAX_MAXSEM);
196
197 t_lock_cpu();
198 if (tnum_sem == 0 || queue_empty(&free_semcb)) {
199 ercd = E_NOID;
200 }
201 else {
202 p_semcb = ((SEMCB *) queue_delete_next(&free_semcb));
203 p_seminib = (SEMINIB *)(p_semcb->p_seminib);
204 p_seminib->sematr = pk_csem->sematr;
205 p_seminib->isemcnt = pk_csem->isemcnt;
206 p_seminib->maxsem = pk_csem->maxsem;
207
208 queue_initialize(&(p_semcb->wait_queue));
209 p_semcb->semcnt = p_semcb->p_seminib->isemcnt;
210 ercd = SEMID(p_semcb);
211 }
212 t_unlock_cpu();
213
214 error_exit:
215 LOG_ACRE_SEM_LEAVE(ercd);
216 return(ercd);
217}
218
219#endif /* TOPPERS_acre_sem */
220
221/*
222 * ƒZƒ}ƒtƒH‚̍폜
223 */
224#ifdef TOPPERS_del_sem
225
226ER
227del_sem(ID semid)
228{
229 SEMCB *p_semcb;
230 SEMINIB *p_seminib;
231 bool_t dspreq;
232 ER ercd;
233
234 LOG_DEL_SEM_ENTER(semid);
235 CHECK_TSKCTX_UNL();
236 CHECK_SEMID(semid);
237 p_semcb = get_semcb(semid);
238
239 t_lock_cpu();
240 if (p_semcb->p_seminib->sematr == TA_NOEXS) {
241 ercd = E_NOEXS;
242 }
243 else if (SEMID(p_semcb) > tmax_ssemid) {
244 dspreq = init_wait_queue(&(p_semcb->wait_queue));
245 p_seminib = (SEMINIB *)(p_semcb->p_seminib);
246 p_seminib->sematr = TA_NOEXS;
247 queue_insert_prev(&free_semcb, &(p_semcb->wait_queue));
248 if (dspreq) {
249 dispatch();
250 }
251 ercd = E_OK;
252 }
253 else {
254 ercd = E_OBJ;
255 }
256 t_unlock_cpu();
257
258 error_exit:
259 LOG_DEL_SEM_LEAVE(ercd);
260 return(ercd);
261}
262
263#endif /* TOPPERS_del_sem */
264
265/*
266 * ƒZƒ}ƒtƒHŽ‘Œ¹‚Ì•Ô‹p
267 */
268#ifdef TOPPERS_sig_sem
269
270ER
271sig_sem(ID semid)
272{
273 SEMCB *p_semcb;
274 TCB *p_tcb;
275 ER ercd;
276
277 LOG_SIG_SEM_ENTER(semid);
278 CHECK_TSKCTX_UNL();
279 CHECK_SEMID(semid);
280 p_semcb = get_semcb(semid);
281
282 t_lock_cpu();
283 if (p_semcb->p_seminib->sematr == TA_NOEXS) {
284 ercd = E_NOEXS;
285 }
286 else if (!queue_empty(&(p_semcb->wait_queue))) {
287 p_tcb = (TCB *) queue_delete_next(&(p_semcb->wait_queue));
288 if (wait_complete(p_tcb)) {
289 dispatch();
290 }
291 ercd = E_OK;
292 }
293 else if (p_semcb->semcnt < p_semcb->p_seminib->maxsem) {
294 p_semcb->semcnt += 1;
295 ercd = E_OK;
296 }
297 else {
298 ercd = E_QOVR;
299 }
300 t_unlock_cpu();
301
302 error_exit:
303 LOG_SIG_SEM_LEAVE(ercd);
304 return(ercd);
305}
306
307#endif /* TOPPERS_sig_sem */
308
309/*
310 * ƒZƒ}ƒtƒHŽ‘Œ¹‚Ì•Ô‹pi”ñƒ^ƒXƒNƒRƒ“ƒeƒLƒXƒg—pj
311 */
312#ifdef TOPPERS_isig_sem
313
314ER
315isig_sem(ID semid)
316{
317 SEMCB *p_semcb;
318 TCB *p_tcb;
319 ER ercd;
320
321 LOG_ISIG_SEM_ENTER(semid);
322 CHECK_INTCTX_UNL();
323 CHECK_SEMID(semid);
324 p_semcb = get_semcb(semid);
325
326 i_lock_cpu();
327 if (p_semcb->p_seminib->sematr == TA_NOEXS) {
328 ercd = E_NOEXS;
329 }
330 else if (!queue_empty(&(p_semcb->wait_queue))) {
331 p_tcb = (TCB *) queue_delete_next(&(p_semcb->wait_queue));
332 if (wait_complete(p_tcb)) {
333 reqflg = true;
334 }
335 ercd = E_OK;
336 }
337 else if (p_semcb->semcnt < p_semcb->p_seminib->maxsem) {
338 p_semcb->semcnt += 1;
339 ercd = E_OK;
340 }
341 else {
342 ercd = E_QOVR;
343 }
344 i_unlock_cpu();
345
346 error_exit:
347 LOG_ISIG_SEM_LEAVE(ercd);
348 return(ercd);
349}
350
351#endif /* TOPPERS_isig_sem */
352
353/*
354 * ƒZƒ}ƒtƒHŽ‘Œ¹‚ÌŠl“¾
355 */
356#ifdef TOPPERS_wai_sem
357
358ER
359wai_sem(ID semid)
360{
361 SEMCB *p_semcb;
362 WINFO_SEM winfo_sem;
363 ER ercd;
364
365 LOG_WAI_SEM_ENTER(semid);
366 CHECK_DISPATCH();
367 CHECK_SEMID(semid);
368 p_semcb = get_semcb(semid);
369
370 t_lock_cpu();
371 if (p_semcb->p_seminib->sematr == TA_NOEXS) {
372 ercd = E_NOEXS;
373 }
374 else if (p_semcb->semcnt >= 1) {
375 p_semcb->semcnt -= 1;
376 ercd = E_OK;
377 }
378 else {
379 p_runtsk->tstat = (TS_WAITING | TS_WAIT_SEM);
380 wobj_make_wait((WOBJCB *) p_semcb, (WINFO_WOBJ *) &winfo_sem);
381 dispatch();
382 ercd = winfo_sem.winfo.wercd;
383 }
384 t_unlock_cpu();
385
386 error_exit:
387 LOG_WAI_SEM_LEAVE(ercd);
388 return(ercd);
389}
390
391#endif /* TOPPERS_wai_sem */
392
393/*
394 * ƒZƒ}ƒtƒHŽ‘Œ¹‚ÌŠl“¾iƒ|[ƒŠƒ“ƒOj
395 */
396#ifdef TOPPERS_pol_sem
397
398ER
399pol_sem(ID semid)
400{
401 SEMCB *p_semcb;
402 ER ercd;
403
404 LOG_POL_SEM_ENTER(semid);
405 CHECK_TSKCTX_UNL();
406 CHECK_SEMID(semid);
407 p_semcb = get_semcb(semid);
408
409 t_lock_cpu();
410 if (p_semcb->p_seminib->sematr == TA_NOEXS) {
411 ercd = E_NOEXS;
412 }
413 else if (p_semcb->semcnt >= 1) {
414 p_semcb->semcnt -= 1;
415 ercd = E_OK;
416 }
417 else {
418 ercd = E_TMOUT;
419 }
420 t_unlock_cpu();
421
422 error_exit:
423 LOG_POL_SEM_LEAVE(ercd);
424 return(ercd);
425}
426
427#endif /* TOPPERS_pol_sem */
428
429/*
430 * ƒZƒ}ƒtƒHŽ‘Œ¹‚ÌŠl“¾iƒ^ƒCƒ€ƒAƒEƒg‚ ‚èj
431 */
432#ifdef TOPPERS_twai_sem
433
434ER
435twai_sem(ID semid, TMO tmout)
436{
437 SEMCB *p_semcb;
438 WINFO_SEM winfo_sem;
439 TMEVTB tmevtb;
440 ER ercd;
441
442 LOG_TWAI_SEM_ENTER(semid, tmout);
443 CHECK_DISPATCH();
444 CHECK_SEMID(semid);
445 CHECK_TMOUT(tmout);
446 p_semcb = get_semcb(semid);
447
448 t_lock_cpu();
449 if (p_semcb->p_seminib->sematr == TA_NOEXS) {
450 ercd = E_NOEXS;
451 }
452 else if (p_semcb->semcnt >= 1) {
453 p_semcb->semcnt -= 1;
454 ercd = E_OK;
455 }
456 else if (tmout == TMO_POL) {
457 ercd = E_TMOUT;
458 }
459 else {
460 p_runtsk->tstat = (TS_WAITING | TS_WAIT_SEM);
461 wobj_make_wait_tmout((WOBJCB *) p_semcb, (WINFO_WOBJ *) &winfo_sem,
462 &tmevtb, tmout);
463 dispatch();
464 ercd = winfo_sem.winfo.wercd;
465 }
466 t_unlock_cpu();
467
468 error_exit:
469 LOG_TWAI_SEM_LEAVE(ercd);
470 return(ercd);
471}
472
473#endif /* TOPPERS_twai_sem */
474
475/*
476 * ƒZƒ}ƒtƒH‚̍ú‰»
477 */
478#ifdef TOPPERS_ini_sem
479
480ER
481ini_sem(ID semid)
482{
483 SEMCB *p_semcb;
484 bool_t dspreq;
485 ER ercd;
486
487 LOG_INI_SEM_ENTER(semid);
488 CHECK_TSKCTX_UNL();
489 CHECK_SEMID(semid);
490 p_semcb = get_semcb(semid);
491
492 t_lock_cpu();
493 if (p_semcb->p_seminib->sematr == TA_NOEXS) {
494 ercd = E_NOEXS;
495 }
496 else {
497 dspreq = init_wait_queue(&(p_semcb->wait_queue));
498 p_semcb->semcnt = p_semcb->p_seminib->isemcnt;
499 if (dspreq) {
500 dispatch();
501 }
502 ercd = E_OK;
503 }
504 t_unlock_cpu();
505
506 error_exit:
507 LOG_INI_SEM_LEAVE(ercd);
508 return(ercd);
509}
510
511#endif /* TOPPERS_ini_sem */
512
513/*
514 * ƒZƒ}ƒtƒH‚̏ó‘ÔŽQÆ
515 */
516#ifdef TOPPERS_ref_sem
517
518ER
519ref_sem(ID semid, T_RSEM *pk_rsem)
520{
521 SEMCB *p_semcb;
522 ER ercd;
523
524 LOG_REF_SEM_ENTER(semid, pk_rsem);
525 CHECK_TSKCTX_UNL();
526 CHECK_SEMID(semid);
527 p_semcb = get_semcb(semid);
528
529 t_lock_cpu();
530 if (p_semcb->p_seminib->sematr == TA_NOEXS) {
531 ercd = E_NOEXS;
532 }
533 else {
534 pk_rsem->wtskid = wait_tskid(&(p_semcb->wait_queue));
535 pk_rsem->semcnt = p_semcb->semcnt;
536 ercd = E_OK;
537 }
538 t_unlock_cpu();
539
540 error_exit:
541 LOG_REF_SEM_LEAVE(ercd, pk_rsem);
542 return(ercd);
543}
544
545#endif /* TOPPERS_ref_sem */
Note: See TracBrowser for help on using the repository browser.