./. schrieb:
> Peter II schrieb:
>> Vorsicht: das ist aber langsamer und nicht atomar.
>
> Ach, und das Original des TO ist atomar?!
Ja, ist es.
Lass zum Beispiel
1 | #include <avr/io.h>
|
2 |
|
3 | void foo (void)
|
4 | {
|
5 | while (1)
|
6 | {
|
7 | if (!(PORTB & (1 << PORTB0)))
|
8 | PORTB |= (1 << PORTB0);
|
9 | else
|
10 | PORTB &= ~(1 << PORTB0);
|
11 | }
|
12 | }
|
durch avr-gcc -O und du bekommst
1 | foo:
|
2 | .L5:
|
3 | sbic 0x18,0
|
4 | rjmp .L3
|
5 | sbi 0x18,0
|
6 | rjmp .L5
|
7 | .L3:
|
8 | cbi 0x18,0
|
9 | rjmp .L5
|
Es ist nun kein Problem mehr, wemm asynchron zum Beispiel PB.1 verändert
wird.
Kommt es auf die Reihenfolge von Änderungen an, etwa daß PB.1 nur nach
einer bestimmten Aktion an PB.0 verändert werden darf, dann ist das ein
Synchronisierungsproblem aber keines der Atomarität.
Code wie
wie ihn neuere AVRs erlauben ist natürlich das beste, aber ein Compiler
darf und wird nicht eine Sequenz wie
darin umwandeln.