Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
atomic operations among tasks

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
stevech



Joined: 23 Feb 2006
Posts: 657

Posted: 07 November 2006, 5:08 AM    Post subject: atomic operations among tasks Reply with quote

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 Reply with quote

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: Reply with quote

thanks. a terse VM crib sheet someday would be handy
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group