nodemcu-firmware/components/task/include/task/task.h

59 lines
2.2 KiB
C
Raw Normal View History

#ifndef _TASK_H_
#define _TASK_H_
#include <stdint.h>
#include <stdbool.h>
/* use LOW / MEDIUM / HIGH since it isn't clear from the docs which is higher */
typedef enum {
TASK_PRIORITY_LOW,
TASK_PRIORITY_MEDIUM,
TASK_PRIORITY_HIGH,
TASK_PRIORITY_COUNT
} task_prio_t;
typedef uint32_t task_handle_t;
typedef intptr_t task_param_t;
/*
* Signals are a 32-bit number of the form header:14; count:18. The header
* is just a fixed fingerprint and the count is allocated serially by the
* task_get_id() function.
*/
bool task_post(task_prio_t priority, task_handle_t handle, task_param_t param);
2021-07-26 07:36:57 +02:00
/* When posting NodeMCU tasks from an ISR, this version MUST be used,
* and vice versa.
* Doing otherwise breaks assumptions made by the FreeRTOS kernel in terms of
* concurrency safety, and may lead to obscure and hard-to-pinpoint bugs.
* While a specific hardware port may be implemented in a manner which makes
* it concurrency safe to call FreeRTOS xxxFromISR functions from any context,
* doing so would still lead to scheduling oddities as the xxxFromISR
* functions do not perform task switching whereas the non-FromISR versions
* do. In practical terms, that means that even if a higher priority FreeRTOS
* task was unblocked, it would not get scheduled at that point.
*/
bool task_post_isr(task_prio_t priority, task_handle_t handle, task_param_t param);
#define task_post_low(handle,param) task_post(TASK_PRIORITY_LOW, handle, param)
#define task_post_medium(handle,param) task_post(TASK_PRIORITY_MEDIUM, handle, param)
#define task_post_high(handle,param) task_post(TASK_PRIORITY_HIGH, handle, param)
2021-07-26 07:36:57 +02:00
#define task_post_isr_low(handle,param) task_post_isr(TASK_PRIORITY_LOW, handle, param)
#define task_post_isr_medium(handle,param) task_post_isr(TASK_PRIORITY_MEDIUM, handle, param)
#define task_post_isr_high(handle,param) task_post_isr(TASK_PRIORITY_HIGH, handle, param)
typedef void (*task_callback_t)(task_param_t param, task_prio_t prio);
task_handle_t task_get_id(task_callback_t t);
Fix net module data loss & RTOS task unsafety (#2829) To avoid races between the lwIP callbacks (lwIP RTOS task) and the Lua handlers (LVM RTOS task), the data flow and ownership has been simplified and cleaned up. lwIP callbacks now have no visibility of the userdata struct. They are limited to creating small event objects and task_post()ing them over to the LVM "thread", passing ownership in doing so. The shared identifier then becomes the struct netconn*. On the LVM side, we keep a linked list of active userdata objects. This allows us to retrieve the correct userdata when we get an event with a netconn pointer. Because this list is only ever used within the LVM task, no locking is necessary. The old approach of stashing a userdata pointer into the 'socket' field on the netconn has been removed entirely, as this was both not thread/RTOS-task safe, and also interfered with the IDFs internal use of the socket field (even when using only the netconn layer). As an added benefit, this removed the need for all the SYS_ARCH_PROTECT() locking stuff. The need to track receive events before the corresponding userdata object has been established has been removed by virtue of not reordering the "accept" and the "recv" events any more (previously accepts were posted with medium priority, while the receives where high priority, leading to the observed reordering and associated headaches). The workaround for IDF issue 784 has been removed as it is now not needed and is in fact directly harmful as it results in a double-free. Yay for getting rid of old workarounds! DNS resolution code paths were merged for the two instances of "socket" initiated resolves (connect/dns functions). Also fixed an instance of using a stack variable for receiving the resolved IP address, with said variable going out of scope before the DNS resolution necessarily completed (hello, memory corruption!). Where possible, moved to use the Lua allocator rather than plain malloc. Finally, the NodeMCU task posting mechanism got a polish and an adjustment. Given all the Bad(tm) that tends to happen if something fails task posting, I went through a couple of iterations on how to avoid that. Alas, the preferred solution of blocking non-LVM RTOS tasks until a slot is free turned out to not be viable, as this easily resulted in deadlocks with the lwIP stack. After much deliberation I settled on increasing the number of available queue slots for the task_post() mechanism, but in the interest of user control also now made it user configurable via Kconfig.
2019-07-14 23:20:20 +02:00
/* Init, must be called before any posting happens */
void task_init (void);
/* RTOS loop to pump task messages until infinity */
void task_pump_messages (void);
#endif