arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.22k stars 224 forks source link

Task not started with multiple files in platformio (no error) #114

Closed fightforlife closed 3 years ago

fightforlife commented 3 years ago

Hello together, I have a problem getting multiple files in platformio to work. I tried to follow https://github.com/arkhipenko/TaskScheduler/tree/master/examples/Scheduler_example16_Multitab. But something ist still wrong.

Example of my code.

Main.h

#ifndef Main_h
#define Main_h
#define Module

#define _TASK_SLEEP_ON_IDLE_RUN
#include <TaskSchedulerDeclarations.h>
#include "Module.h"

extern Scheduler mainRunner;

Main.cpp

#include "Main.h"
Scheduler mainRunner;
Task ConnectionTask(1000, TASK_FOREVER, &checkConnection, &mainRunner, true);

Module.h

#ifndef Module_h
#define Module_h
#include "Main.h"

Module.cpp

#include "Module.h"
#ifdef Module

Task CC1101_loopTask(500, TASK_FOREVER, &CC1101_Loop, &mainRunner, true);

ts.cpp

#define _TASK_SLEEP_ON_IDLE_RUN
#include <TaskScheduler.h>
arkhipenko commented 3 years ago

So you are basically asking to debug your code for you?

I would suspect that your problem is undefined word Module in Module.cpp, no?

ifdef Module

Where is Module defined?

fightforlife commented 3 years ago

Sorry, #define Module is included the Main.h in my original code. This is an error in my code abstraction. I edited my example above. Other functions in Module.cpp are working just fine.

Since there is no compiler error/warning or any debug output I am currently stuck. Maybe there is a way of enabling debug output and seeing what is really hapenning in the Scheduler?

arkhipenko commented 3 years ago

Need to see the entire code.

arkhipenko commented 3 years ago

What you describe is basic Task scheduler functionality that has been proven to work in 100's of projects. The issue is somewhere else.

arkhipenko commented 3 years ago

Closing this for lack of activity

kitlaan commented 3 years ago

Since I ran into this, I figured I should leave a note.

I think the problem relates to when the global-scoped objects are initialized.

I don't believe there's a guarantee that Scheduler mainRunner is fully-constructed before the Task CC1101_loopTask in Module.cpp is constructed. So the order ends up being (I think):

  1. CC1101_loopTask calls addTask(), which sets up iFirst et al.
  2. Scheduler mainRunner constructs, which calls init() and nulls out iFirst.
  3. At this point, the task's iScheduler and linked list pointers are all suspect, so calling addTask() or deleteTask() probably ends in a crash.

My solution was to avoid hooking up the Scheduler in the Task constructor, and addTask() at some point during runtime.

arkhipenko commented 3 years ago

That is quite possible. I also suspect there is a way to figure out how platform sequences the global objects and avoid this scenario. I do not use platform.io very often, so apologies - never ran into a situation like this. Happy to look at the code if you have a working example of this behavior.