t2ex/t2ex_source/include/sys/sysdepend/atomic_common.h | 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: * @(#)atomic_common.h (sys) 48: * 49: * Atomic integer operations 50: */ 51: 52: #ifndef __SYS_ATOMIC_COMMON_H__ 53: #define __SYS_ATOMIC_COMMON_H__ 54: 55: #include <basic.h> 56: #include <tk/syslib.h> 57: 58: #if TEF_EM1D 59: # include <sys/sysdepend/tef_em1d/atomic_depend.h> 60: #endif 61: 62: /* 63: * From here, generic implementations of atomic integer operations 64: * are defined. Each of these definitions is used only when 65: * ATOMIC_xxx_USER_MODE remains undefined at this point. 66: * 67: * The value of ATOMIC_xxx_USER_MODE shall be defined as follows: 68: * 0: atomic_xxx implementation cannot be used in user mode 69: * 1: atomic_xxx implementation can be used in user mode 70: */ 71: 72: #ifndef ATOMIC_INC_USER_MODE 73: #define ATOMIC_INC_USER_MODE 0 74: Inline UINT atomic_inc(volatile UINT* addr) 75: { 76: UINT imask; 77: UINT re; 78: DI(imask); 79: re = ++*addr; 80: EI(imask); 81: return re; 82: } 83: #endif /* ATOMIC_INC_USER_MODE */ 84: 85: #ifndef ATOMIC_DEC_USER_MODE 86: #define ATOMIC_DEC_USER_MODE 0 87: Inline UINT atomic_dec(volatile UINT* addr) 88: { 89: UINT imask; 90: UINT re; 91: DI(imask); 92: re = --*addr; 93: EI(imask); 94: return re; 95: } 96: #endif /* ATOMIC_DEC_USER_MODE */ 97: 98: #ifndef ATOMIC_ADD_USER_MODE 99: #define ATOMIC_ADD_USER_MODE 0 100: Inline UINT atomic_add(volatile UINT* addr, UINT val) 101: { 102: UINT imask; 103: UINT re; 104: DI(imask); 105: *addr += val; 106: re = *addr; 107: EI(imask); 108: return re; 109: } 110: #endif /* ATOMIC_ADD_USER_MODE */ 111: 112: #ifndef ATOMIC_SUB_USER_MODE 113: #define ATOMIC_SUB_USER_MODE 0 114: Inline UINT atomic_sub(volatile UINT* addr, UINT val) 115: { 116: UINT imask; 117: UINT re; 118: DI(imask); 119: *addr -= val; 120: re = *addr; 121: EI(imask); 122: return re; 123: } 124: #endif /* ATOMIC_SUB_USER_MODE */ 125: 126: /* TAS: test-and-set */ 127: #ifndef ATOMIC_XCHG_USER_MODE 128: #define ATOMIC_XCHG_USER_MODE 0 129: Inline UINT atomic_xchg(volatile UINT* addr, UINT val) 130: { 131: UINT imask; 132: UINT re; 133: DI(imask); 134: re = *addr; 135: *addr = val; 136: EI(imask); 137: return re; 138: } 139: #endif /* ATOMIC_XCHG_USER_MODE */ 140: 141: /* CAS: compare-and-swap */ 142: #ifndef ATOMIC_CMPXCHG_USER_MODE 143: #define ATOMIC_CMPXCHG_USER_MODE 0 144: Inline UINT atomic_cmpxchg(volatile UINT* addr, UINT val, UINT cmp) 145: { 146: UINT imask; 147: UINT re; 148: DI(imask); 149: re = *addr; 150: if ( *addr == cmp ) { 151: *addr = val; 152: } 153: EI(imask); 154: return re; 155: } 156: #endif /* ATOMIC_CMPXCHG_USER_MODE */ 157: 158: #ifndef ATOMIC_BITSET_USER_MODE 159: #define ATOMIC_BITSET_USER_MODE 0 160: Inline UINT atomic_bitset(volatile UINT* addr, UINT setptn) 161: { 162: UINT imask; 163: UINT re; 164: DI(imask); 165: re = *addr; 166: *addr |= setptn; 167: EI(imask); 168: return re; 169: } 170: #endif /* ATOMIC_BITSET_USER_MODE */ 171: 172: #ifndef ATOMIC_BITCLR_USER_MODE 173: #define ATOMIC_BITCLR_USER_MODE 0 174: Inline UINT atomic_bitclr(volatile UINT* addr, UINT clrptn) 175: { 176: UINT imask; 177: UINT re; 178: DI(imask); 179: re = *addr; 180: *addr &= clrptn; 181: EI(imask); 182: return re; 183: } 184: #endif /* ATOMIC_BITCLR_USER_MODE */ 185: 186: #endif /* __SYS_ATOMIC_COMMON_H__ */