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