2020-05-11 09:37:20 -04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-07-20 09:44:39 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-07-17 09:36:26 -04:00
|
|
|
#include "py/gc.h"
|
2020-05-11 09:37:20 -04:00
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#include "supervisor/background_callback.h"
|
2020-09-28 22:21:17 -04:00
|
|
|
#include "supervisor/linker.h"
|
2021-11-09 21:04:34 -05:00
|
|
|
#include "supervisor/port.h"
|
2020-05-11 09:37:20 -04:00
|
|
|
#include "supervisor/shared/tick.h"
|
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC volatile background_callback_t *volatile callback_head, *volatile callback_tail;
|
2020-05-11 09:37:20 -04:00
|
|
|
|
2023-05-02 15:13:54 -04:00
|
|
|
#ifndef CALLBACK_CRITICAL_BEGIN
|
2020-05-11 09:37:20 -04:00
|
|
|
#define CALLBACK_CRITICAL_BEGIN (common_hal_mcu_disable_interrupts())
|
2023-05-02 15:13:54 -04:00
|
|
|
#endif
|
|
|
|
#ifndef CALLBACK_CRITICAL_END
|
2020-05-11 09:37:20 -04:00
|
|
|
#define CALLBACK_CRITICAL_END (common_hal_mcu_enable_interrupts())
|
2023-05-02 15:13:54 -04:00
|
|
|
#endif
|
2020-05-11 09:37:20 -04:00
|
|
|
|
2023-05-02 15:13:54 -04:00
|
|
|
MP_WEAK void PLACE_IN_ITCM(port_wake_main_task)(void) {
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2021-01-09 14:59:54 -05:00
|
|
|
|
2023-02-28 18:07:35 -05:00
|
|
|
void PLACE_IN_ITCM(background_callback_add_core)(background_callback_t * cb) {
|
2020-05-11 09:37:20 -04:00
|
|
|
CALLBACK_CRITICAL_BEGIN;
|
|
|
|
if (cb->prev || callback_head == cb) {
|
|
|
|
CALLBACK_CRITICAL_END;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cb->next = 0;
|
2021-03-15 09:57:36 -04:00
|
|
|
cb->prev = (background_callback_t *)callback_tail;
|
2020-05-11 09:37:20 -04:00
|
|
|
if (callback_tail) {
|
|
|
|
callback_tail->next = cb;
|
|
|
|
}
|
|
|
|
if (!callback_head) {
|
|
|
|
callback_head = cb;
|
|
|
|
}
|
|
|
|
callback_tail = cb;
|
|
|
|
CALLBACK_CRITICAL_END;
|
2021-01-09 14:59:54 -05:00
|
|
|
|
|
|
|
port_wake_main_task();
|
2020-05-11 09:37:20 -04:00
|
|
|
}
|
|
|
|
|
2023-02-28 18:07:35 -05:00
|
|
|
void PLACE_IN_ITCM(background_callback_add)(background_callback_t * cb, background_callback_fun fun, void *data) {
|
2020-05-11 09:37:20 -04:00
|
|
|
cb->fun = fun;
|
|
|
|
cb->data = data;
|
|
|
|
background_callback_add_core(cb);
|
|
|
|
}
|
|
|
|
|
2023-08-13 19:05:09 -04:00
|
|
|
inline bool background_callback_pending(void) {
|
2021-08-19 15:18:13 -04:00
|
|
|
return callback_head != NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:37:20 -04:00
|
|
|
static bool in_background_callback;
|
2020-09-28 22:21:17 -04:00
|
|
|
void PLACE_IN_ITCM(background_callback_run_all)() {
|
2022-07-30 01:09:49 -04:00
|
|
|
port_background_task();
|
2021-08-19 15:18:13 -04:00
|
|
|
if (!background_callback_pending()) {
|
2020-07-10 15:42:21 -04:00
|
|
|
return;
|
|
|
|
}
|
2020-05-11 09:37:20 -04:00
|
|
|
CALLBACK_CRITICAL_BEGIN;
|
2020-07-10 15:42:21 -04:00
|
|
|
if (in_background_callback) {
|
2020-05-11 09:37:20 -04:00
|
|
|
CALLBACK_CRITICAL_END;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
in_background_callback = true;
|
2021-03-15 09:57:36 -04:00
|
|
|
background_callback_t *cb = (background_callback_t *)callback_head;
|
2020-05-11 09:37:20 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-06-06 19:54:02 -04:00
|
|
|
|
|
|
|
// Filter out queued callbacks if they are allocated on the heap.
|
2020-05-11 09:37:20 -04:00
|
|
|
void background_callback_reset() {
|
2022-06-06 19:54:02 -04:00
|
|
|
background_callback_t *new_head = NULL;
|
|
|
|
background_callback_t **previous_next = &new_head;
|
|
|
|
background_callback_t *new_tail = NULL;
|
2020-05-11 09:37:20 -04:00
|
|
|
CALLBACK_CRITICAL_BEGIN;
|
2021-03-15 09:57:36 -04:00
|
|
|
background_callback_t *cb = (background_callback_t *)callback_head;
|
|
|
|
while (cb) {
|
2020-07-20 09:44:39 -04:00
|
|
|
background_callback_t *next = cb->next;
|
2023-10-02 19:49:31 -04:00
|
|
|
if (gc_ptr_on_heap((void *)cb)) {
|
2022-06-06 19:54:02 -04:00
|
|
|
*previous_next = cb;
|
|
|
|
previous_next = &cb->next;
|
|
|
|
cb->next = NULL;
|
|
|
|
new_tail = cb;
|
|
|
|
} else {
|
|
|
|
memset(cb, 0, sizeof(*cb));
|
|
|
|
}
|
2020-07-20 09:44:39 -04:00
|
|
|
cb = next;
|
|
|
|
}
|
2022-06-06 19:54:02 -04:00
|
|
|
callback_head = new_head;
|
|
|
|
callback_tail = new_tail;
|
2020-05-11 09:37:20 -04:00
|
|
|
in_background_callback = false;
|
|
|
|
CALLBACK_CRITICAL_END;
|
|
|
|
}
|
2020-07-17 09:36:26 -04:00
|
|
|
|
|
|
|
void background_callback_gc_collect(void) {
|
2020-07-17 15:55:46 -04:00
|
|
|
// We don't enter the callback critical section here. We rely on
|
|
|
|
// gc_collect_ptr _NOT_ entering background callbacks, so it is not
|
|
|
|
// possible for the list to be cleared.
|
|
|
|
//
|
|
|
|
// However, it is possible for the list to be extended. We make the
|
|
|
|
// minor assumption that no newly added callback is for a
|
|
|
|
// collectable object. That is, we only plug the hole where an
|
|
|
|
// object becomes collectable AFTER it is added but before the
|
|
|
|
// callback is run, not the hole where an object was ALREADY
|
|
|
|
// collectable but adds a background task for itself.
|
|
|
|
//
|
|
|
|
// It's necessary to traverse the whole list here, as the callbacks
|
|
|
|
// themselves can be in non-gc memory, and some of the cb->data
|
|
|
|
// objects themselves might be in non-gc memory.
|
2021-03-15 09:57:36 -04:00
|
|
|
background_callback_t *cb = (background_callback_t *)callback_head;
|
|
|
|
while (cb) {
|
2020-07-17 15:55:46 -04:00
|
|
|
gc_collect_ptr(cb->data);
|
|
|
|
cb = cb->next;
|
|
|
|
}
|
2020-07-17 09:36:26 -04:00
|
|
|
}
|