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
run-time Task Control Block allocation

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



Joined: 23 Feb 2006
Posts: 657

Posted: 07 March 2006, 7:09 AM    Post subject: run-time Task Control Block allocation Reply with quote

For an application that needs to create multiple instances of the same code for a task or other run-time decisions, and the number of tasks isn't known at compile-time, how would this be done in ZBasic? There is nothing similar to malloc() and free() that I've found, i.e., malloc() a TCB for the task's use.

I tried but didn't see a way to coerce makestring() into malloc, for a byte array, by calling a string function that uses makestring().

Maybe there's some way to have a sub DIM b() as Byte in local storage and pass that as the TCB, and have that sub delay returning until the task exits. Seems awkward - someone may know a better way in ZBasic!
Back to top
mikep



Joined: 24 Sep 2005
Posts: 765
Location: Austin, TX

Posted: 07 March 2006, 7:36 AM    Post subject: Reply with quote

My first response is what are you trying to do exactly? Tasks consume resources and need to be managed (see application notes AN-209 and AN-210) and so should not be take lightly.

To answer your question, the memory address for the task stack is determined at compile time and not runtime. Part of the reason for this is so that all of the global memory is known about prior to runtime and the remaining memory is allocated to the "main" task. Therefore you cannot do what you are asking.

It is actually a good thing that ZBasic handles most of the memory management for you. Malloc and Free are the #1 causes of bugs in C code. The only dynamically allocated resource (except for the main stack) are strings which are stored in a different part of the memory map - the heap if you like.
Back to top
stevech



Joined: 23 Feb 2006
Posts: 657

Posted: 07 March 2006, 18:23 PM    Post subject: Reply with quote

As to the goal:

I'm merely comparing the multitasking capabilities to other operating systems I've used in the past to solve various needs. In many or even most, you don't know at design-time how many instances of what tasks are needed because tasks come and go as real time I/O events happen. And there may be 2 or more active instances of the same task-code on occasion.

Just a general question.
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 07 March 2006, 20:18 PM    Post subject: Reply with quote

Quote:
I'm merely comparing the multitasking capabilities to other operating systems I've used in the past...

To call the ZBasic VM an operating system is to be overly generous, I think. It does have some rudimentary capabilities that would be expected in an OS but it is quite primitive.

On top of that, the resources available to be managed are quite meager an it is, therefore, probably better to leave some critical decisions to the programmer who has a better view of the tradeoffs that they are willing to make.

One possible solution is to create an array of task stacks and then use them as necessary. This doesn't solve the allocation problem - you still have to decide the maximum number of tasks. The disadvantage of this approach is that different tasks have different stack size requirements so you'll be "wasting" RAM space in order to have a cleaner method of selecting the task stack to use.

In any event, you'll probably find that you're limited (by available RAM) to 4 or 5 significant tasks, more or less, depending on their complexity.
Back to top
spamiam



Joined: 13 Nov 2005
Posts: 664

Posted: 07 March 2006, 20:36 PM    Post subject: Re: run-time Task Control Block allocation Reply with quote

stevech wrote:
For an application that needs to create multiple instances of the same code for a task....



I am not sure about exactly what you are looking for, but....

We did have a discussion of a somewhat similar issue in the past (I think it was during development, before full release).

I believe it was Mike Perks who came up with this question.

Can you have multiple instances of the same task each with its own instruction pointer, stack, and stuff.

I never tried it, but I think that the answer was a YES, it can be done.

All that was necessary was a new task invocation using the same code, but its own separate task stack.

Is this what your question was driving at? In this scenario, a task can be invoked when the situation is right and then it can be released when the need is over. There is no specific limit to the number of tasks, but you will quickly run out of memory for more task stacks.....

-Tony
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 07 March 2006, 21:15 PM    Post subject: Reply with quote

Yes, it is true that you can have multiple, concurrent invocations of a specific task but each must have its own task stack.

What 'stevech' was hoping for was to have the task stacks automatically allocated for each task invocation, obviating the need to declare them in advance. This would be nice, of course, but I think it is impractical with the resource limitations of the mega32.
Back to top
stevech



Joined: 23 Feb 2006
Posts: 657

Posted: 08 March 2006, 0:39 AM    Post subject: Reply with quote

No need for automatic TCB creation - just a means to get an array of bytes at run-time and use that as the TCB. Many of these tasks need only 50 or so bytes for each instance - which isn't always known at design-tme.

I tried
dim s1 as string, s2 as string
dim t() as bytearray ' does this work?

s1=""
s2=makestring(s1, 50) ' gimme some heap

now if I can "cast" that to an array of bytes I have a TCB, eh?

t = cbytearray(s2) ' ????

calltask ("IamPolyMorphic", memaddress(t))
or
calltask ("IamPolyMorphic", straddress(s2)+4) ' ???

and again as needed.

Or I am totally full of it.
-------------

By the way: Why does calltask use the string name of the sub rather than it's symbol name, since this is compile-time code generation?
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 08 March 2006, 1:27 AM    Post subject: Reply with quote

Quote:
now if I can "cast" that to an array of bytes I have a TCB

This won't work currently (though it or something similar might be made to work in the future). A minor issue is that a string variable occupies 4 bytes, two of which are the address of the allocated string store that contains the string's characters. You can retrieve this address using the StrAddress() function. (Note that StrAddress() returns the address of the first character of any string, not just allocated strings. Moreover, the returned address is not necessarily a RAM address. For example, if you've just assigned a constant string to a string variable, the returned address will probably be a Program Memory address. The StrType() function returns a value that indicates the address space that the string's characters are in. See the documentation for more complete information.)

The other issue is that the compiler currently requires that the task stack be a module level variable. This is done so that the lifetime of the task stack can be longer than the routine that invokes CallTask(). This is something that could be changed, but that's the way it is now.

Regarding the CallTask syntax, your point is well taken. The current syntax was implemented to be compatible with BasicX. The compiler could be modified to alternately accept a non-quoted subroutine name in native mode.
Back to top
stevech



Joined: 23 Feb 2006
Posts: 657

Posted: 08 March 2006, 3:24 AM    Post subject: Reply with quote

I have a large VB6 application that runs 24/7 to present a variety of home theater and web surfing options in menus on the family TV. There's a date/time based scheduler in VB6 inside that App - that I wrote. It uses VB6's CallByName capability to implement the scheduler. When I first saw the ZBasic call task with a string argument, I thought about that! The task name is computed at run-time and put in a string.

So both task-by-name and task-by-compiler-symbol are useful.

(given the run time creation of a TCB for some situations)
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 08 March 2006, 11:31 AM    Post subject: Reply with quote

Quote:
The task name is computed at run-time...

The task address is resolved at compile-time as shown in the following excerpt from a simple program.
    CallTask "task1", ts1
    0014 1b2000 PSHI_W 0x0020 (32)
    0017 1ba000 PSHI_W 0x00a0 (160)
    001a 1b2800 PSHI_W 0x0028 (40)
    001d fe4a SCALL TASK_START

The first parameter to TASK_START is the address of the task, the second is the address of task's stack and the third is the size of the stack.

If you examine a listing file, you'll find that Main() is started in exactly the same way except that the third parameter is zero signifying that the size of all remaining remaining RAM is to be used as the stack size.
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