LCOV - code coverage report
Current view: top level - py - scheduler.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-724-gfb7d21153.info Lines: 68 68 100.0 %
Date: 2022-12-01 09:37:31 Functions: 7 7 100.0 %
Branches: 22 24 91.7 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2017 Damien P. George
       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 <stdio.h>
      28                 :            : 
      29                 :            : #include "py/runtime.h"
      30                 :            : 
      31                 :            : // Schedules an exception on the main thread (for exceptions "thrown" by async
      32                 :            : // sources such as interrupts and UNIX signal handlers).
      33                 :          8 : void MICROPY_WRAP_MP_SCHED_EXCEPTION(mp_sched_exception)(mp_obj_t exc) {
      34                 :          8 :     MP_STATE_MAIN_THREAD(mp_pending_exception) = exc;
      35                 :            : 
      36                 :            :     #if MICROPY_ENABLE_SCHEDULER && !MICROPY_PY_THREAD
      37                 :            :     // Optimisation for the case where we have scheduler but no threading.
      38                 :            :     // Allows the VM to do a single check to exclude both pending exception
      39                 :            :     // and queued tasks.
      40                 :            :     if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
      41                 :            :         MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
      42                 :            :     }
      43                 :            :     #endif
      44                 :          8 : }
      45                 :            : 
      46                 :            : #if MICROPY_KBD_EXCEPTION
      47                 :            : // This function may be called asynchronously at any time so only do the bare minimum.
      48                 :          8 : void MICROPY_WRAP_MP_SCHED_KEYBOARD_INTERRUPT(mp_sched_keyboard_interrupt)(void) {
      49                 :          8 :     MP_STATE_VM(mp_kbd_exception).traceback_data = NULL;
      50                 :          8 :     mp_sched_exception(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)));
      51                 :          8 : }
      52                 :            : #endif
      53                 :            : 
      54                 :            : #if MICROPY_ENABLE_SCHEDULER
      55                 :            : 
      56                 :            : #define IDX_MASK(i) ((i) & (MICROPY_SCHEDULER_DEPTH - 1))
      57                 :            : 
      58                 :            : // This is a macro so it is guaranteed to be inlined in functions like
      59                 :            : // mp_sched_schedule that may be located in a special memory region.
      60                 :            : #define mp_sched_full() (mp_sched_num_pending() == MICROPY_SCHEDULER_DEPTH)
      61                 :            : 
      62                 :      15576 : static inline bool mp_sched_empty(void) {
      63                 :      15576 :     MP_STATIC_ASSERT(MICROPY_SCHEDULER_DEPTH <= 255); // MICROPY_SCHEDULER_DEPTH must fit in 8 bits
      64                 :      15576 :     MP_STATIC_ASSERT((IDX_MASK(MICROPY_SCHEDULER_DEPTH) == 0)); // MICROPY_SCHEDULER_DEPTH must be a power of 2
      65                 :            : 
      66                 :      15576 :     return mp_sched_num_pending() == 0;
      67                 :            : }
      68                 :            : 
      69                 :      15627 : static inline void mp_sched_run_pending(void) {
      70                 :      15627 :     mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
      71         [ +  + ]:      15660 :     if (MP_STATE_VM(sched_state) != MP_SCHED_PENDING) {
      72                 :            :         // Something else (e.g. hard IRQ) locked the scheduler while we
      73                 :            :         // acquired the lock.
      74                 :         84 :         MICROPY_END_ATOMIC_SECTION(atomic_state);
      75                 :         84 :         return;
      76                 :            :     }
      77                 :            : 
      78                 :            :     // Equivalent to mp_sched_lock(), but we're already in the atomic
      79                 :            :     // section and know that we're pending.
      80                 :      15576 :     MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
      81                 :            : 
      82                 :            :     #if MICROPY_SCHEDULER_STATIC_NODES
      83                 :            :     // Run all pending C callbacks.
      84                 :            :     while (MP_STATE_VM(sched_head) != NULL) {
      85                 :            :         mp_sched_node_t *node = MP_STATE_VM(sched_head);
      86                 :            :         MP_STATE_VM(sched_head) = node->next;
      87                 :            :         if (MP_STATE_VM(sched_head) == NULL) {
      88                 :            :             MP_STATE_VM(sched_tail) = NULL;
      89                 :            :         }
      90                 :            :         mp_sched_callback_t callback = node->callback;
      91                 :            :         node->callback = NULL;
      92                 :            :         MICROPY_END_ATOMIC_SECTION(atomic_state);
      93                 :            :         callback(node);
      94                 :            :         atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
      95                 :            :     }
      96                 :            :     #endif
      97                 :            : 
      98                 :            :     // Run at most one pending Python callback.
      99         [ +  + ]:      15576 :     if (!mp_sched_empty()) {
     100                 :      13246 :         mp_sched_item_t item = MP_STATE_VM(sched_queue)[MP_STATE_VM(sched_idx)];
     101                 :      13246 :         MP_STATE_VM(sched_idx) = IDX_MASK(MP_STATE_VM(sched_idx) + 1);
     102                 :      13246 :         --MP_STATE_VM(sched_len);
     103                 :      13246 :         MICROPY_END_ATOMIC_SECTION(atomic_state);
     104                 :      13246 :         mp_call_function_1_protected(item.func, item.arg);
     105                 :            :     } else {
     106                 :       2330 :         MICROPY_END_ATOMIC_SECTION(atomic_state);
     107                 :            :     }
     108                 :            : 
     109                 :            :     // Restore MP_STATE_VM(sched_state) to idle (or pending if there are still
     110                 :            :     // tasks in the queue).
     111                 :      15576 :     mp_sched_unlock();
     112                 :            : }
     113                 :            : 
     114                 :            : // Locking the scheduler prevents tasks from executing (does not prevent new
     115                 :            : // tasks from being added). We lock the scheduler while executing scheduled
     116                 :            : // tasks and also in hard interrupts or GC finalisers.
     117                 :       4040 : void mp_sched_lock(void) {
     118                 :       4040 :     mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
     119         [ +  + ]:       4040 :     if (MP_STATE_VM(sched_state) < 0) {
     120                 :            :         // Already locked, increment lock (recursive lock).
     121                 :          2 :         --MP_STATE_VM(sched_state);
     122                 :            :     } else {
     123                 :            :         // Pending or idle.
     124                 :       4038 :         MP_STATE_VM(sched_state) = MP_SCHED_LOCKED;
     125                 :            :     }
     126                 :       4040 :     MICROPY_END_ATOMIC_SECTION(atomic_state);
     127                 :       4040 : }
     128                 :            : 
     129                 :      19616 : void mp_sched_unlock(void) {
     130                 :      19616 :     mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
     131         [ -  + ]:      19616 :     assert(MP_STATE_VM(sched_state) < 0);
     132         [ +  + ]:      19616 :     if (++MP_STATE_VM(sched_state) == 0) {
     133                 :            :         // Scheduler became unlocked. Check if there are still tasks in the
     134                 :            :         // queue and set sched_state accordingly.
     135                 :      19614 :         if (
     136                 :            :             #if !MICROPY_PY_THREAD
     137                 :            :             // See optimisation in mp_sched_exception.
     138                 :            :             MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL ||
     139                 :            :             #endif
     140                 :            :             #if MICROPY_SCHEDULER_STATIC_NODES
     141                 :            :             MP_STATE_VM(sched_head) != NULL ||
     142                 :            :             #endif
     143         [ +  + ]:      19614 :             mp_sched_num_pending()) {
     144                 :        205 :             MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
     145                 :            :         } else {
     146                 :      19409 :             MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
     147                 :            :         }
     148                 :            :     }
     149                 :      19616 :     MICROPY_END_ATOMIC_SECTION(atomic_state);
     150                 :      19616 : }
     151                 :            : 
     152                 :      13269 : bool MICROPY_WRAP_MP_SCHED_SCHEDULE(mp_sched_schedule)(mp_obj_t function, mp_obj_t arg) {
     153                 :      13269 :     mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
     154                 :      13270 :     bool ret;
     155         [ +  + ]:      13270 :     if (!mp_sched_full()) {
     156         [ +  + ]:      13251 :         if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
     157                 :      13045 :             MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
     158                 :            :         }
     159                 :      13251 :         uint8_t iput = IDX_MASK(MP_STATE_VM(sched_idx) + MP_STATE_VM(sched_len)++);
     160                 :      13251 :         MP_STATE_VM(sched_queue)[iput].func = function;
     161                 :      13251 :         MP_STATE_VM(sched_queue)[iput].arg = arg;
     162                 :      13251 :         MICROPY_SCHED_HOOK_SCHEDULED;
     163                 :      13251 :         ret = true;
     164                 :            :     } else {
     165                 :            :         // schedule queue is full
     166                 :            :         ret = false;
     167                 :            :     }
     168                 :      13270 :     MICROPY_END_ATOMIC_SECTION(atomic_state);
     169                 :      13270 :     return ret;
     170                 :            : }
     171                 :            : 
     172                 :            : #if MICROPY_SCHEDULER_STATIC_NODES
     173                 :            : bool mp_sched_schedule_node(mp_sched_node_t *node, mp_sched_callback_t callback) {
     174                 :            :     mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
     175                 :            :     bool ret;
     176                 :            :     if (node->callback == NULL) {
     177                 :            :         if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
     178                 :            :             MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
     179                 :            :         }
     180                 :            :         node->callback = callback;
     181                 :            :         node->next = NULL;
     182                 :            :         if (MP_STATE_VM(sched_tail) == NULL) {
     183                 :            :             MP_STATE_VM(sched_head) = node;
     184                 :            :         } else {
     185                 :            :             MP_STATE_VM(sched_tail)->next = node;
     186                 :            :         }
     187                 :            :         MP_STATE_VM(sched_tail) = node;
     188                 :            :         MICROPY_SCHED_HOOK_SCHEDULED;
     189                 :            :         ret = true;
     190                 :            :     } else {
     191                 :            :         // already scheduled
     192                 :            :         ret = false;
     193                 :            :     }
     194                 :            :     MICROPY_END_ATOMIC_SECTION(atomic_state);
     195                 :            :     return ret;
     196                 :            : }
     197                 :            : #endif
     198                 :            : 
     199                 :            : MP_REGISTER_ROOT_POINTER(mp_sched_item_t sched_queue[MICROPY_SCHEDULER_DEPTH]);
     200                 :            : 
     201                 :            : #endif // MICROPY_ENABLE_SCHEDULER
     202                 :            : 
     203                 :            : // Called periodically from the VM or from "waiting" code (e.g. sleep) to
     204                 :            : // process background tasks and pending exceptions (e.g. KeyboardInterrupt).
     205                 :      26823 : void mp_handle_pending(bool raise_exc) {
     206         [ +  + ]:      26823 :     if (MP_STATE_THREAD(mp_pending_exception) != MP_OBJ_NULL) {
     207                 :          6 :         mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
     208                 :          6 :         mp_obj_t obj = MP_STATE_THREAD(mp_pending_exception);
     209         [ +  - ]:          6 :         if (obj != MP_OBJ_NULL) {
     210                 :          6 :             MP_STATE_THREAD(mp_pending_exception) = MP_OBJ_NULL;
     211         [ +  + ]:          6 :             if (raise_exc) {
     212                 :          4 :                 MICROPY_END_ATOMIC_SECTION(atomic_state);
     213                 :          4 :                 nlr_raise(obj);
     214                 :            :             }
     215                 :            :         }
     216                 :          2 :         MICROPY_END_ATOMIC_SECTION(atomic_state);
     217                 :            :     }
     218                 :            :     #if MICROPY_ENABLE_SCHEDULER
     219         [ +  + ]:      26795 :     if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {
     220                 :      15627 :         mp_sched_run_pending();
     221                 :            :     }
     222                 :            :     #endif
     223                 :      26827 : }

Generated by: LCOV version 1.15-5-g462f71d