[1] | 1 | /* |
---|
| 2 | * scheduler.h - Core scheduler definition. |
---|
| 3 | * |
---|
| 4 | * Author Alain Greiner (2016) |
---|
| 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #ifndef _SCHEDULER_H_ |
---|
| 25 | #define _SCHEDULER_H_ |
---|
| 26 | |
---|
| 27 | #include <hal_types.h> |
---|
| 28 | #include <list.h> |
---|
| 29 | #include <spinlock.h> |
---|
| 30 | |
---|
| 31 | /**** Forward declarations ****/ |
---|
| 32 | |
---|
| 33 | struct core_s; |
---|
| 34 | struct thread_s; |
---|
| 35 | |
---|
[279] | 36 | /********************************************************************************************* |
---|
[1] | 37 | * This structure define the scheduler associated to a given core. |
---|
| 38 | * WARNING : the idle thread is executed when there is no runable thread in the list |
---|
| 39 | * of attached threads, but is NOT part of the list of attached threads. |
---|
[279] | 40 | ********************************************************************************************/ |
---|
[1] | 41 | |
---|
| 42 | typedef struct scheduler_s |
---|
| 43 | { |
---|
[279] | 44 | spinlock_t lock; /*! readlock protecting lists of threads */ |
---|
| 45 | uint16_t u_threads_nr; /*! total numbre of attached user threads */ |
---|
| 46 | uint16_t k_threads_nr; /*! total number of attached kernel threads */ |
---|
| 47 | list_entry_t u_root; /*! root of list of user threads for this scheduler */ |
---|
| 48 | list_entry_t k_root; /*! root of list of kernel threads for this scheduler */ |
---|
| 49 | list_entry_t * u_last; /*! pointer on list_entry for last executed k_thread */ |
---|
| 50 | list_entry_t * k_last; /*! pointer on list entry for last executed u_thread */ |
---|
| 51 | struct thread_s * idle; /*! pointer on idle thread */ |
---|
| 52 | struct thread_s * current; /*! pointer on current running thread */ |
---|
[1] | 53 | } |
---|
| 54 | scheduler_t; |
---|
| 55 | |
---|
[279] | 56 | /********************************************************************************************* |
---|
[14] | 57 | * This function initialises the scheduler for a given core. |
---|
[279] | 58 | ********************************************************************************************/ |
---|
[1] | 59 | void sched_init( struct core_s * core ); |
---|
| 60 | |
---|
[279] | 61 | /********************************************************************************************* |
---|
[1] | 62 | * This function register a new thread in a given core scheduler. |
---|
[279] | 63 | ********************************************************************************************* |
---|
[1] | 64 | * @ core : local pointer on the core descriptor. |
---|
| 65 | * @ thread : local pointer on the thread descriptor. |
---|
[279] | 66 | ********************************************************************************************/ |
---|
[1] | 67 | void sched_register_thread( struct core_s * core, |
---|
| 68 | struct thread_s * thread ); |
---|
| 69 | |
---|
[279] | 70 | /********************************************************************************************* |
---|
[1] | 71 | * This function removes a thread from the set of threads attached to a given core. |
---|
[279] | 72 | ********************************************************************************************* |
---|
[1] | 73 | * @ thread : local pointer on the thread descriptor. |
---|
[279] | 74 | ********************************************************************************************/ |
---|
[1] | 75 | void sched_remove_thread( struct thread_s * thread ); |
---|
| 76 | |
---|
[279] | 77 | /********************************************************************************************* |
---|
[1] | 78 | * This function handles pending signals for all registered threads, and tries to make |
---|
| 79 | * a context switch for the core running the calling thread. |
---|
| 80 | * - If there is a runable thread (other than the current thread or the idle thread), |
---|
| 81 | * the calling thread is descheduled, but its state is not modified. |
---|
| 82 | * - If there is no other runable thread, the calling thread continues execution. |
---|
| 83 | * - If there is no runable thread, the idle thread is executed. |
---|
[279] | 84 | ********************************************************************************************/ |
---|
[1] | 85 | void sched_yield(); |
---|
| 86 | |
---|
[279] | 87 | /********************************************************************************************* |
---|
[1] | 88 | * This function handles pending signals for all registered threads, and make |
---|
| 89 | * a context switch to the thread defined by the <thread> argument. |
---|
| 90 | * If the selected thread is not attached to the same core as the calling thread, |
---|
| 91 | * or is blocked, it causes a kernel panic. |
---|
[279] | 92 | ********************************************************************************************* |
---|
[1] | 93 | * @ new : local pointer on the thread to run. |
---|
[279] | 94 | ********************************************************************************************/ |
---|
[1] | 95 | void sched_switch_to( struct thread_s * new ); |
---|
| 96 | |
---|
[279] | 97 | /********************************************************************************************* |
---|
[1] | 98 | * This function scan all threads attached to a given core scheduler, and executes |
---|
| 99 | * the relevant actions for pending signals, such as the THREAD_SIG_KILL signal. |
---|
[279] | 100 | ********************************************************************************************* |
---|
[1] | 101 | * @ core : local pointer on the core descriptor. |
---|
[279] | 102 | ********************************************************************************************/ |
---|
[1] | 103 | void sched_handle_signals( struct core_s * core ); |
---|
| 104 | |
---|
[279] | 105 | /********************************************************************************************* |
---|
[1] | 106 | * This function is used by the scheduler of a given core to actually kill a thread that has |
---|
| 107 | * the SIG_KILL signal set (following a thread_exit() or a thread_kill() event). |
---|
| 108 | * - It checks that the thread has released all locks => panic otherwise... |
---|
| 109 | * - It detach the thread from the local process descriptor. |
---|
| 110 | * - It removes the thread from the scheduler. |
---|
| 111 | * - It release physical memory allocated for thread descriptor. |
---|
[279] | 112 | ********************************************************************************************* |
---|
[1] | 113 | * @ thread : local pointer on the thread descriptor. |
---|
[279] | 114 | ********************************************************************************************/ |
---|
[1] | 115 | void sched_kill_thread( struct thread_s * thread ); |
---|
| 116 | |
---|
[279] | 117 | /********************************************************************************************* |
---|
[1] | 118 | * This function does NOT modify the scheduler state. |
---|
| 119 | * It just select a thread in the list of attached threads, implementing the following policy: |
---|
| 120 | * 1) it scan the list of kernel threads, from the next thread after the last executed one, |
---|
| 121 | * and returns the first runnable found (can be the current thread). |
---|
| 122 | * 2) if no kernel thread found, it scan the list of user thread, from the next thread after |
---|
| 123 | * the last executed one, and returns the first runable found (can be the current thread). |
---|
| 124 | * 3) if no runable thread found, it returns the idle thread. |
---|
[279] | 125 | ********************************************************************************************* |
---|
[1] | 126 | * @ core : local pointer on the core descriptor. |
---|
| 127 | * @ returns pointer on selected thread descriptor |
---|
[279] | 128 | ********************************************************************************************/ |
---|
[1] | 129 | struct thread_s * sched_select( struct core_s * core ); |
---|
| 130 | |
---|
[279] | 131 | /********************************************************************************************* |
---|
[1] | 132 | * This function scan the list of kernel threads to find an idle (blocked) RPC thread. |
---|
[279] | 133 | ********************************************************************************************* |
---|
[1] | 134 | * @ core : local pointer on the core descriptor. |
---|
| 135 | * @ returns pointer on RPC thread descriptor / returns NULL if no idle RPC thread. |
---|
[279] | 136 | ********************************************************************************************/ |
---|
[1] | 137 | struct thread_s * sched_get_rpc_thead( struct core_s * core ); |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | #endif /* _SCHEDULER_H_ */ |
---|