t2ex/t2ex_source/lib/libtk/src_t2ex/fastulock.c | bare source | permlink (0.02 seconds) |
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: * @(#)fastulock.c (libtk) 48: * 49: * User-mode high-speed exclusive control lock 50: */ 51: 52: #include <basic.h> 53: #include <tk/tkernel.h> 54: #include <t2ex/util.h> 55: #include <sys/atomic.h> 56: #include <libstr.h> 57: 58: #define USE_ATOMIC_INT ( ATOMIC_INC_USER_MODE && ATOMIC_DEC_USER_MODE ) 59: 60: /* ------------------------------------------------------------------------ */ 61: 62: /* 63: * Lock 64: */ 65: EXPORT void ULock( FastULock *lock ) 66: { 67: #if USE_ATOMIC_INT 68: if ( (INT)atomic_inc((INT*)&lock->cnt) > 0 ) { 69: tk_wai_sem(lock->id, 1, TMO_FEVR); 70: } 71: #else 72: tk_wai_sem(lock->id, 1, TMO_FEVR); 73: #endif 74: } 75: 76: /* 77: * Lock release 78: */ 79: EXPORT void UUnlock( FastULock *lock ) 80: { 81: #if USE_ATOMIC_INT 82: if ( (INT)atomic_dec((INT*)&lock->cnt) >= 0 ) { 83: tk_sig_sem(lock->id, 1); 84: } 85: #else 86: tk_sig_sem(lock->id, 1); 87: #endif 88: } 89: 90: /* 91: * Create high-speed lock 92: */ 93: EXPORT ER CreateULock( FastULock *lock, CONST UB *name ) 94: { 95: T_CSEM csem; 96: ER ercd; 97: 98: if ( name == NULL ) { 99: csem.exinf = NULL; 100: } else { 101: strncpy((char*)&csem.exinf, (char*)name, sizeof(csem.exinf)); 102: } 103: csem.sematr = TA_TPRI | TA_NODISWAI; 104: #if USE_ATOMIC_INT 105: csem.isemcnt = 0; 106: #else 107: csem.isemcnt = 1; 108: #endif 109: csem.maxsem = 1; 110: 111: ercd = tk_cre_sem(&csem); 112: if ( ercd < E_OK ) { 113: return ercd; 114: } 115: 116: lock->id = ercd; 117: lock->cnt = (UINT)-1; 118: 119: return E_OK; 120: } 121: 122: /* 123: * Delete high-speed lock 124: */ 125: EXPORT void DeleteULock( FastULock *lock ) 126: { 127: if ( lock->id > 0 ) { 128: tk_del_sem(lock->id); 129: } 130: lock->id = 0; 131: }