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
task stacks with system.alloc

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



Joined: 23 Feb 2006
Posts: 657

Posted: 16 October 2006, 17:43 PM    Post subject: task stacks with system.alloc Reply with quote

I've successfully used (no memory leaks) system.alloc() to get memory for task stacks with the new 1.4 compiler/VM. I have a state machine where each state is code in a particular ZBasic sub. State transitions are done by CallTask() to startup the next state then quitting the current task (state). This scheme is what I've done in C for a long time, using function pointers though, for state machines. To me, it's easier to maintain and modify than a big switch statement. Each state (task) is a sub with a do loop that executes until the conditions arise for a state transition - wherein CallTask() changes states.

I think this general technique can be used for other than state machines, where you have tasks that are fleeting.

At the moment, I'm doing this to switch tasks:

...
callTask StateN(register.taskCurrent), system.alloc(stacksize) ' pass current stack pointer
end sub ' exit this task

Note that the stack pointer for the exiting task is passed to the new task. This permits the new task to release the stack memory for the old task:

sub StateN(byVal oldStack as unsignedInteger)
system.free(oldStack)
... compute things

end sub

----------
Of course, by design you don't system.free() stack that is a static storage item declared in a DIM. So You can have a common sub like

if (oldStack <> someStaticStack.dataAddress) then
system.free(oldStack)
end if

I've observed, but am not 100% sure, that the old task will have always exited before the system.free() is done in the new task, given the old task has no more code after the calltask(). But, I'm not sure about the case where there's a task switch just after the Calltask() causing the exit to be delayed.
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