| 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 |  | 
|---|
| 36 | /********************************************************************************************* | 
|---|
| 37 | * This structure define the scheduler associated to a given core. | 
|---|
| 38 | ********************************************************************************************/ | 
|---|
| 39 |  | 
|---|
| 40 | typedef struct scheduler_s | 
|---|
| 41 | { | 
|---|
| 42 | spinlock_t        lock;         /*! readlock protecting lists of threads                */ | 
|---|
| 43 | uint16_t          u_threads_nr; /*! total numbre of attached user threads               */ | 
|---|
| 44 | uint16_t          k_threads_nr; /*! total number of attached kernel threads             */ | 
|---|
| 45 | list_entry_t      u_root;       /*! root of list of user threads for this scheduler     */ | 
|---|
| 46 | list_entry_t      k_root;       /*! root of list of kernel threads for this scheduler   */ | 
|---|
| 47 | list_entry_t    * u_last;       /*! pointer on list_entry for last executed k_thread    */ | 
|---|
| 48 | list_entry_t    * k_last;       /*! pointer on list entry for last executed u_thread    */ | 
|---|
| 49 | struct thread_s * idle;         /*! pointer on idle thread                              */ | 
|---|
| 50 | struct thread_s * current;      /*! pointer on current running thread                   */ | 
|---|
| 51 | } | 
|---|
| 52 | scheduler_t; | 
|---|
| 53 |  | 
|---|
| 54 | /********************************************************************************************* | 
|---|
| 55 | *  This function initialises the scheduler for a given core. | 
|---|
| 56 | ********************************************************************************************/ | 
|---|
| 57 | void sched_init( struct core_s * core ); | 
|---|
| 58 |  | 
|---|
| 59 | /********************************************************************************************* | 
|---|
| 60 | * This function register a new thread in a given core scheduler. | 
|---|
| 61 | ********************************************************************************************* | 
|---|
| 62 | * @ core    : local pointer on the core descriptor. | 
|---|
| 63 | * @ thread  : local pointer on the thread descriptor. | 
|---|
| 64 | ********************************************************************************************/ | 
|---|
| 65 | void sched_register_thread( struct core_s   * core, | 
|---|
| 66 | struct thread_s * thread ); | 
|---|
| 67 |  | 
|---|
| 68 | /********************************************************************************************* | 
|---|
| 69 | *  This function removes a thread from the set of threads attached to a given core. | 
|---|
| 70 | ********************************************************************************************* | 
|---|
| 71 | * @ thread  : local pointer on the thread descriptor. | 
|---|
| 72 | ********************************************************************************************/ | 
|---|
| 73 | void sched_remove_thread( struct thread_s * thread ); | 
|---|
| 74 |  | 
|---|
| 75 | /********************************************************************************************* | 
|---|
| 76 | * This function handles pending signals for all registered threads, and calls the | 
|---|
| 77 | * sched_select() function to make a context switch for the core running the calling thread. | 
|---|
| 78 | ********************************************************************************************/ | 
|---|
| 79 | void sched_yield(); | 
|---|
| 80 |  | 
|---|
| 81 | /********************************************************************************************* | 
|---|
| 82 | * This function scan all threads attached to a given core scheduler, and executes | 
|---|
| 83 | * the relevant actions for pending signals, such as the THREAD_SIG_KILL signal. | 
|---|
| 84 | ********************************************************************************************* | 
|---|
| 85 | * @ core    : local pointer on the core descriptor. | 
|---|
| 86 | ********************************************************************************************/ | 
|---|
| 87 | void sched_handle_signals( struct core_s * core ); | 
|---|
| 88 |  | 
|---|
| 89 | /********************************************************************************************* | 
|---|
| 90 | * This function is used by the scheduler of a given core to actually kill a thread that has | 
|---|
| 91 | * the SIG_KILL / SIG_SUICIDE signal set (following a thread_exit() or a thread_kill() event). | 
|---|
| 92 | * - It checks that the thread has released all locks => panic otherwise... | 
|---|
| 93 | * - It removes the thread from the scheduler. | 
|---|
| 94 | * - It reset the SIG_KILL signal to acknoledge the killer. | 
|---|
| 95 | * - In case of SIG_SUCIDE, it removes the detached thread from its process, and destroys it. | 
|---|
| 96 | ********************************************************************************************* | 
|---|
| 97 | * @ thread  : local pointer on the thread descriptor. | 
|---|
| 98 | ********************************************************************************************/ | 
|---|
| 99 | void sched_kill_thread( struct thread_s * thread ); | 
|---|
| 100 |  | 
|---|
| 101 | /********************************************************************************************* | 
|---|
| 102 | * This function does NOT modify the scheduler state. | 
|---|
| 103 | * It just select a thread in the list of attached threads, implementing the following policy: | 
|---|
| 104 | * 1) it scan the list of kernel threads, from the next thread after the last executed one, | 
|---|
| 105 | *    and returns the first runnable found (can be the current thread). | 
|---|
| 106 | * 2) if no kernel thread found, it scan the list of user thread, from the next thread after | 
|---|
| 107 | *    the last executed one, and returns the first runable found (can be the current thread). | 
|---|
| 108 | * 3) if no runable thread found, it returns the idle thread. | 
|---|
| 109 | ********************************************************************************************* | 
|---|
| 110 | * @ core    : local pointer on the core descriptor. | 
|---|
| 111 | * @ returns pointer on selected thread descriptor | 
|---|
| 112 | ********************************************************************************************/ | 
|---|
| 113 | struct thread_s * sched_select( struct core_s * core ); | 
|---|
| 114 |  | 
|---|
| 115 | /********************************************************************************************* | 
|---|
| 116 | * This function display the internal state of the local core identified by its <lid>. | 
|---|
| 117 | ********************************************************************************************* | 
|---|
| 118 | * @ lid      : local index of target core. | 
|---|
| 119 | ********************************************************************************************/ | 
|---|
| 120 | void sched_display( lid_t lid ); | 
|---|
| 121 |  | 
|---|
| 122 |  | 
|---|
| 123 | #endif  /* _SCHEDULER_H_ */ | 
|---|