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.
This commit is contained in:
Jeff Epler 2020-05-11 08:37:20 -05:00
parent dc74ae83da
commit 1474fccd2f
5 changed files with 192 additions and 4 deletions

3
main.c
View File

@ -45,6 +45,7 @@
#include "background.h"
#include "mpconfigboard.h"
#include "supervisor/background_callback.h"
#include "supervisor/cpu.h"
#include "supervisor/memory.h"
#include "supervisor/port.h"
@ -161,6 +162,8 @@ void stop_mp(void) {
MP_STATE_VM(vfs_cur) = vfs;
#endif
background_callback_reset();
gc_deinit();
}

View File

@ -0,0 +1,82 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef CIRCUITPY_INCLUDED_SUPERVISOR_BACKGROUND_CALLBACK_H
#define CIRCUITPY_INCLUDED_SUPERVISOR_BACKGROUND_CALLBACK_H
/** Background callbacks are a linked list of tasks to call in the background.
*
* Include a member of type `background_callback_t` inside an object
* which needs to queue up background work, and zero-initialize it.
*
* To schedule the work, use background_callback_add, with fun as the
* function to call and data pointing to the object itself.
*
* Next time run_background_tasks_if_tick is called, the callback will
* be run and removed from the linked list.
*
* Queueing a task that is already queued does nothing. Unconditionally
* re-queueing it from its own background task will cause it to run during the
* very next background-tasks invocation, leading to a CircuitPython freeze, so
* don't do that.
*
* background_callback_add can be called from interrupt context.
*/
typedef void (*background_callback_fun)(void *data);
typedef struct background_callback {
background_callback_fun fun;
void *data;
struct background_callback *next;
struct background_callback *prev;
} background_callback_t;
/* Add a background callback for which 'fun' and 'data' were previously set */
void background_callback_add_core(background_callback_t *cb);
/* Add a background callback to the given function with the given data. When
* the callback involves an object on the GC heap, the 'data' must be a pointer
* to that object itself, not an internal pointer. Otherwise, it can be the
* case that no other references to the object itself survive, and the object
* becomes garbage collected while an outstanding background callback still
* exists.
*/
void background_callback_add(background_callback_t *cb, background_callback_fun fun, void *data);
/* Run all background callbacks. Normally, this is done by the supervisor
* whenever the list is non-empty */
void background_callback_run_all(void);
/* During soft reset, remove all pending callbacks and clear the critical section flag */
void background_callback_reset(void);
/* Sometimes background callbacks must be blocked. Use these functions to
* bracket the section of code where this is the case. These calls nest, and
* begins must be balanced with ends.
*/
void background_callback_begin_critical_section(void);
void background_callback_end_critical_section(void);
#endif

View File

@ -0,0 +1,104 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/mpconfig.h"
#include "supervisor/background_callback.h"
#include "supervisor/shared/tick.h"
#include "shared-bindings/microcontroller/__init__.h"
STATIC volatile background_callback_t *callback_head, *callback_tail;
#define CALLBACK_CRITICAL_BEGIN (common_hal_mcu_disable_interrupts())
#define CALLBACK_CRITICAL_END (common_hal_mcu_enable_interrupts())
void background_callback_add_core(background_callback_t *cb) {
CALLBACK_CRITICAL_BEGIN;
if (cb->prev || callback_head == cb) {
CALLBACK_CRITICAL_END;
return;
}
cb->next = 0;
cb->prev = (background_callback_t*)callback_tail;
if (callback_tail) {
callback_tail->next = cb;
cb->prev = (background_callback_t*)callback_tail;
}
if (!callback_head) {
callback_head = cb;
}
callback_tail = cb;
CALLBACK_CRITICAL_END;
}
void background_callback_add(background_callback_t *cb, background_callback_fun fun, void *data) {
cb->fun = fun;
cb->data = data;
background_callback_add_core(cb);
}
static bool in_background_callback;
void background_callback_run_all() {
CALLBACK_CRITICAL_BEGIN;
if(in_background_callback) {
CALLBACK_CRITICAL_END;
return;
}
in_background_callback = true;
background_callback_t *cb = (background_callback_t*)callback_head;
callback_head = NULL;
callback_tail = NULL;
while (cb) {
background_callback_t *next = cb->next;
cb->next = cb->prev = NULL;
background_callback_fun fun = cb->fun;
void *data = cb->data;
CALLBACK_CRITICAL_END;
// Leave the critical section in order to run the callback function
if (fun) {
fun(data);
}
CALLBACK_CRITICAL_BEGIN;
cb = next;
}
in_background_callback = false;
CALLBACK_CRITICAL_END;
}
void background_callback_begin_critical_section() {
CALLBACK_CRITICAL_BEGIN;
}
void background_callback_end_critical_section() {
CALLBACK_CRITICAL_END;
}
void background_callback_reset() {
CALLBACK_CRITICAL_BEGIN;
callback_head = NULL;
callback_tail = NULL;
in_background_callback = false;
CALLBACK_CRITICAL_END;
}

View File

@ -29,6 +29,7 @@
#include "py/mpstate.h"
#include "supervisor/linker.h"
#include "supervisor/filesystem.h"
#include "supervisor/background_callback.h"
#include "supervisor/port.h"
#include "supervisor/shared/autoreload.h"
@ -86,10 +87,7 @@ uint32_t supervisor_ticks_ms32() {
extern void run_background_tasks(void);
void PLACE_IN_ITCM(supervisor_run_background_tasks_if_tick)() {
// TODO: Add a global that can be set by anyone to indicate we should run background tasks. That
// way we can short circuit the background tasks early. We used to do it based on time but it
// breaks cases where we wake up for a short period and then sleep. If we skipped the last
// background task or more before sleeping we may end up starving a task like USB.
background_callback_run_all();
run_background_tasks();
}

View File

@ -2,6 +2,7 @@ SRC_SUPERVISOR = \
main.c \
supervisor/port.c \
supervisor/shared/autoreload.c \
supervisor/shared/background_callback.c \
supervisor/shared/board.c \
supervisor/shared/filesystem.c \
supervisor/shared/flash.c \