gonzui


Format: Advanced Search

t2ex/t2ex_source/lib/libtk/src_t2ex/fastumlock.cbare sourcepermlink (0.03 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T2EX Software Package
    4:  *
    5:  *    Copyright 2012 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 2012/12/12.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/04.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: /*
   15:  * This software package is available for use, modification, 
   16:  * and redistribution in accordance with the terms of the attached 
   17:  * T-License 2.x.
   18:  * If you want to redistribute the source code, you need to attach 
   19:  * the T-License 2.x document.
   20:  * There's no obligation to publish the content, and no obligation 
   21:  * to disclose it to the TRON Forum if you have modified the 
   22:  * software package.
   23:  * You can also distribute the modified source code. In this case, 
   24:  * please register the modification to T-Kernel traceability service.
   25:  * People can know the history of modifications by the service, 
   26:  * and can be sure that the version you have inherited some 
   27:  * modification of a particular version or not.
   28:  *
   29:  *    http://trace.tron.org/tk/?lang=en
   30:  *    http://trace.tron.org/tk/?lang=ja
   31:  *
   32:  * As per the provisions of the T-License 2.x, TRON Forum ensures that 
   33:  * the portion of the software that is copyrighted by Ken Sakamura or 
   34:  * the TRON Forum does not infringe the copyrights of a third party.
   35:  * However, it does not make any warranty other than this.
   36:  * DISCLAIMER: TRON Forum and Ken Sakamura shall not be held
   37:  * responsible for any consequences or damages caused directly or
   38:  * indirectly by the use of this software package.
   39:  *
   40:  * The source codes in bsd_source.tar.gz in this software package are 
   41:  * derived from NetBSD or OpenBSD and not covered under T-License 2.x.
   42:  * They need to be changed or redistributed according to the 
   43:  * representation of each source header.
   44:  */
   45: 
   46: /*
   47:  *      @(#)fastumlock.c (libtk)
   48:  *
   49:  *      User-mode high-speed exclusive control multi-lock
   50:  */
   51: 
   52: #include <basic.h>
   53: #include <tk/tkernel.h>
   54: #include <t2ex/util.h>
   55: #include <libstr.h>
   56: 
   57: #define USE_ATOMIC_INT ( ATOMIC_INC_USER_MODE && ATOMIC_DEC_USER_MODE && ATOMIC_BITSET_USER_MODE && ATOMIC_BITCLR_USER_MODE  )
   58: 
   59: /* ------------------------------------------------------------------------ */
   60: 
   61: /*
   62:  * Lock with wait time designation
   63:  *      no   lock number 0 - 31
   64:  */
   65: EXPORT ER UMLockTmo( FastUMLock *lock, INT no, TMO tmo )
   66: {
   67:         UINT   ptn = (UINT)(1 << no);
   68:         UINT   flg;
   69:         ER     ercd;
   70: 
   71: #if USE_ATOMIC_INT
   72:         atomic_inc(&lock->wai);
   73:         for ( ;; ) {
   74:                 if ( !(atomic_bitset(&lock->flg, ptn) & ptn) ) {
   75:                         ercd = E_OK;
   76:                         break;
   77:                 }
   78: 
   79:                 ercd = tk_wai_flg(lock->id, ptn, TWF_ORW|TWF_BITCLR, &flg, tmo);
   80:                 if ( ercd < E_OK ) {
   81:                         break;
   82:                 }
   83:         }
   84:         atomic_dec(&lock->wai);
   85: #else
   86:         ercd = tk_wai_flg(lock->id, ptn, TWF_ORW|TWF_BITCLR, &flg, tmo);
   87: #endif
   88: 
   89:         return ercd;
   90: }
   91: EXPORT ER UMLockTmo_u( FastUMLock *lock, INT no, TMO_U tmo )
   92: {
   93:         UINT   ptn = (UINT)(1 << no);
   94:         UINT   flg;
   95:         ER     ercd;
   96: 
   97: #if USE_ATOMIC_INT
   98:         atomic_inc(&lock->wai);
   99:         for ( ;; ) {
  100:                 if ( !(atomic_bitset(&lock->flg, ptn) & ptn) ) {
  101:                         ercd = E_OK;
  102:                         break;
  103:                 }
  104: 
  105:                 ercd = tk_wai_flg_u(lock->id, ptn, TWF_ORW|TWF_BITCLR, &flg, tmo);
  106:                 if ( ercd < E_OK ) {
  107:                         break;
  108:                 }
  109:         }
  110:         atomic_dec(&lock->wai);
  111: #else
  112:         ercd = tk_wai_flg_u(lock->id, ptn, TWF_ORW|TWF_BITCLR, &flg, tmo);
  113: #endif
  114: 
  115:         return ercd;
  116: }
  117: 
  118: /*
  119:  * Lock
  120:  *      no   Lock number 0 - 31
  121:  */
  122: EXPORT ER UMLock( FastUMLock *lock, INT no )
  123: {
  124:         return UMLockTmo(lock, no, TMO_FEVR);
  125: }
  126: 
  127: /*
  128:  * Lock release
  129:  *      no   Lock number 0 - 31
  130:  */
  131: EXPORT ER UMUnlock( FastUMLock *lock, INT no )
  132: {
  133:         UINT   ptn = (UINT)(1 << no);
  134:         ER     ercd;
  135: 
  136: #if USE_ATOMIC_INT
  137:         atomic_bitclr(&lock->flg, ~ptn);
  138:         ercd = ( lock->wai == 0 )? E_OK: tk_set_flg(lock->id, ptn);
  139: #else
  140:         ercd = tk_set_flg(lock->id, ptn);
  141: #endif
  142: 
  143:         return ercd;
  144: }
  145: 
  146: /*
  147:  * Create multi-lock
  148:  */
  149: EXPORT ER CreateUMLock( FastUMLock *lock, CONST UB *name )
  150: {
  151:         T_CFLG cflg;
  152:         ER     ercd;
  153: 
  154:         if ( name == NULL ) {
  155:                 cflg.exinf = NULL;
  156:         } else {
  157:                 strncpy((char*)&cflg.exinf, (char*)name, sizeof(cflg.exinf));
  158:         }
  159:         cflg.flgatr  = TA_TPRI | TA_WMUL | TA_NODISWAI;
  160: #if USE_ATOMIC_INT
  161:         cflg.iflgptn = 0;
  162: #else
  163:         cflg.iflgptn = ~0;
  164: #endif
  165: 
  166:         lock->id = ercd = tk_cre_flg(&cflg);
  167:         if ( ercd < E_OK ) {
  168:                 return ercd;
  169:         }
  170: 
  171:         lock->wai = 0;
  172:         lock->flg = 0;
  173: 
  174:         return E_OK;
  175: }
  176: 
  177: /*
  178:  * Delete multi-lock
  179:  */
  180: EXPORT ER DeleteUMLock( FastUMLock *lock )
  181: {
  182:         ER     ercd;
  183: 
  184:         if ( lock->id <= 0 ) {
  185:                 return E_PAR;
  186:         }
  187: 
  188:         ercd = tk_del_flg(lock->id);
  189:         if ( ercd < E_OK ) {
  190:                 return ercd;
  191:         }
  192: 
  193:         lock->id = 0;
  194: 
  195:         return E_OK;
  196: }