Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define _TASK_THREAD_SAFE for TaskScheduler #1598

Merged
merged 1 commit into from
Jan 4, 2024

Conversation

schlimmchen
Copy link
Contributor

I appreciate the use of TaskScheduler as an alternative to extending the loop() approach.

To make further use of TaskScheduler's features, it makes sense to me to change task scheduling intervals dynamically at runtime. Example: The MQTT loop is executed at a specific interval, the MQTT publish interval. Once the user changes this interval, the scheduling interval of the MQTT loop should be changed accordingly. The straight-forward approach is to call setInterval() in the respective web server handler. That will execute in the web server's context. Changing TaskScheduler task properties from another context is not inherently save.

In platformio.ini, the build_flags should use -D_TASK_THREAD_SAFE to enable the use of an internal mutex which prevents unexpected behavior once multiple threads (FreeRTOS tasks) start changing task or scheduler settings.

An excerpt from the TaskScheduler docs:

#define _TASK_THREAD_SAFE

…will use an internal mutex to protect task scheduling methods from preemption and unexpected behavior. This is a recommended option for esp32 and/or other MCU's running TaskScheduler under preemptive scheduler like FreeRTOS.

The author suggests to define this flag when using TaskScheduler on ESP32.

the TaskScheduler runs in the context of a FreeRTOS thread/task. there
are other such threads (MQTT client and web server in particular). to
allow changing TaskScheduler task properties from different threads, we
need to enable the use of a mutex to protect the TaskScheduler.
@tbnobody tbnobody merged commit 892230b into tbnobody:master Jan 4, 2024
8 checks passed
@schlimmchen schlimmchen deleted the taskscheduler-thread-safety branch January 5, 2024 07:44
Copy link

github-actions bot commented Apr 2, 2024

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new discussion or issue for related concerns.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants