summaryrefslogtreecommitdiff
path: root/supervisor/shared/background_callback.c
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2020-05-11 08:37:20 -0500
committerJeff Epler <jepler@gmail.com>2020-07-15 09:26:47 -0500
commit1474fccd2fb1cf6ccd14979a3b5405d2a355054b (patch)
treef8f21c3712388c34176f1d142b808e488042d1f5 /supervisor/shared/background_callback.c
parentdc74ae83da6f519a36d75388b5be9923a10bf063 (diff)
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.
Diffstat (limited to 'supervisor/shared/background_callback.c')
-rw-r--r--supervisor/shared/background_callback.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/supervisor/shared/background_callback.c b/supervisor/shared/background_callback.c
new file mode 100644
index 000000000..99607b7a8
--- /dev/null
+++ b/supervisor/shared/background_callback.c
@@ -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;
+}