[61] | 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-2008 by Embedded and Real-Time Systems Laboratory
|
---|
| 9 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
| 10 | *
|
---|
| 11 | * ãLì ÒÍCȺÌ(1)`(4)Ìðð½·êÉÀèC{\tgEF
|
---|
| 12 | * Ai{\tgEFAðüϵ½àÌðÜÞDȺ¯¶jðgpE¡»Eü
|
---|
| 13 | * ÏEÄzziȺCpÆÄÔj·é±Æð³Åø·éD
|
---|
| 14 | * (1) {\tgEFAð\[XR[hÌ`Åp·éêÉÍCãLÌì
|
---|
| 15 | * \¦C±Ìpð¨æѺL̳ÛØKèªC»ÌÜÜÌ`Å\[
|
---|
| 16 | * XR[hÉÜÜêÄ¢é±ÆD
|
---|
| 17 | * (2) {\tgEFAðCCu`®ÈÇC¼Ì\tgEFAJÉg
|
---|
| 18 | * pÅ«é`ÅÄzz·éêÉÍCÄzzɺ¤hL
|
---|
| 19 | gip
|
---|
| 20 | * Ò}j
|
---|
| 21 | AÈÇjÉCãLÌì \¦C±Ìpð¨æѺL
|
---|
| 22 | * ̳ÛØKèðfÚ·é±ÆD
|
---|
| 23 | * (3) {\tgEFAðC@íÉgÝÞÈÇC¼Ì\tgEFAJÉg
|
---|
| 24 | * pÅ«È¢`ÅÄzz·éêÉÍCÌ¢¸ê©Ìðð½·±
|
---|
| 25 | * ÆD
|
---|
| 26 | * (a) Äzzɺ¤hL
|
---|
| 27 | gipÒ}j
|
---|
| 28 | AÈÇjÉCãLÌ
|
---|
| 29 | * ì \¦C±Ìpð¨æѺL̳ÛØKèðfÚ·é±ÆD
|
---|
| 30 | * (b) ÄzzÌ`ÔðCÊÉèßéû@ÉæÁÄCTOPPERSvWFNgÉ
|
---|
| 31 | * ñ·é±ÆD
|
---|
| 32 | * (4) {\tgEFAÌpÉæè¼ÚIܽÍÔÚIɶ¶é¢©Èé¹
|
---|
| 33 | * Q©çàCãLì Ò¨æÑTOPPERSvWFNgðÆÓ·é±ÆD
|
---|
| 34 | * ܽC{\tgEFAÌ[UܽÍGh[U©çÌ¢©Èé
|
---|
| 35 | * RÉîÿ©çàCãLì Ò¨æÑTOPPERSvWFNgð
|
---|
| 36 | * ÆÓ·é±ÆD
|
---|
| 37 | *
|
---|
| 38 | * {\tgEFAÍC³ÛØÅñ³êÄ¢éàÌÅ éDãLì Ò¨
|
---|
| 39 | * æÑTOPPERSvWFNgÍC{\tgEFAÉÖµÄCÁèÌgpÚI
|
---|
| 40 | * ÉηéK«àÜßÄC¢©ÈéÛØàsíÈ¢DܽC{\tgEF
|
---|
| 41 | * AÌpÉæè¼ÚIܽÍÔÚIɶ¶½¢©Èé¹QÉÖµÄàC»
|
---|
| 42 | * ÌÓCðíÈ¢D
|
---|
| 43 | *
|
---|
| 44 | * @(#) $Id: semaphore.c 748 2008-03-07 17:18:06Z hiro $
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | /*
|
---|
| 48 | * Z}tH@\
|
---|
| 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[XO}NÌftHgè`
|
---|
| 59 | */
|
---|
| 60 | #ifndef LOG_SIG_SEM_ENTER
|
---|
| 61 | #define LOG_SIG_SEM_ENTER(semid)
|
---|
| 62 | #endif /* LOG_SIG_SEM_ENTER */
|
---|
| 63 |
|
---|
| 64 | #ifndef LOG_SIG_SEM_LEAVE
|
---|
| 65 | #define LOG_SIG_SEM_LEAVE(ercd)
|
---|
| 66 | #endif /* LOG_SIG_SEM_LEAVE */
|
---|
| 67 |
|
---|
| 68 | #ifndef LOG_ISIG_SEM_ENTER
|
---|
| 69 | #define LOG_ISIG_SEM_ENTER(semid)
|
---|
| 70 | #endif /* LOG_ISIG_SEM_ENTER */
|
---|
| 71 |
|
---|
| 72 | #ifndef LOG_ISIG_SEM_LEAVE
|
---|
| 73 | #define LOG_ISIG_SEM_LEAVE(ercd)
|
---|
| 74 | #endif /* LOG_ISIG_SEM_LEAVE */
|
---|
| 75 |
|
---|
| 76 | #ifndef LOG_WAI_SEM_ENTER
|
---|
| 77 | #define LOG_WAI_SEM_ENTER(semid)
|
---|
| 78 | #endif /* LOG_WAI_SEM_ENTER */
|
---|
| 79 |
|
---|
| 80 | #ifndef LOG_WAI_SEM_LEAVE
|
---|
| 81 | #define LOG_WAI_SEM_LEAVE(ercd)
|
---|
| 82 | #endif /* LOG_WAI_SEM_LEAVE */
|
---|
| 83 |
|
---|
| 84 | #ifndef LOG_POL_SEM_ENTER
|
---|
| 85 | #define LOG_POL_SEM_ENTER(semid)
|
---|
| 86 | #endif /* LOG_POL_SEM_ENTER */
|
---|
| 87 |
|
---|
| 88 | #ifndef LOG_POL_SEM_LEAVE
|
---|
| 89 | #define LOG_POL_SEM_LEAVE(ercd)
|
---|
| 90 | #endif /* LOG_POL_SEM_LEAVE */
|
---|
| 91 |
|
---|
| 92 | #ifndef LOG_TWAI_SEM_ENTER
|
---|
| 93 | #define LOG_TWAI_SEM_ENTER(semid, tmout)
|
---|
| 94 | #endif /* LOG_TWAI_SEM_ENTER */
|
---|
| 95 |
|
---|
| 96 | #ifndef LOG_TWAI_SEM_LEAVE
|
---|
| 97 | #define LOG_TWAI_SEM_LEAVE(ercd)
|
---|
| 98 | #endif /* LOG_TWAI_SEM_LEAVE */
|
---|
| 99 |
|
---|
| 100 | #ifndef LOG_INI_SEM_ENTER
|
---|
| 101 | #define LOG_INI_SEM_ENTER(semid)
|
---|
| 102 | #endif /* LOG_INI_SEM_ENTER */
|
---|
| 103 |
|
---|
| 104 | #ifndef LOG_INI_SEM_LEAVE
|
---|
| 105 | #define LOG_INI_SEM_LEAVE(ercd)
|
---|
| 106 | #endif /* LOG_INI_SEM_LEAVE */
|
---|
| 107 |
|
---|
| 108 | #ifndef LOG_REF_SEM_ENTER
|
---|
| 109 | #define LOG_REF_SEM_ENTER(semid, pk_rsem)
|
---|
| 110 | #endif /* LOG_REF_SEM_ENTER */
|
---|
| 111 |
|
---|
| 112 | #ifndef LOG_REF_SEM_LEAVE
|
---|
| 113 | #define LOG_REF_SEM_LEAVE(ercd, pk_rsem)
|
---|
| 114 | #endif /* LOG_REF_SEM_LEAVE */
|
---|
| 115 |
|
---|
| 116 | /*
|
---|
| 117 | * Z}tHÌ
|
---|
| 118 | */
|
---|
| 119 | #define tnum_sem ((uint_t)(tmax_semid - TMIN_SEMID + 1))
|
---|
| 120 |
|
---|
| 121 | /*
|
---|
| 122 | * Z}tHID©çZ}tHÇubNðæèo·½ßÌ}N
|
---|
| 123 | */
|
---|
| 124 | #define INDEX_SEM(semid) ((uint_t)((semid) - TMIN_SEMID))
|
---|
| 125 | #define get_semcb(semid) (&(semcb_table[INDEX_SEM(semid)]))
|
---|
| 126 |
|
---|
| 127 | /*
|
---|
| 128 | * Z}tH@\Ìú»
|
---|
| 129 | */
|
---|
| 130 | #ifdef TOPPERS_semini
|
---|
| 131 |
|
---|
| 132 | void
|
---|
| 133 | initialize_semaphore(void)
|
---|
| 134 | {
|
---|
| 135 | uint_t i;
|
---|
| 136 | SEMCB *p_semcb;
|
---|
| 137 |
|
---|
| 138 | for (p_semcb = semcb_table, i = 0; i < tnum_sem; p_semcb++, i++) {
|
---|
| 139 | queue_initialize(&(p_semcb->wait_queue));
|
---|
| 140 | p_semcb->p_seminib = &(seminib_table[i]);
|
---|
| 141 | p_semcb->semcnt = p_semcb->p_seminib->isemcnt;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | #endif /* TOPPERS_semini */
|
---|
| 146 |
|
---|
| 147 | /*
|
---|
| 148 | * Z}tH¹ÌÔp
|
---|
| 149 | */
|
---|
| 150 | #ifdef TOPPERS_sig_sem
|
---|
| 151 |
|
---|
| 152 | ER
|
---|
| 153 | sig_sem(ID semid)
|
---|
| 154 | {
|
---|
| 155 | SEMCB *p_semcb;
|
---|
| 156 | TCB *p_tcb;
|
---|
| 157 | ER ercd;
|
---|
| 158 |
|
---|
| 159 | LOG_SIG_SEM_ENTER(semid);
|
---|
| 160 | CHECK_TSKCTX_UNL();
|
---|
| 161 | CHECK_SEMID(semid);
|
---|
| 162 | p_semcb = get_semcb(semid);
|
---|
| 163 |
|
---|
| 164 | t_lock_cpu();
|
---|
| 165 | if (!queue_empty(&(p_semcb->wait_queue))) {
|
---|
| 166 | p_tcb = (TCB *) queue_delete_next(&(p_semcb->wait_queue));
|
---|
| 167 | if (wait_complete(p_tcb)) {
|
---|
| 168 | dispatch();
|
---|
| 169 | }
|
---|
| 170 | ercd = E_OK;
|
---|
| 171 | }
|
---|
| 172 | else if (p_semcb->semcnt < p_semcb->p_seminib->maxsem) {
|
---|
| 173 | p_semcb->semcnt += 1;
|
---|
| 174 | ercd = E_OK;
|
---|
| 175 | }
|
---|
| 176 | else {
|
---|
| 177 | ercd = E_QOVR;
|
---|
| 178 | }
|
---|
| 179 | t_unlock_cpu();
|
---|
| 180 |
|
---|
| 181 | error_exit:
|
---|
| 182 | LOG_SIG_SEM_LEAVE(ercd);
|
---|
| 183 | return(ercd);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | #endif /* TOPPERS_sig_sem */
|
---|
| 187 |
|
---|
| 188 | /*
|
---|
| 189 | * Z}tH¹ÌÔpiñ^XNReLXgpj
|
---|
| 190 | */
|
---|
| 191 | #ifdef TOPPERS_isig_sem
|
---|
| 192 |
|
---|
| 193 | ER
|
---|
| 194 | isig_sem(ID semid)
|
---|
| 195 | {
|
---|
| 196 | SEMCB *p_semcb;
|
---|
| 197 | TCB *p_tcb;
|
---|
| 198 | ER ercd;
|
---|
| 199 |
|
---|
| 200 | LOG_ISIG_SEM_ENTER(semid);
|
---|
| 201 | CHECK_INTCTX_UNL();
|
---|
| 202 | CHECK_SEMID(semid);
|
---|
| 203 | p_semcb = get_semcb(semid);
|
---|
| 204 |
|
---|
| 205 | i_lock_cpu();
|
---|
| 206 | if (!queue_empty(&(p_semcb->wait_queue))) {
|
---|
| 207 | p_tcb = (TCB *) queue_delete_next(&(p_semcb->wait_queue));
|
---|
| 208 | if (wait_complete(p_tcb)) {
|
---|
| 209 | reqflg = true;
|
---|
| 210 | }
|
---|
| 211 | ercd = E_OK;
|
---|
| 212 | }
|
---|
| 213 | else if (p_semcb->semcnt < p_semcb->p_seminib->maxsem) {
|
---|
| 214 | p_semcb->semcnt += 1;
|
---|
| 215 | ercd = E_OK;
|
---|
| 216 | }
|
---|
| 217 | else {
|
---|
| 218 | ercd = E_QOVR;
|
---|
| 219 | }
|
---|
| 220 | i_unlock_cpu();
|
---|
| 221 |
|
---|
| 222 | error_exit:
|
---|
| 223 | LOG_ISIG_SEM_LEAVE(ercd);
|
---|
| 224 | return(ercd);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | #endif /* TOPPERS_isig_sem */
|
---|
| 228 |
|
---|
| 229 | /*
|
---|
| 230 | * Z}tH¹Ìl¾
|
---|
| 231 | */
|
---|
| 232 | #ifdef TOPPERS_wai_sem
|
---|
| 233 |
|
---|
| 234 | ER
|
---|
| 235 | wai_sem(ID semid)
|
---|
| 236 | {
|
---|
| 237 | SEMCB *p_semcb;
|
---|
| 238 | WINFO_SEM winfo_sem;
|
---|
| 239 | ER ercd;
|
---|
| 240 |
|
---|
| 241 | LOG_WAI_SEM_ENTER(semid);
|
---|
| 242 | CHECK_DISPATCH();
|
---|
| 243 | CHECK_SEMID(semid);
|
---|
| 244 | p_semcb = get_semcb(semid);
|
---|
| 245 |
|
---|
| 246 | t_lock_cpu();
|
---|
| 247 | if (p_semcb->semcnt >= 1) {
|
---|
| 248 | p_semcb->semcnt -= 1;
|
---|
| 249 | ercd = E_OK;
|
---|
| 250 | }
|
---|
| 251 | else {
|
---|
| 252 | p_runtsk->tstat = (TS_WAITING | TS_WAIT_SEM);
|
---|
| 253 | wobj_make_wait((WOBJCB *) p_semcb, (WINFO_WOBJ *) &winfo_sem);
|
---|
| 254 | dispatch();
|
---|
| 255 | ercd = winfo_sem.winfo.wercd;
|
---|
| 256 | }
|
---|
| 257 | t_unlock_cpu();
|
---|
| 258 |
|
---|
| 259 | error_exit:
|
---|
| 260 | LOG_WAI_SEM_LEAVE(ercd);
|
---|
| 261 | return(ercd);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | #endif /* TOPPERS_wai_sem */
|
---|
| 265 |
|
---|
| 266 | /*
|
---|
| 267 | * Z}tH¹Ìl¾i|[Oj
|
---|
| 268 | */
|
---|
| 269 | #ifdef TOPPERS_pol_sem
|
---|
| 270 |
|
---|
| 271 | ER
|
---|
| 272 | pol_sem(ID semid)
|
---|
| 273 | {
|
---|
| 274 | SEMCB *p_semcb;
|
---|
| 275 | ER ercd;
|
---|
| 276 |
|
---|
| 277 | LOG_POL_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->semcnt >= 1) {
|
---|
| 284 | p_semcb->semcnt -= 1;
|
---|
| 285 | ercd = E_OK;
|
---|
| 286 | }
|
---|
| 287 | else {
|
---|
| 288 | ercd = E_TMOUT;
|
---|
| 289 | }
|
---|
| 290 | t_unlock_cpu();
|
---|
| 291 |
|
---|
| 292 | error_exit:
|
---|
| 293 | LOG_POL_SEM_LEAVE(ercd);
|
---|
| 294 | return(ercd);
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | #endif /* TOPPERS_pol_sem */
|
---|
| 298 |
|
---|
| 299 | /*
|
---|
| 300 | * Z}tH¹Ìl¾i^CAEg èj
|
---|
| 301 | */
|
---|
| 302 | #ifdef TOPPERS_twai_sem
|
---|
| 303 |
|
---|
| 304 | ER
|
---|
| 305 | twai_sem(ID semid, TMO tmout)
|
---|
| 306 | {
|
---|
| 307 | SEMCB *p_semcb;
|
---|
| 308 | WINFO_SEM winfo_sem;
|
---|
| 309 | TMEVTB tmevtb;
|
---|
| 310 | ER ercd;
|
---|
| 311 |
|
---|
| 312 | LOG_TWAI_SEM_ENTER(semid, tmout);
|
---|
| 313 | CHECK_DISPATCH();
|
---|
| 314 | CHECK_SEMID(semid);
|
---|
| 315 | CHECK_TMOUT(tmout);
|
---|
| 316 | p_semcb = get_semcb(semid);
|
---|
| 317 |
|
---|
| 318 | t_lock_cpu();
|
---|
| 319 | if (p_semcb->semcnt >= 1) {
|
---|
| 320 | p_semcb->semcnt -= 1;
|
---|
| 321 | ercd = E_OK;
|
---|
| 322 | }
|
---|
| 323 | else if (tmout == TMO_POL) {
|
---|
| 324 | ercd = E_TMOUT;
|
---|
| 325 | }
|
---|
| 326 | else {
|
---|
| 327 | p_runtsk->tstat = (TS_WAITING | TS_WAIT_SEM);
|
---|
| 328 | wobj_make_wait_tmout((WOBJCB *) p_semcb, (WINFO_WOBJ *) &winfo_sem,
|
---|
| 329 | &tmevtb, tmout);
|
---|
| 330 | dispatch();
|
---|
| 331 | ercd = winfo_sem.winfo.wercd;
|
---|
| 332 | }
|
---|
| 333 | t_unlock_cpu();
|
---|
| 334 |
|
---|
| 335 | error_exit:
|
---|
| 336 | LOG_TWAI_SEM_LEAVE(ercd);
|
---|
| 337 | return(ercd);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | #endif /* TOPPERS_twai_sem */
|
---|
| 341 |
|
---|
| 342 | /*
|
---|
| 343 | * Z}tHÌÄú»
|
---|
| 344 | */
|
---|
| 345 | #ifdef TOPPERS_ini_sem
|
---|
| 346 |
|
---|
| 347 | ER
|
---|
| 348 | ini_sem(ID semid)
|
---|
| 349 | {
|
---|
| 350 | SEMCB *p_semcb;
|
---|
| 351 | bool_t dspreq;
|
---|
| 352 | ER ercd;
|
---|
| 353 |
|
---|
| 354 | LOG_INI_SEM_ENTER(semid);
|
---|
| 355 | CHECK_TSKCTX_UNL();
|
---|
| 356 | CHECK_SEMID(semid);
|
---|
| 357 | p_semcb = get_semcb(semid);
|
---|
| 358 |
|
---|
| 359 | t_lock_cpu();
|
---|
| 360 | dspreq = init_wait_queue(&(p_semcb->wait_queue));
|
---|
| 361 | p_semcb->semcnt = p_semcb->p_seminib->isemcnt;
|
---|
| 362 | if (dspreq) {
|
---|
| 363 | dispatch();
|
---|
| 364 | }
|
---|
| 365 | ercd = E_OK;
|
---|
| 366 | t_unlock_cpu();
|
---|
| 367 |
|
---|
| 368 | error_exit:
|
---|
| 369 | LOG_INI_SEM_LEAVE(ercd);
|
---|
| 370 | return(ercd);
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | #endif /* TOPPERS_ini_sem */
|
---|
| 374 |
|
---|
| 375 | /*
|
---|
| 376 | * Z}tHÌóÔQÆ
|
---|
| 377 | */
|
---|
| 378 | #ifdef TOPPERS_ref_sem
|
---|
| 379 |
|
---|
| 380 | ER
|
---|
| 381 | ref_sem(ID semid, T_RSEM *pk_rsem)
|
---|
| 382 | {
|
---|
| 383 | SEMCB *p_semcb;
|
---|
| 384 | ER ercd;
|
---|
| 385 |
|
---|
| 386 | LOG_REF_SEM_ENTER(semid, pk_rsem);
|
---|
| 387 | CHECK_TSKCTX_UNL();
|
---|
| 388 | CHECK_SEMID(semid);
|
---|
| 389 | p_semcb = get_semcb(semid);
|
---|
| 390 |
|
---|
| 391 | t_lock_cpu();
|
---|
| 392 | pk_rsem->wtskid = wait_tskid(&(p_semcb->wait_queue));
|
---|
| 393 | pk_rsem->semcnt = p_semcb->semcnt;
|
---|
| 394 | ercd = E_OK;
|
---|
| 395 | t_unlock_cpu();
|
---|
| 396 |
|
---|
| 397 | error_exit:
|
---|
| 398 | LOG_REF_SEM_LEAVE(ercd, pk_rsem);
|
---|
| 399 | return(ercd);
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | #endif /* TOPPERS_ref_sem */
|
---|