Giving a semaphore from the main function

sarahaka wrote on Sunday, February 24, 2019:

Hello,

I want to acheive the following:
1. Once the main starts, check for the type of reset of the microcontroller. This can be a user reset, software reset etc…
2. Based on the type of reset, give a semaphore to a specific task.

The problem is, techincially I am giving a smaphore to a task that is not yet created because I am checking for the type of reset in the begining of the main function. Initially I have several tasks, I only want the reset_task to run incase of a specific reset, while the other tasks will not run. The code looks like this:

int main(){

    check for type of reset;
    if (type of reset == software reset){
        Give a semaphore to task 3;
        (Or in other words, task 3 should run first and keepi running)
        (all other tasks will stay blocked)
    }
    create task 1;
    create task 2;
    create task 3;
}

Main function is not a task, but I need to check for resets at this point. This is the contradiction. Thus my question is, how do I make sure that task 3 is the first task to run and keep running. Task 3 is initially blocking on a semaphore.

I know that I may give the semaphore from task 1 or 2, but that mean I need to use a flag to store the state and it also means I need to copy paste the same peice of code into both tasks. Is there any better wat to do this?

Thank you

hs2sf wrote on Sunday, February 24, 2019:

What about using a flag checked by ‘root’ task 3 and let task 3 then create ‘child’ task 1 and 2 ?

richarddamon wrote on Sunday, February 24, 2019:

First, you can’t give a flag to a specific task, but only raise or lower a given semaphore and let each task have their own.

What I would do is change your order a bit,
First check for the type of reset and save that value,
Next, create all of tasks/semaphores/queue/etc needed in the system (or at least all that can be created prescheduler)
Finally, based on the saved flag value, ajust things to get the state you want.

You could have each of the task begin with taking a semaphore that is specific for that task, and only give the semaphore when that task is ready to run. Or you coul after creating each of the tasks suspend it, and then issue a resume when things are ready for it to run.

richarddamon wrote on Sunday, February 24, 2019:

First, you can’t give a flag to a specific task, but only raise or lower a given semaphore and let each task have their own.

What I would do is change your order a bit,
First check for the type of reset and save that value,
Next, create all of tasks/semaphores/queue/etc needed in the system (or at least all that can be created prescheduler)
Finally, based on the saved flag value, ajust things to get the state you want.

You could have each of the task begin with taking a semaphore that is specific for that task, and only give the semaphore when that task is ready to run. Or you coul after creating each of the tasks suspend it, and then issue a resume when things are ready for it to run.