LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/arch/posix/core - swap.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 17 17
Test Date: 2026-11-06 14:43:19 Functions: 100.0 % 2 2
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2010-2015 Wind River Systems, Inc.
       3                 :             :  * Copyright (c) 2017 Oticon A/S
       4                 :             :  *
       5                 :             :  * SPDX-License-Identifier: Apache-2.0
       6                 :             :  */
       7                 :             : 
       8                 :             : /**
       9                 :             :  * @file
      10                 :             :  * @brief Kernel swapper code for POSIX
      11                 :             :  *
      12                 :             :  * This module implements the arch_swap() routine for the POSIX architecture.
      13                 :             :  *
      14                 :             :  */
      15                 :             : 
      16                 :             : #include <zephyr/kernel.h>
      17                 :             : #include "posix_core.h"
      18                 :             : #include <zephyr/irq.h>
      19                 :             : #include "kswap.h"
      20                 :             : #include <zephyr/pm/pm.h>
      21                 :             : 
      22                 :         114 : int arch_swap(unsigned int key)
      23                 :             : {
      24                 :             :         /*
      25                 :             :          * struct k_thread * _current is the currently running thread
      26                 :             :          * struct k_thread * _kernel.ready_q.cache contains the next thread to
      27                 :             :          * run (cannot be NULL)
      28                 :             :          *
      29                 :             :          * Here a "real" arch would save all processor registers, stack pointer
      30                 :             :          * and so forth.  But we do not need to do so because we use posix
      31                 :             :          * threads => those are all nicely kept by the native OS kernel
      32                 :             :          */
      33                 :             : #if CONFIG_INSTRUMENT_THREAD_SWITCHING
      34                 :             :         z_thread_mark_switched_out();
      35                 :             : #endif
      36                 :         114 :         _current->callee_saved.key = key;
      37                 :         114 :         _current->callee_saved.retval = -EAGAIN;
      38                 :             : 
      39                 :             :         /* retval may be modified with a call to
      40                 :             :          * arch_thread_return_value_set()
      41                 :             :          */
      42                 :             : 
      43                 :         114 :         posix_thread_status_t *ready_thread_ptr =
      44                 :             :                 (posix_thread_status_t *)
      45                 :         114 :                 _kernel.ready_q.cache->callee_saved.thread_status;
      46                 :             : 
      47                 :         114 :         posix_thread_status_t *this_thread_ptr  =
      48                 :             :                 (posix_thread_status_t *)
      49                 :             :                 _current->callee_saved.thread_status;
      50                 :             : 
      51                 :             : 
      52                 :         114 :         z_current_thread_set(_kernel.ready_q.cache);
      53                 :             : #if CONFIG_INSTRUMENT_THREAD_SWITCHING
      54                 :             :         z_thread_mark_switched_in();
      55                 :             : #endif
      56                 :             : 
      57                 :             :         /*
      58                 :             :          * Here a "real" arch would load all processor registers for the thread
      59                 :             :          * to run. In this arch case, we just block this thread until allowed
      60                 :             :          * to run later, and signal to whomever is allowed to run to
      61                 :             :          * continue.
      62                 :             :          */
      63                 :         114 :         posix_swap(ready_thread_ptr->thread_idx,
      64                 :             :                 this_thread_ptr->thread_idx);
      65                 :             : 
      66                 :             :         /* When we continue, _kernel->current points back to this thread */
      67                 :             : 
      68                 :          60 :         irq_unlock(_current->callee_saved.key);
      69                 :             : 
      70                 :          60 :         return _current->callee_saved.retval;
      71                 :             : }
      72                 :             : 
      73                 :             : 
      74                 :             : 
      75                 :             : #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN
      76                 :             : /* This is just a version of arch_swap() in which we do not save anything
      77                 :             :  * about the current thread.
      78                 :             :  *
      79                 :             :  * Note that we will never come back to this thread: posix_main_thread_start()
      80                 :             :  * does never return.
      81                 :             :  */
      82                 :           1 : void arch_switch_to_main_thread(struct k_thread *main_thread, char *stack_ptr,
      83                 :             :                                 k_thread_entry_t _main)
      84                 :             : {
      85                 :           1 :         ARG_UNUSED(stack_ptr);
      86                 :           1 :         ARG_UNUSED(_main);
      87                 :             : 
      88                 :           1 :         posix_thread_status_t *ready_thread_ptr =
      89                 :             :                         (posix_thread_status_t *)
      90                 :           1 :                         _kernel.ready_q.cache->callee_saved.thread_status;
      91                 :             : 
      92                 :             : #ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
      93                 :             :         z_thread_mark_switched_out();
      94                 :             : #endif
      95                 :             : 
      96                 :           1 :         z_current_thread_set(_kernel.ready_q.cache);
      97                 :             : 
      98                 :             : #ifdef CONFIG_INSTRUMENT_THREAD_SWITCHING
      99                 :             :         z_thread_mark_switched_in();
     100                 :             : #endif
     101                 :             : 
     102                 :           1 :         posix_main_thread_start(ready_thread_ptr->thread_idx);
     103                 :             : } /* LCOV_EXCL_LINE */
     104                 :             : #endif
     105                 :             : 
     106                 :             : #ifdef CONFIG_PM
     107                 :             : /**
     108                 :             :  * If the kernel is in idle mode, take it out
     109                 :             :  */
     110                 :             : void posix_irq_check_idle_exit(void)
     111                 :             : {
     112                 :             :         if (_kernel.idle) {
     113                 :             :                 _kernel.idle = 0;
     114                 :             :                 pm_system_resume();
     115                 :             :         }
     116                 :             : }
     117                 :             : #endif
        

Generated by: LCOV version 2.0-1