atomic_util.h (1615B)
1 /* Copyright 2021 QMK 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 #pragma once 17 18 #include <ch.h> 19 20 static __inline__ uint8_t __interrupt_disable__(void) { 21 chSysLock(); 22 23 return 1; 24 } 25 26 static __inline__ void __interrupt_enable__(const uint8_t *__s) { 27 chSysUnlock(); 28 29 __asm__ volatile("" ::: "memory"); 30 (void)__s; 31 } 32 33 static __inline__ syssts_t __interrupt_lock__(void) { 34 return chSysGetStatusAndLockX(); 35 } 36 37 static __inline__ void __interrupt_unlock__(const syssts_t *__s) { 38 chSysRestoreStatusX(*__s); 39 40 __asm__ volatile("" ::: "memory"); 41 } 42 43 #define ATOMIC_BLOCK(type) for (type, __ToDo = 1; __ToDo; __ToDo = 0) 44 #define ATOMIC_FORCEON uint8_t status_save __attribute__((__cleanup__(__interrupt_enable__))) = __interrupt_disable__() 45 #define ATOMIC_RESTORESTATE syssts_t status_save __attribute__((__cleanup__(__interrupt_unlock__))) = __interrupt_lock__() 46 47 #define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE) 48 #define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)