what is all this atomicity stuff anyhow?
Submitted By: Erik Malund FAQ Last Modified: 04/22/07
- sorry, Bob Pease, I 'stole' your preferred headline.
Atomicity need be considered when a a multibyte value is used both in an ISR (interrupt Service Routine) and the main.
It should be noted that ignoring atomicity usually will lead to an intermittent problem.
Example (an external counter is connected to P1 (MSB) and P2 (LSB) an interrupt happens every time the counter increments):
Main:
C:
unsigned int value
.....
Ralph = value;
....
(the asm below is a 'typical' result of this C)
Asm:
mov a,HIGH value
POINT X
mov b,LOW value
ISR:
C:
....
value - (U16) (P1 << 8);
value |= (U16) P2;
....
Asm:
.....
HIGH value = P1
LOW value = P2
...
description:
If the value is at 01ffh and the interrupt happens at point x, main will read the high byte as 1 and will then read the low byte as 0; Thus the value that should have read 0200h is read as 0100h. Reversing the read of the low and high byte does not fix the problem, it just changes it.
NOTE: the assumption “C takes care of this" is false
The cure:
disable the specific interrupt or all interrupts before using the two byte pointer in main and re-enable immediately after processing the pointer.
"two byte pointer"...
Submitted By: Michael Karas FAQ Last Modified: 04/22/07
- The usage of the terms "two byte pointer" in the very last lines of the above FAQ entry is somewhat misleading. The sample C code shown uses an int type variable which is a "two byte value" or "two byte variable" either of which would be a much better term to use in describing the 'fix' for the interrupt routine interfering with the main code fetch of the 16-bit variable value.
The term usage of "two byte pointer" is additionally confusing in this context where C code is shown in the examples due to the fact that the C language has a 'pointer' data type. However that data type is _not_ being shown in the code samples.
- Michael Karas
Atomicity is not only about multibyte-variables ?
Submitted By: Jan Waclawek FAQ Last Modified: 04/22/07
- Each time a set of variables (including, but not only, multi-byte variables) is accessed both in "main" and in "interrupt" (or in various processes in multitasking environment), care must be taken that one of the processes does not see an inconsistent state.
A nice example of atomicity problem NOT dealing with multibyte variables was discussed in the following thread:
http://www.8052.com/forum/thr...ead=137677
An another interesting problem of the similar nature:
http://www.8052.com/forum/thr...ead=141811
Add Information to this FAQ: If you have additional information or alternative solutions to this question, you may add to this FAQ by clicking here.