gonzui


Format: Advanced Search

tkernel_2/lib/libtk/src/fastmlock.cbare sourcepermlink (0.03 seconds)

Search this content:

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