|
|
| Author |
Message |
stevech
Joined: 23 Feb 2006
Posts: 657
|
|
Posted: 07 November 2006, 5:08 AM Post subject: atomic operations among tasks |
|
|
Just to make sure I understand how the task switching works:
In the code, below, there's no need for lockTask() in taskA() to make the access to n(0) atomic at the byte level. That is, an RTC interrupt may cause a task switch in taskA after n(0) + 1 is calculated but before the result is stored back into n(0). So long as taskB does not also alter n(0), there is no need for lockTask(). There would be such a need, if taskA and taskB both altered the n(0).
That n() is a multibyte variable is irrelevant, I assume, as the VM assures that the access to a variable is atomic at the byte level. (In C and assembler programming of 8 bit micros, one has to write one's own code to assure atomic accesses to multi-byte variables, but not in ZBasic's VM).
Correct?
| Code: |
DIM n(2) as unsignedInteger
sub taskA()
do
waitForInterrupt(something)
n(0) = n(0) + 1
loop
end sub
sub taskB()
dim x as unsignedInteger
do
x = n(0)
...
loop
end sub
|
|
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR
|
|
Posted: 07 November 2006, 6:28 AM Post subject: Re: atomic operations among tasks |
|
|
| stevech wrote: | | In the code, below, there's no need for lockTask() in taskA() to make the access to n(0) atomic at the byte level. |
Correct. In the particular case that you've shown, the incrementing operation is performed by a single VM instruction and all VM instructions are atomic. A task switch can only occur at VM instruction boundaries.
If the index expression of the array were not constant, more VM instructions would be generated with the possibility of a task switch between any pair. Even so, the reading/writing of an intrinsic data type like a Byte, Integer, etc. is atomic.
Here again, it may be instructive to inspect the listing file that can be generated by the compiler, keeping in mind that all resulting VM instructions are atomic but that lines of ZBasic code are not necessarily atomic since they may be implemented by 1 or many VM instructions. |
|
| Back to top |
|
 |
stevech
Joined: 23 Feb 2006
Posts: 657
|
|
Posted: 07 November 2006, 6:42 AM Post subject: |
|
|
| thanks. a terse VM crib sheet someday would be handy |
|
| Back to top |
|
 |
|