|
| 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 |
0 commit comments