gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/console/src/consmlock.cbare sourcepermlink (0.02 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: /*
   18:  *      consmlock.c  Console/Low-level serial I/O driver
   19:  *
   20:  *      Multilocking for high-speed exclusive control : system-independent
   21:  *      * Modify "fastmlock.c (libtk)" for "console/serial" driver use
   22:  */
   23: 
   24: #include <basic.h>
   25: #include <tk/tkernel.h>
   26: #include <tk/util.h>
   27: #include <libstr.h>
   28: 
   29: /* ------------------------------------------------------------------------ */
   30: /*
   31:  *      System-dependent code
   32:  *      void INC( INT *val )         Increment
   33:  *      void DEC( INT *val )         Decrement
   34:  *      BOOL BTS( UINT *val, INT no )        Bit test & Set
   35:  *      void BR( UINT *val, INT no ) Bit reset
   36:  *
   37:  *      These require the exclusive behavior.
   38:  */
   39: 
   40: Inline  void     INC( INT *val )
   41: {
   42:         UINT   imask;
   43: 
   44:         DI(imask);
   45:         (*val)++;
   46:         EI(imask);
   47: }
   48: 
   49: Inline  void     DEC( INT *val )
   50: {
   51:         UINT   imask;
   52: 
   53:         DI(imask);
   54:         (*val)--;
   55:         EI(imask);
   56: }
   57: 
   58: Inline  BOOL     BTS( UINT *val, INT no )
   59: {
   60:         UINT   imask;
   61:         UINT   b;
   62:         UINT   bm = 1 << no;
   63: 
   64:         DI(imask);
   65:         b = *val & bm;
   66:         *val |= bm;
   67:         EI(imask);
   68:         return b;
   69: }
   70: 
   71: Inline  void     BR( UINT *val, INT no )
   72: {
   73:         UINT   imask;
   74: 
   75:         DI(imask);
   76:         *val &= ~(1 << no);
   77:         EI(imask);
   78: }
   79: 
   80: /* ------------------------------------------------------------------------ */
   81: 
   82: /*
   83:  *      Lock with specification of waiting time
   84:  *      no   Lock number 0-31
   85:  */
   86: LOCAL   ER        consMLockTmo( FastMLock *lock, INT no, TMO tmo )
   87: {
   88:         UINT   ptn = 1 << no;
   89:         UINT   flg;
   90:         ER     err;
   91: 
   92:         INC(&lock->wai);
   93:         for ( ;; ) {
   94:                 if ( !BTS(&lock->flg, no) ) {
   95:                         err = E_OK;
   96:                         break;
   97:                 }
   98:                 err = tk_wai_flg(lock->id, ptn, TWF_ORW|TWF_BITCLR, &flg, tmo);
   99:                 if ( err < E_OK ) break;
  100:         }
  101:         DEC(&lock->wai);
  102: 
  103:         return err;
  104: }
  105: 
  106: /*
  107:  *      Lock
  108:  *      no   Lock number 0-31
  109:  */
  110: EXPORT  ER       consMLock( FastMLock *lock, INT no )
  111: {
  112:         return consMLockTmo(lock, no, TMO_FEVR);
  113: }
  114: 
  115: /*
  116:  *      Release the lock
  117:  *      no   Lock number 0-31
  118:  */
  119: EXPORT  ER       consMUnlock( FastMLock *lock, INT no )
  120: {
  121:         UINT   ptn = 1 << no;
  122:         ER     err;
  123: 
  124:         BR(&lock->flg, no);
  125:         err = ( lock->wai == 0 )? E_OK: tk_set_flg(lock->id, ptn);
  126: 
  127:         return err;
  128: }
  129: 
  130: /*
  131:  *      Create the multilocking
  132:  */
  133: EXPORT  ER       consCreateMLock( FastMLock *lock, UB *name )
  134: {
  135:         T_CFLG cflg;
  136:         ER     err;
  137: 
  138:         if ( name == NULL ) {
  139:                 cflg.exinf = NULL;
  140:         } else {
  141:                 STRNCPY((B*)&cflg.exinf, name, sizeof(cflg.exinf));
  142:         }
  143:         cflg.flgatr  = TA_TFIFO | TA_WMUL;
  144:         cflg.iflgptn = 0;
  145: 
  146:         lock->id = err = tk_cre_flg(&cflg);
  147:         if ( err < E_OK ) return err;
  148: 
  149:         lock->wai = 0;
  150:         lock->flg = 0;
  151: 
  152:         return lock->id;
  153: }
  154: 
  155: /*
  156:  *      Delete the multilocking
  157:  */
  158: EXPORT  ER       consDeleteMLock( FastMLock *lock )
  159: {
  160:         ER     err;
  161: 
  162:         if ( lock->id <= 0 ) return E_PAR;
  163: 
  164:         err = tk_del_flg(lock->id);
  165:         if ( err < E_OK ) return err;
  166: 
  167:         lock->id = 0;
  168: 
  169:         return E_OK;
  170: }