gonzui


Format: Advanced Search

mtkernel_3/lib/libtk/fastmlock.cbare sourcepermlink (0.01 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    micro T-Kernel 3.00.00
    4:  *
    5:  *    Copyright (C) 2006-2019 by Ken Sakamura.
    6:  *    This software is distributed under the T-License 2.1.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by TRON Forum(http://www.tron.org) at 2019/12/11.
   10:  *
   11:  *----------------------------------------------------------------------
   12:  */
   13: 
   14: /*
   15:  *      fastmlock.c
   16:  *
   17:  *      High-speed exclusive control multi-lock 
   18:  */
   19: 
   20: #include <config.h>
   21: #include <tk/tkernel.h>
   22: 
   23: #if USE_MULTI_LOCK
   24: /* ------------------------------------------------------------------------ */
   25: /*
   26:  *      void INC( INT *val )         increment 
   27:  *      void DEC( INT *val )         decrement 
   28:  *      BOOL BTS( UINT *val, INT no )        bit test and set 
   29:  *      void BR( UINT *val, INT no ) bit reset 
   30:  *
   31:  *      The above must be operated exclusively.
   32:  */
   33: 
   34: Inline void INC( INT *val )
   35: {
   36:         UINT   imask;
   37: 
   38:         DI(imask);
   39:         (*val)++;
   40:         EI(imask);
   41: }
   42: 
   43: Inline void DEC( INT *val )
   44: {
   45:         UINT   imask;
   46: 
   47:         DI(imask);
   48:         (*val)--;
   49:         EI(imask);
   50: }
   51: 
   52: Inline BOOL BTS( UINT *val, INT no )
   53: {
   54:         UINT   imask;
   55:         UINT   b;
   56:         UINT   bm = (UINT)(1 << no);
   57: 
   58:         DI(imask);
   59:         b = *val & bm;
   60:         *val |= bm;
   61:         EI(imask);
   62:         return (BOOL)b;
   63: }
   64: 
   65: Inline void BR( UINT *val, INT no )
   66: {
   67:         UINT   imask;
   68: 
   69:         DI(imask);
   70:         *val &= ~(UINT)(1 << no);
   71:         EI(imask);
   72: }
   73: 
   74: 
   75: /*
   76:  * Lock with wait time designation 
   77:  *      no   lock number 0 - 31 
   78:  */
   79: EXPORT ER MLockTmo( FastMLock *lock, INT no, TMO tmo )
   80: {
   81:         UINT   ptn = (UINT)(1 << no);
   82:         UINT   flg;
   83:         ER     ercd;
   84: 
   85:         INC(&lock->wai);
   86:         for ( ;; ) {
   87:                 if ( !BTS(&lock->flg, no) ) {
   88:                         ercd = E_OK;
   89:                         break;
   90:                 }
   91: 
   92:                 ercd = tk_wai_flg(lock->id, ptn, TWF_ORW|TWF_BITCLR, &flg, tmo);
   93:                 if ( ercd < E_OK ) {
   94:                         break;
   95:                 }
   96:         }
   97:         DEC(&lock->wai);
   98: 
   99:         return ercd;
  100: }
  101: 
  102: /*
  103:  * Lock 
  104:  *      no   Lock number 0 - 31 
  105:  */
  106: EXPORT ER MLock( FastMLock *lock, INT no )
  107: {
  108:         return MLockTmo(lock, no, TMO_FEVR);
  109: }
  110: 
  111: /*
  112:  * Lock release 
  113:  *      no   Lock number 0 - 31 
  114:  */
  115: EXPORT ER MUnlock( FastMLock *lock, INT no )
  116: {
  117:         UINT   ptn = (UINT)(1 << no);
  118:         ER     ercd;
  119: 
  120:         BR(&lock->flg, no);
  121:         ercd = ( lock->wai == 0 )? E_OK: tk_set_flg(lock->id, ptn);
  122: 
  123:         return ercd;
  124: }
  125: 
  126: /*
  127:  * Create multi-lock 
  128:  */
  129: EXPORT ER CreateMLock( FastMLock *lock, CONST UB *name )
  130: {
  131:         T_CFLG cflg;
  132:         ER     ercd;
  133: 
  134:         cflg.flgatr  = TA_TPRI | TA_WMUL;
  135:         cflg.iflgptn = 0;
  136:         SetOBJNAME(cflg.exinf, "MLOC");
  137: 
  138:         lock->id = ercd = tk_cre_flg(&cflg);
  139:         if ( ercd < E_OK ) {
  140:                 return ercd;
  141:         }
  142: 
  143:         lock->wai = 0;
  144:         lock->flg = 0;
  145:         lock->name = name;
  146: 
  147:         return E_OK;
  148: }
  149: 
  150: /*
  151:  * Delete multi-lock 
  152:  */
  153: EXPORT ER DeleteMLock( FastMLock *lock )
  154: {
  155:         ER     ercd;
  156: 
  157:         if ( lock->id <= 0 ) {
  158:                 return E_PAR;
  159:         }
  160: 
  161:         ercd = tk_del_flg(lock->id);
  162:         if ( ercd < E_OK ) {
  163:                 return ercd;
  164:         }
  165: 
  166:         lock->id = 0;
  167: 
  168:         return E_OK;
  169: }
  170: 
  171: #endif /* USE_MULTI_LOCK */