Skip to content

Commit 31aab5f

Browse files
committed
add background callbacks more carefully; remove improper clearing of callback state
1 parent 26f70c1 commit 31aab5f

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

supervisor/shared/background_callback.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#include "supervisor/shared/tick.h"
1515
#include "shared-bindings/microcontroller/__init__.h"
1616

17-
static volatile background_callback_t *volatile callback_head, *volatile callback_tail;
17+
static volatile background_callback_t *volatile callback_head = NULL;
18+
static volatile background_callback_t *volatile callback_tail = NULL;
19+
;
1820

1921
#ifndef CALLBACK_CRITICAL_BEGIN
2022
#define CALLBACK_CRITICAL_BEGIN (common_hal_mcu_disable_interrupts())
@@ -28,15 +30,26 @@ MP_WEAK void PLACE_IN_ITCM(port_wake_main_task)(void) {
2830

2931
void PLACE_IN_ITCM(background_callback_add_core)(background_callback_t * cb) {
3032
CALLBACK_CRITICAL_BEGIN;
31-
if (cb->prev || callback_head == cb) {
32-
CALLBACK_CRITICAL_END;
33-
return;
33+
// next_callback_on_list is volatile only to match callback_head declaration.
34+
volatile background_callback_t *next_callback_on_list = callback_head;
35+
// Add cb only if it is not already on the callback list.
36+
while (next_callback_on_list) {
37+
if (cb == next_callback_on_list) {
38+
// Already on the list. Don't add.
39+
CALLBACK_CRITICAL_END;
40+
return;
41+
}
42+
next_callback_on_list = next_callback_on_list->next;
3443
}
44+
45+
// Add the cb to the end of the list.
3546
cb->next = 0;
3647
cb->prev = (background_callback_t *)callback_tail;
3748
if (callback_tail) {
3849
callback_tail->next = cb;
3950
}
51+
52+
// If the callback list was empty, record that cb is the first item.
4053
if (!callback_head) {
4154
callback_head = cb;
4255
}

supervisor/shared/workflow.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ void supervisor_workflow_reset(void) {
4545
bool result = supervisor_start_web_workflow();
4646
if (result) {
4747
if (!workflow_background_cb.fun) {
48-
memset(&workflow_background_cb, 0, sizeof(workflow_background_cb));
4948
workflow_background_cb.fun = supervisor_web_workflow_background;
5049
}
5150
supervisor_workflow_request_background();
@@ -108,7 +107,6 @@ void supervisor_workflow_start(void) {
108107
#if CIRCUITPY_WEB_WORKFLOW
109108
if (supervisor_start_web_workflow()) {
110109
// Enable background callbacks if web_workflow startup successful.
111-
memset(&workflow_background_cb, 0, sizeof(workflow_background_cb));
112110
workflow_background_cb.fun = supervisor_web_workflow_background;
113111
// Kick the first background run now that the callback is installed.
114112
supervisor_workflow_request_background();

0 commit comments

Comments
 (0)