pico]OS  1.0.4
Macros
Disable / Enable Interrupts
Configuration: Pico Layer

Macros

#define POSCFG_LOCK_USEFLAGS   0
#define POSCFG_LOCK_FLAGSTYPE   register VAR_t
#define POS_SCHED_LOCK   asm { PUSHF; CLI }
#define POS_SCHED_UNLOCK   asm POPF
#define POS_IRQ_DISABLE_ALL   POS_SCHED_LOCK
#define POS_IRQ_ENABLE_ALL   POS_SCHED_UNLOCK

Detailed Description

The operating system must be able to disable the interrupts on the processor for a short time to get exclusive access to internal data structures. There are three possible ways to solve this:

1) Most processors have assembler commands that directly allow disabling and enabling interrupts. When the operating system needs to get exclusive access to any data, it will disable interrupts, access the data and enable interrupts again. The disadvantage of this simple way is that if the processor had disabled interrupts before the OS entered the critical section, the OS will reenable the interrupts again after it left the critical section regardless if interrupts were disabled before.

2) A better way is to save the current processor state before disabling interrupts. Much processors support a "push flags to stack" OP-Code for this purpose. When the operating system enters a critical section, it will push the processor flags to the stack and disables the interrupts. Then, when the operating system will left the critical section again, it simply restores the old processor state by popping the last processor state from the stack. If interrupts where enabled before, it just became enabled now.

3) There are some processors which have no OP-code for directly pushing the processor flags (=PSW, Processor Status Word) directly to the stack. For this processors, you can define a local variable which will hold the original PSW when the operating system enters the critical section. If your processor has enough general purpose register, you may define the variable as register variable for fastest possible access. This is truly better than pushing the flags to the stack.

Hint:
Sometimes it is not possible to disable interrupts globally. Then you will set POS_SCHED_LOCK to disable only the timer interrupt. If you wish to use pico]OS software interrupts, you will then also need to configure POS_IRQ_DISABLE_ALL to disable interrupts globally.


Macro Definition Documentation

#define POS_IRQ_DISABLE_ALL   POS_SCHED_LOCK

Optional scheduler locking: Disable all interrupts. In some environments it is not possible to disable all interrupts for a longer period of time. In such an environment you will need to implement POS_SCHED_LOCK to only disable the timer interrupt and the interrupts that are directly connected to pico]OS. All other interrupts can be interfaced with the pico]OS software interrupt system to the pico]OS core. Only the interface functions need then to disable interrupts globally. Set this macro to a function that globally disables interrupts, including the interrupts that are also disabled by the usual POS_SCHED_LOCK macro.

Note:
If your system timing is not critical, you can set this macro to POS_SCHED_LOCK
See also:
POS_IRQ_ENABLE_ALL, POS_SCHED_LOCK, POSCFG_LOCK_USEFLAGS
#define POS_IRQ_ENABLE_ALL   POS_SCHED_UNLOCK

Optional scheduler locking: Enable all interrupts. This is the counterpart macro of POS_IRQ_DISABLE_ALL. It enables all interrupts again.

#define POS_SCHED_LOCK   asm { PUSHF; CLI }

Scheduler locking. Locking the scheduler for a short time is done by disabling the interrupts on the processor. This macro can contain a subroutine call or a piece of assembler code that stores the processor state and disables the interrupts. See POSCFG_LOCK_FLAGSTYPE for more details.

#define POS_SCHED_UNLOCK   asm POPF

Scheduler unlocking. This is the counterpart macro of POS_SCHED_LOCK. It restores the saved processor flags and reenables the interrupts this way.

#define POSCFG_LOCK_FLAGSTYPE   register VAR_t

Define variable type for the processor flags. If POSCFG_LOCK_USEFLAGS is set to 1, this define must be set to the variable type that shall be used for the processor flags. In this example, the variable definition "register VAR_t flags;" would be added to each function using the macros POS_SCHED_LOCK, POS_SCHED_UNLOCK, POS_IRQ_DISABLE_ALL and POS_IRQ_ENABLE_ALL.

#define POSCFG_LOCK_USEFLAGS   0

Enable local flags variable. When this define is set to 1, a user defined variable will be generated for storing the current processor state before disabling interrupts. Then the define POSCFG_LOCK_FLAGSTYPE must be set to the type of variable to be used for the flags.