Skip to content

Commit 56d2923

Browse files
Prevent idle priority threads from potentially starving the FreeRTOS idle task (#3909)
* FuriThread: Make FuriThreadPriorityIdle equal to the FreeRTOS one, remove FuriThreadPriorityNone This magic constant was meaningless, FuriThreadPriorityNormal is now assigned by default instead. * Make furi_thread_list_process private Its 'runtime' parameter is to be obtained from FreeRTOS, which means apps cannot do it. * DirectDraw: Remove an useless include and fix memory leak Makes this debug app compileable with uFBT out of the box Co-authored-by: あく <alleteam@gmail.com>
1 parent 7fc209f commit 56d2923

File tree

8 files changed

+35
-25
lines changed

8 files changed

+35
-25
lines changed

applications/debug/direct_draw/direct_draw.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <furi.h>
22
#include <gui/gui.h>
3-
#include <gui/canvas_i.h>
43
#include <input/input.h>
54

65
#define BUFFER_SIZE (32U)
@@ -42,10 +41,11 @@ static DirectDraw* direct_draw_alloc(void) {
4241
static void direct_draw_free(DirectDraw* instance) {
4342
furi_pubsub_unsubscribe(instance->input, instance->input_subscription);
4443

45-
instance->canvas = NULL;
4644
gui_direct_draw_release(instance->gui);
4745
furi_record_close(RECORD_GUI);
4846
furi_record_close(RECORD_INPUT_EVENTS);
47+
48+
free(instance);
4949
}
5050

5151
static void direct_draw_block(Canvas* canvas, uint32_t size, uint32_t counter) {

furi/core/thread.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "thread.h"
2-
#include "thread_list.h"
2+
#include "thread_list_i.h"
33
#include "kernel.h"
44
#include "memmgr.h"
55
#include "memmgr_heap.h"
@@ -65,6 +65,9 @@ struct FuriThread {
6565
// IMPORTANT: container MUST be the FIRST struct member
6666
static_assert(offsetof(FuriThread, container) == 0);
6767

68+
// Our idle priority should be equal to the one from FreeRTOS
69+
static_assert(FuriThreadPriorityIdle == tskIDLE_PRIORITY);
70+
6871
static size_t __furi_thread_stdout_write(FuriThread* thread, const char* data, size_t size);
6972
static int32_t __furi_thread_stdout_flush(FuriThread* thread);
7073

@@ -145,6 +148,8 @@ static void furi_thread_init_common(FuriThread* thread) {
145148
furi_thread_set_appid(thread, "driver");
146149
}
147150

151+
thread->priority = FuriThreadPriorityNormal;
152+
148153
FuriHalRtcHeapTrackMode mode = furi_hal_rtc_get_heap_track_mode();
149154
if(mode == FuriHalRtcHeapTrackModeAll) {
150155
thread->heap_trace_enabled = true;
@@ -269,7 +274,7 @@ void furi_thread_set_context(FuriThread* thread, void* context) {
269274
void furi_thread_set_priority(FuriThread* thread, FuriThreadPriority priority) {
270275
furi_check(thread);
271276
furi_check(thread->state == FuriThreadStateStopped);
272-
furi_check(priority >= FuriThreadPriorityIdle && priority <= FuriThreadPriorityIsr);
277+
furi_check(priority <= FuriThreadPriorityIsr);
273278
thread->priority = priority;
274279
}
275280

@@ -281,9 +286,7 @@ FuriThreadPriority furi_thread_get_priority(FuriThread* thread) {
281286

282287
void furi_thread_set_current_priority(FuriThreadPriority priority) {
283288
furi_check(priority <= FuriThreadPriorityIsr);
284-
285-
UBaseType_t new_priority = priority ? priority : FuriThreadPriorityNormal;
286-
vTaskPrioritySet(NULL, new_priority);
289+
vTaskPrioritySet(NULL, priority);
287290
}
288291

289292
FuriThreadPriority furi_thread_get_current_priority(void) {
@@ -345,7 +348,6 @@ void furi_thread_start(FuriThread* thread) {
345348
furi_thread_set_state(thread, FuriThreadStateStarting);
346349

347350
uint32_t stack_depth = thread->stack_size / sizeof(StackType_t);
348-
UBaseType_t priority = thread->priority ? thread->priority : FuriThreadPriorityNormal;
349351

350352
thread->is_active = true;
351353

@@ -355,7 +357,7 @@ void furi_thread_start(FuriThread* thread) {
355357
thread->name,
356358
stack_depth,
357359
thread,
358-
priority,
360+
thread->priority,
359361
thread->stack_buffer,
360362
&thread->container) == (TaskHandle_t)thread);
361363
}

furi/core/thread.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ typedef enum {
3030
* @brief Enumeration of possible FuriThread priorities.
3131
*/
3232
typedef enum {
33-
FuriThreadPriorityNone = 0, /**< Uninitialized, choose system default */
34-
FuriThreadPriorityIdle = 1, /**< Idle priority */
33+
FuriThreadPriorityIdle = 0, /**< Idle priority */
3534
FuriThreadPriorityLowest = 14, /**< Lowest */
3635
FuriThreadPriorityLow = 15, /**< Low */
37-
FuriThreadPriorityNormal = 16, /**< Normal */
36+
FuriThreadPriorityNormal = 16, /**< Normal, system default */
3837
FuriThreadPriorityHigh = 17, /**< High */
3938
FuriThreadPriorityHighest = 18, /**< Highest */
4039
FuriThreadPriorityIsr =

furi/core/thread_list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ FuriThreadListItem* furi_thread_list_get_or_insert(FuriThreadList* instance, Fur
8484
}
8585

8686
void furi_thread_list_process(FuriThreadList* instance, uint32_t runtime, uint32_t tick) {
87-
furi_check(instance);
87+
furi_assert(instance);
8888

8989
instance->runtime_previous = instance->runtime_current;
9090
instance->runtime_current = runtime;

furi/core/thread_list.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ FuriThreadListItem* furi_thread_list_get_at(FuriThreadList* instance, size_t pos
6868
*/
6969
FuriThreadListItem* furi_thread_list_get_or_insert(FuriThreadList* instance, FuriThread* thread);
7070

71-
/** Process items in the FuriThreadList instance
72-
*
73-
* @param instance The instance
74-
* @param[in] runtime The runtime of the system since start
75-
* @param[in] tick The tick when processing happened
76-
*/
77-
void furi_thread_list_process(FuriThreadList* instance, uint32_t runtime, uint32_t tick);
78-
7971
/** Get percent of time spent in ISR
8072
*
8173
* @param instance The instance

furi/core/thread_list_i.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "thread_list.h"
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
/** Process items in the FuriThreadList instance
10+
*
11+
* @param instance The instance
12+
* @param[in] runtime The runtime of the system since start
13+
* @param[in] tick The tick when processing happened
14+
*/
15+
void furi_thread_list_process(FuriThreadList* instance, uint32_t runtime, uint32_t tick);
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif

targets/f18/api_symbols.csv

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
entry,status,name,type,params
2-
Version,+,74.0,,
2+
Version,+,75.0,,
33
Header,+,applications/services/bt/bt_service/bt.h,,
44
Header,+,applications/services/bt/bt_service/bt_keys_storage.h,,
55
Header,+,applications/services/cli/cli.h,,
@@ -1652,7 +1652,6 @@ Function,+,furi_thread_list_free,void,FuriThreadList*
16521652
Function,+,furi_thread_list_get_at,FuriThreadListItem*,"FuriThreadList*, size_t"
16531653
Function,+,furi_thread_list_get_isr_time,float,FuriThreadList*
16541654
Function,+,furi_thread_list_get_or_insert,FuriThreadListItem*,"FuriThreadList*, FuriThread*"
1655-
Function,+,furi_thread_list_process,void,"FuriThreadList*, uint32_t, uint32_t"
16561655
Function,+,furi_thread_list_size,size_t,FuriThreadList*
16571656
Function,+,furi_thread_resume,void,FuriThreadId
16581657
Function,+,furi_thread_set_appid,void,"FuriThread*, const char*"

targets/f7/api_symbols.csv

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
entry,status,name,type,params
2-
Version,+,74.0,,
2+
Version,+,75.0,,
33
Header,+,applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h,,
44
Header,+,applications/services/bt/bt_service/bt.h,,
55
Header,+,applications/services/bt/bt_service/bt_keys_storage.h,,
@@ -1866,7 +1866,6 @@ Function,+,furi_thread_list_free,void,FuriThreadList*
18661866
Function,+,furi_thread_list_get_at,FuriThreadListItem*,"FuriThreadList*, size_t"
18671867
Function,+,furi_thread_list_get_isr_time,float,FuriThreadList*
18681868
Function,+,furi_thread_list_get_or_insert,FuriThreadListItem*,"FuriThreadList*, FuriThread*"
1869-
Function,+,furi_thread_list_process,void,"FuriThreadList*, uint32_t, uint32_t"
18701869
Function,+,furi_thread_list_size,size_t,FuriThreadList*
18711870
Function,+,furi_thread_resume,void,FuriThreadId
18721871
Function,+,furi_thread_set_appid,void,"FuriThread*, const char*"

0 commit comments

Comments
 (0)