[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. |
---|
[279] | 38 | ********************************************************************************************/ |
---|
[1] | 39 | |
---|
| 40 | typedef struct scheduler_s |
---|
| 41 | { |
---|
[416] | 42 | spinlock_t lock; /*! readlock protecting lists of threads */ |
---|
| 43 | uint16_t u_threads_nr; /*! total number 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 */ |
---|
| 46 | list_entry_t k_root; /*! root of list of kernel threads */ |
---|
| 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 | bool_t req_ack_pending; /*! signal_handller must be called when true */ |
---|
[1] | 52 | } |
---|
| 53 | scheduler_t; |
---|
| 54 | |
---|
[279] | 55 | /********************************************************************************************* |
---|
[14] | 56 | * This function initialises the scheduler for a given core. |
---|
[279] | 57 | ********************************************************************************************/ |
---|
[1] | 58 | void sched_init( struct core_s * core ); |
---|
| 59 | |
---|
[279] | 60 | /********************************************************************************************* |
---|
[1] | 61 | * This function register a new thread in a given core scheduler. |
---|
[279] | 62 | ********************************************************************************************* |
---|
[1] | 63 | * @ core : local pointer on the core descriptor. |
---|
| 64 | * @ thread : local pointer on the thread descriptor. |
---|
[279] | 65 | ********************************************************************************************/ |
---|
[1] | 66 | void sched_register_thread( struct core_s * core, |
---|
| 67 | struct thread_s * thread ); |
---|
| 68 | |
---|
[279] | 69 | /********************************************************************************************* |
---|
[408] | 70 | * This function is the only method to make a context switch. It is called in cas of TICK, |
---|
| 71 | * or when when a thread explicitely requires a scheduling. |
---|
| 72 | * It handles the pending signals for all threads attached to the core running the calling |
---|
| 73 | * thread, and calls the sched_select() function to select a new thread. |
---|
| 74 | * The cause argument is only used for debug by the sched_display() function, and |
---|
| 75 | * indicates the scheduling cause. |
---|
| 76 | ********************************************************************************************* |
---|
| 77 | * @ cause : character string defining the scheduling cause. |
---|
[279] | 78 | ********************************************************************************************/ |
---|
[408] | 79 | void sched_yield( char * cause ); |
---|
[1] | 80 | |
---|
[279] | 81 | /********************************************************************************************* |
---|
[416] | 82 | * This function scan all threads attached to a given scheduler, and executes the relevant |
---|
| 83 | * actions for pending THREAD_FLAG_REQ_ACK or THREAD_FLAG_REQ_DELETE requests. |
---|
| 84 | * It is called in by the sched_yield() function, with IRQ disabled. |
---|
| 85 | * - REQ_ACK : it checks that target thread is blocked, decrements the response counter |
---|
| 86 | * to acknowledge the client thread, and reset the pending request. |
---|
| 87 | * - REQ_DELETE : it detach the target thread from parent if attached, detach it from |
---|
| 88 | * the process, remove it from scheduler, release memory allocated to thread descriptor, |
---|
| 89 | * and destroy the process descriptor it the target thread was the last thread. |
---|
[279] | 90 | ********************************************************************************************* |
---|
[1] | 91 | * @ core : local pointer on the core descriptor. |
---|
[279] | 92 | ********************************************************************************************/ |
---|
[416] | 93 | void sched_handle_requests( struct core_s * core ); |
---|
[1] | 94 | |
---|
[279] | 95 | /********************************************************************************************* |
---|
[1] | 96 | * This function does NOT modify the scheduler state. |
---|
[408] | 97 | * It just select a thread in the list of attached threads, implementing the following |
---|
| 98 | * three steps policy: |
---|
| 99 | * 1) It scan the list of kernel threads, from the next thread after the last executed one, |
---|
| 100 | * and returns the first runnable found : not IDLE, not blocked, client queue not empty. |
---|
| 101 | * It can be the current thread. |
---|
| 102 | * 2) If no kernel thread found, it scan the list of user thread, from the next thread after |
---|
| 103 | * the last executed one, and returns the first runable found : not blocked. |
---|
| 104 | * It can be the current thread. |
---|
| 105 | * 3) If no runable thread found, it returns the idle thread. |
---|
[279] | 106 | ********************************************************************************************* |
---|
[408] | 107 | * @ core : local pointer on scheduler. |
---|
[1] | 108 | * @ returns pointer on selected thread descriptor |
---|
[279] | 109 | ********************************************************************************************/ |
---|
[408] | 110 | struct thread_s * sched_select( struct scheduler_s * sched ); |
---|
[1] | 111 | |
---|
[279] | 112 | /********************************************************************************************* |
---|
[407] | 113 | * This function display the internal state of the local core identified by its <lid>. |
---|
| 114 | ********************************************************************************************* |
---|
| 115 | * @ lid : local index of target core. |
---|
[279] | 116 | ********************************************************************************************/ |
---|
[407] | 117 | void sched_display( lid_t lid ); |
---|
[1] | 118 | |
---|
| 119 | |
---|
| 120 | #endif /* _SCHEDULER_H_ */ |
---|