Author Topic: Program structure for cooperative scheduling  (Read 2191 times)

0 Members and 1 Guest are viewing this topic.

Offline BlogRahulTopic starter

  • Regular Contributor
  • *
  • Posts: 75
  • Country: in
Program structure for cooperative scheduling
« on: January 12, 2022, 02:11:46 pm »
Hi all

I am trying to write generic c code for cooperative scheduling just for learning.

Here is my basic idea   

 
Code: [Select]
enum Task_STATE {READY_To_RUN, RUNNING, WAITING};
 
enum Task_STATE STATE_FLAG;
 
 
int main(void)
{
 
   
    STATE_FLAG = READY_To_RUN;
   
    while(1)
    {
 
     if (Timer_Flag == 1)
        {   
            // Timer_Flag set every 1 ms
            Timer_Flag = 0;
{
       
              switch (STATE_FLAG)
               {
   case READY_To_RUN :
                   // Pick the taks that is ready to run
                   STATE_FLAG =  RUNNING;
                   break;
 
                   case  RUNNING :
                   // Run the task
                   STATE_FLAG = WAITING;
                   break;
 
                   case WAITING :
                   // waiting 
                   STATE_FLAG = READY_To_RUN;
                   break;
 
                  default:
                  // Unknown State Encountered, Reset System to READY_To_RUN,
                  STATE_FLAG = READY_To_RUN;
   }
}
 
        }
    }
 
    return (0);
}

// interrupt every 1 ms
void ISR_Timer_Interrupt ()
{
    if ( T1IF = SET )
    {
      T1IF = 0;
      Timer_Flag = 1;
    }
}

I need to create eight tasks and choose one of the task that is ready to run in state 1 . How to do this in code?
   
 

Offline brendan0powers

  • Newbie
  • Posts: 9
  • Country: us
    • My Blog
Re: Program structure for cooperative scheduling
« Reply #1 on: January 12, 2022, 02:38:45 pm »
I'm certainly no expert here, but I think you need some kind of abstraction for a task. The abstraction needs to include what task to run, and wether the task is ready to execute. You might do something like:

Code: [Select]
typedef struct {
  bool isReady;
  void (*taskFunc)();
} Task;

This struct holds a pointer to a task function, and a boolean flag indicating wether it should be run. You'd need to store a list of these, and iterate over the list every time you're in the running state. Once you have that working, you can start to add features. Time to next ready, priority, some kind of yield mechanism that makes it easier to wait for an interrupt, etc...
 
The following users thanked this post: BlogRahul

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14487
  • Country: fr
Re: Program structure for cooperative scheduling
« Reply #2 on: January 12, 2022, 05:44:49 pm »
Yep, you do need to structure your code if you want this to be maintainable, otherwise it will just look like spaghetti.

I just happen to have run into this yesterday: https://github.com/cocoOS/cocoOS , from which you could probably find some inspiration.
 
The following users thanked this post: BlogRahul

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3036
  • Country: us
Re: Program structure for cooperative scheduling
« Reply #3 on: January 12, 2022, 06:53:12 pm »
A very simplistic solution is to have your main loop look like this:

Code: [Select]
void loop() {
    while (1) {
        task1();
        task2();
        ...
        task8();
    }
}

and each task subroutine looks like the one you have in your original post -- i.e. each has its own state variable which selects a fragment of code to run. A task gives up the cpu by returning from its subroutine.

There is no scheduling or priorities implemented and there's a lot of busy waiting, but it's basically what cooperative multithreading is all about.
 
The following users thanked this post: BlogRahul

Offline BlogRahulTopic starter

  • Regular Contributor
  • *
  • Posts: 75
  • Country: in
Re: Program structure for cooperative scheduling
« Reply #4 on: January 12, 2022, 07:26:20 pm »
Yep, you do need to structure your code if you want this to be maintainable, otherwise it will just look like spaghetti.

I just happen to have run into this yesterday: https://github.com/cocoOS/cocoOS , from which you could probably find some inspiration.

I got the first inspiration that I should think to build a kernel first. To develop kernel I need many source files

This is my list of source files to develop kernel

main.c
Task.c
List.c
Queue.c
Timer.c
event.c

I need to write generic code for all source files
« Last Edit: January 12, 2022, 07:27:52 pm by BlogRahul »
 

Offline errorprone

  • Contributor
  • Posts: 39
Re: Program structure for cooperative scheduling
« Reply #5 on: January 13, 2022, 12:50:07 pm »
Here is another example of a cooperative scheduler that doesn’t have any extra features but is a nice demonstration of function pointers. https://www.cs.ucr.edu/~vahid/rios/
 



Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf