tkernel_2/lib/libtk/src/fastlock.c | bare source | permlink (0.03 seconds) |
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: * @(#)fastlock.c (libtk) 18: * 19: * High-speed exclusive control 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: * Inc Increment cnt, in result if cnt > 0, returns positive value. 30: * If cnt <= 0, returns 0 or negative value. 31: * Dec Decrement cnt, in result if cnt >= 0, returns positive value. 32: * If cnt < 0, returns 0 or negative value. 33: * Increment/Decrement and evaluation of the associated result must 34: * be executed exclusively. 35: */ 36: 37: Inline INT Inc( FastLock *lock ) 38: { 39: UINT imask; 40: INT c; 41: DI(imask); 42: c = ++lock->cnt; 43: EI(imask); 44: return c; 45: } 46: Inline INT Dec( FastLock *lock ) 47: { 48: UINT imask; 49: INT c; 50: DI(imask); 51: c = lock->cnt--; 52: EI(imask); 53: return c; 54: } 55: 56: /* ------------------------------------------------------------------------ */ 57: 58: /* 59: * Lock 60: */ 61: EXPORT void Lock( FastLock *lock ) 62: { 63: if ( Inc(lock) > 0 ) { 64: tk_wai_sem(lock->id, 1, TMO_FEVR); 65: } 66: } 67: 68: /* 69: * Lock release 70: */ 71: EXPORT void Unlock( FastLock *lock ) 72: { 73: if ( Dec(lock) > 0 ) { 74: tk_sig_sem(lock->id, 1); 75: } 76: } 77: 78: /* 79: * Create high-speed lock 80: */ 81: EXPORT ER CreateLock( FastLock *lock, CONST UB *name ) 82: { 83: T_CSEM csem; 84: ER ercd; 85: 86: if ( name == NULL ) { 87: csem.exinf = NULL; 88: } else { 89: STRNCPY((char*)&csem.exinf, (char*)name, sizeof(csem.exinf)); 90: } 91: csem.sematr = TA_TPRI | TA_NODISWAI; 92: csem.isemcnt = 0; 93: csem.maxsem = 1; 94: 95: ercd = tk_cre_sem(&csem); 96: if ( ercd < E_OK ) { 97: return ercd; 98: } 99: 100: lock->id = ercd; 101: lock->cnt = -1; 102: 103: return E_OK; 104: } 105: 106: /* 107: * Delete high-speed lock 108: */ 109: EXPORT void DeleteLock( FastLock *lock ) 110: { 111: if ( lock->id > 0 ) { 112: tk_del_sem(lock->id); 113: } 114: lock->id = 0; 115: }