Skip to content

Commit 1474fcc

Browse files
committed
supervisor: Add a linked list of background callbacks
In time, we should transition interrupt driven background tasks out of the overall run_background_tasks into distinct background callbacks, so that the number of checks that occur with each tick is reduced.
1 parent dc74ae8 commit 1474fcc

File tree

5 files changed

+192
-4
lines changed

5 files changed

+192
-4
lines changed

main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
#include "background.h"
4747
#include "mpconfigboard.h"
48+
#include "supervisor/background_callback.h"
4849
#include "supervisor/cpu.h"
4950
#include "supervisor/memory.h"
5051
#include "supervisor/port.h"
@@ -161,6 +162,8 @@ void stop_mp(void) {
161162
MP_STATE_VM(vfs_cur) = vfs;
162163
#endif
163164

165+
background_callback_reset();
166+
164167
gc_deinit();
165168
}
166169

supervisor/background_callback.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef CIRCUITPY_INCLUDED_SUPERVISOR_BACKGROUND_CALLBACK_H
28+
#define CIRCUITPY_INCLUDED_SUPERVISOR_BACKGROUND_CALLBACK_H
29+
30+
/** Background callbacks are a linked list of tasks to call in the background.
31+
*
32+
* Include a member of type `background_callback_t` inside an object
33+
* which needs to queue up background work, and zero-initialize it.
34+
*
35+
* To schedule the work, use background_callback_add, with fun as the
36+
* function to call and data pointing to the object itself.
37+
*
38+
* Next time run_background_tasks_if_tick is called, the callback will
39+
* be run and removed from the linked list.
40+
*
41+
* Queueing a task that is already queued does nothing. Unconditionally
42+
* re-queueing it from its own background task will cause it to run during the
43+
* very next background-tasks invocation, leading to a CircuitPython freeze, so
44+
* don't do that.
45+
*
46+
* background_callback_add can be called from interrupt context.
47+
*/
48+
typedef void (*background_callback_fun)(void *data);
49+
typedef struct background_callback {
50+
background_callback_fun fun;
51+
void *data;
52+
struct background_callback *next;
53+
struct background_callback *prev;
54+
} background_callback_t;
55+
56+
/* Add a background callback for which 'fun' and 'data' were previously set */
57+
void background_callback_add_core(background_callback_t *cb);
58+
59+
/* Add a background callback to the given function with the given data. When
60+
* the callback involves an object on the GC heap, the 'data' must be a pointer
61+
* to that object itself, not an internal pointer. Otherwise, it can be the
62+
* case that no other references to the object itself survive, and the object
63+
* becomes garbage collected while an outstanding background callback still
64+
* exists.
65+
*/
66+
void background_callback_add(background_callback_t *cb, background_callback_fun fun, void *data);
67+
68+
/* Run all background callbacks. Normally, this is done by the supervisor
69+
* whenever the list is non-empty */
70+
void background_callback_run_all(void);
71+
72+
/* During soft reset, remove all pending callbacks and clear the critical section flag */
73+
void background_callback_reset(void);
74+
75+
/* Sometimes background callbacks must be blocked. Use these functions to
76+
* bracket the section of code where this is the case. These calls nest, and
77+
* begins must be balanced with ends.
78+
*/
79+
void background_callback_begin_critical_section(void);
80+
void background_callback_end_critical_section(void);
81+
82+
#endif
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/mpconfig.h"
28+
#include "supervisor/background_callback.h"
29+
#include "supervisor/shared/tick.h"
30+
#include "shared-bindings/microcontroller/__init__.h"
31+
32+
STATIC volatile background_callback_t *callback_head, *callback_tail;
33+
34+
#define CALLBACK_CRITICAL_BEGIN (common_hal_mcu_disable_interrupts())
35+
#define CALLBACK_CRITICAL_END (common_hal_mcu_enable_interrupts())
36+
37+
void background_callback_add_core(background_callback_t *cb) {
38+
CALLBACK_CRITICAL_BEGIN;
39+
if (cb->prev || callback_head == cb) {
40+
CALLBACK_CRITICAL_END;
41+
return;
42+
}
43+
cb->next = 0;
44+
cb->prev = (background_callback_t*)callback_tail;
45+
if (callback_tail) {
46+
callback_tail->next = cb;
47+
cb->prev = (background_callback_t*)callback_tail;
48+
}
49+
if (!callback_head) {
50+
callback_head = cb;
51+
}
52+
callback_tail = cb;
53+
CALLBACK_CRITICAL_END;
54+
}
55+
56+
void background_callback_add(background_callback_t *cb, background_callback_fun fun, void *data) {
57+
cb->fun = fun;
58+
cb->data = data;
59+
background_callback_add_core(cb);
60+
}
61+
62+
static bool in_background_callback;
63+
void background_callback_run_all() {
64+
CALLBACK_CRITICAL_BEGIN;
65+
if(in_background_callback) {
66+
CALLBACK_CRITICAL_END;
67+
return;
68+
}
69+
in_background_callback = true;
70+
background_callback_t *cb = (background_callback_t*)callback_head;
71+
callback_head = NULL;
72+
callback_tail = NULL;
73+
while (cb) {
74+
background_callback_t *next = cb->next;
75+
cb->next = cb->prev = NULL;
76+
background_callback_fun fun = cb->fun;
77+
void *data = cb->data;
78+
CALLBACK_CRITICAL_END;
79+
// Leave the critical section in order to run the callback function
80+
if (fun) {
81+
fun(data);
82+
}
83+
CALLBACK_CRITICAL_BEGIN;
84+
cb = next;
85+
}
86+
in_background_callback = false;
87+
CALLBACK_CRITICAL_END;
88+
}
89+
90+
void background_callback_begin_critical_section() {
91+
CALLBACK_CRITICAL_BEGIN;
92+
}
93+
94+
void background_callback_end_critical_section() {
95+
CALLBACK_CRITICAL_END;
96+
}
97+
98+
void background_callback_reset() {
99+
CALLBACK_CRITICAL_BEGIN;
100+
callback_head = NULL;
101+
callback_tail = NULL;
102+
in_background_callback = false;
103+
CALLBACK_CRITICAL_END;
104+
}

supervisor/shared/tick.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "py/mpstate.h"
3030
#include "supervisor/linker.h"
3131
#include "supervisor/filesystem.h"
32+
#include "supervisor/background_callback.h"
3233
#include "supervisor/port.h"
3334
#include "supervisor/shared/autoreload.h"
3435

@@ -86,10 +87,7 @@ uint32_t supervisor_ticks_ms32() {
8687
extern void run_background_tasks(void);
8788

8889
void PLACE_IN_ITCM(supervisor_run_background_tasks_if_tick)() {
89-
// TODO: Add a global that can be set by anyone to indicate we should run background tasks. That
90-
// way we can short circuit the background tasks early. We used to do it based on time but it
91-
// breaks cases where we wake up for a short period and then sleep. If we skipped the last
92-
// background task or more before sleeping we may end up starving a task like USB.
90+
background_callback_run_all();
9391
run_background_tasks();
9492
}
9593

supervisor/supervisor.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ SRC_SUPERVISOR = \
22
main.c \
33
supervisor/port.c \
44
supervisor/shared/autoreload.c \
5+
supervisor/shared/background_callback.c \
56
supervisor/shared/board.c \
67
supervisor/shared/filesystem.c \
78
supervisor/shared/flash.c \

0 commit comments

Comments
 (0)