2pe Multi-Tasking : One possible approach
TinkersALot
Posts: 535
Folks,
This project was over in the Basic Stamps Forum in the "Paged Programming" thread. I am moving it over here because I have posted a number of updates to it and have noticed that all versions are being downloaded (maybe I'm using the attach feature incorrectly?). This program was developed because I became intrigued by the potential of different program slots on 2pe (and related) devices and decided to experiment with the development of a simple multi-tasking system.
So, I thought I would re-post the (expected to be) final version of this 2pe framework into this forum in the hope that someone can benefit from this design. This sample/shell provides a slot/task framework that can be used as the basis for a cooperative multi-tasking system for use in 2pe (or related) applications. The main features of this framework are listed below:
1) Illustrates multi-slot programming techniques
2) Slot 0 task implements a simple executive task that can systematically run "tasks" that are loaded into slots 1 through 6. The executive task can use a simple round-robin task selection strategy. Alternatively, the executive task supports up to 4 data arrays of task lists so that the application designer can define a task prioritization system that fits the application requirements. The executive (if running in priority list mode) will iterate the task list arrays in order to determine which task (slot) to execute next. The more times that a task/slot appears in these arrays, the "higher priority" that task will have.
3) The executive task illustrates techniques that are useful for "task initialization" with specific attention given to "initialization code" that should only ever execute once (this is important because·the task/slot programs are "re-entrant") irregardless of the number of times the task/slot program is executed.
4) The example tasks illustrate a "load context, execute, then yield" design pattern that would allow tasks to cooperatively multi-task on the 2pe device. The load context paradigm is another useful technique for re-entrant programs and allows the tasks to reconstruct their "operational state" as part of being entered again by the executive. The yield routine would likely be unique to each task·to allow it so persist its state prior to returning control to the·executive.
5) This system offers a first pass approach at an inter-task messaging system. This would allow tasks to "send messages to" other tasks. To fully realize the potential of this design, tasks would execute logic based on the messages they have received. The message queue is in the shadow-ram of the device and each slot/task program offers a "put/get" set of functions for reading-from and writing-to the intertask message queue. The message queue implementation in this sample implements a 2 byte structure. The first byte stores the message target/source as a pair of nibbles. The 2nd byte in the message structure contains the "message payload" sent by the originator of the message. This byte could be used as a steering variable by the receiving task.
6) The tasks included in this sample yield whenever they do not have any messages pending for them. This is one possible way to make use of inter-task messages wherein tasks with "nothing to do" (as indicated by a lack of incoming messages) would immediately yield so that tasks with pending work can be given control over the device.
This was a fun project to explore and develop, and I hope someone here will find some useful ideas/approaches from this shell.
This project was over in the Basic Stamps Forum in the "Paged Programming" thread. I am moving it over here because I have posted a number of updates to it and have noticed that all versions are being downloaded (maybe I'm using the attach feature incorrectly?). This program was developed because I became intrigued by the potential of different program slots on 2pe (and related) devices and decided to experiment with the development of a simple multi-tasking system.
So, I thought I would re-post the (expected to be) final version of this 2pe framework into this forum in the hope that someone can benefit from this design. This sample/shell provides a slot/task framework that can be used as the basis for a cooperative multi-tasking system for use in 2pe (or related) applications. The main features of this framework are listed below:
1) Illustrates multi-slot programming techniques
2) Slot 0 task implements a simple executive task that can systematically run "tasks" that are loaded into slots 1 through 6. The executive task can use a simple round-robin task selection strategy. Alternatively, the executive task supports up to 4 data arrays of task lists so that the application designer can define a task prioritization system that fits the application requirements. The executive (if running in priority list mode) will iterate the task list arrays in order to determine which task (slot) to execute next. The more times that a task/slot appears in these arrays, the "higher priority" that task will have.
3) The executive task illustrates techniques that are useful for "task initialization" with specific attention given to "initialization code" that should only ever execute once (this is important because·the task/slot programs are "re-entrant") irregardless of the number of times the task/slot program is executed.
4) The example tasks illustrate a "load context, execute, then yield" design pattern that would allow tasks to cooperatively multi-task on the 2pe device. The load context paradigm is another useful technique for re-entrant programs and allows the tasks to reconstruct their "operational state" as part of being entered again by the executive. The yield routine would likely be unique to each task·to allow it so persist its state prior to returning control to the·executive.
5) This system offers a first pass approach at an inter-task messaging system. This would allow tasks to "send messages to" other tasks. To fully realize the potential of this design, tasks would execute logic based on the messages they have received. The message queue is in the shadow-ram of the device and each slot/task program offers a "put/get" set of functions for reading-from and writing-to the intertask message queue. The message queue implementation in this sample implements a 2 byte structure. The first byte stores the message target/source as a pair of nibbles. The 2nd byte in the message structure contains the "message payload" sent by the originator of the message. This byte could be used as a steering variable by the receiving task.
6) The tasks included in this sample yield whenever they do not have any messages pending for them. This is one possible way to make use of inter-task messages wherein tasks with "nothing to do" (as indicated by a lack of incoming messages) would immediately yield so that tasks with pending work can be given control over the device.
This was a fun project to explore and develop, and I hope someone here will find some useful ideas/approaches from this shell.
Comments
Since you are moving the thread and post, put a message stating your intentions with a link to this thread, otherwise two simultaneous threads will parallel and information will be likely be missed. ORIGINAL THREAD POST HERE
http://forums.parallax.com/showthread.php?p=806979
Post Edited (humanoido) : 5/11/2009 2:26:41 PM GMT
Where do you think the sample is too heavy?
Places to trim could be:
1) Remove Priority Based Task Selection from the executive. That is the bulk of the code in the executive program.
2) Remove/Alter the message passing routines.
After that you are left with the core of the original idea, a round-robin scheduler with completely independent tasks.
Does that "take too much out" for the project to remain useful to you? Just trying to define what you may want functionally in a "lite version"
PS And thanks for the guidance on how to properly link to the previous thread. Since I am new to this board, I appreciate this kind of help from other long-time-posters here.