| 1 | /* | 
|---|
| 2 |  * scheduler.h - Core scheduler definition. | 
|---|
| 3 |  *  | 
|---|
| 4 |  * Author    Alain Greiner (2016,2017,2018,2019,2020) | 
|---|
| 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_kernel_types.h> | 
|---|
| 28 | #include <list.h> | 
|---|
| 29 | #include <busylock.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 |     busylock_t        lock;            /*! lock protecting scheduler state                  */ | 
|---|
| 43 |     uint32_t          u_threads_nr;    /*! total number of attached user threads            */ | 
|---|
| 44 |     uint32_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 |     volatile bool_t   req_ack_pending; /*! sequencialize ack requests when true             */ | 
|---|
| 52 |     bool_t            trace;           /*! context switches trace activated if true         */ | 
|---|
| 53 | } | 
|---|
| 54 | scheduler_t; | 
|---|
| 55 |  | 
|---|
| 56 | /********************************************************************************************* | 
|---|
| 57 |  *  This function initialises the scheduler for a given core. | 
|---|
| 58 |  ********************************************************************************************/   | 
|---|
| 59 | void sched_init( struct core_s * core ); | 
|---|
| 60 |  | 
|---|
| 61 | /********************************************************************************************* | 
|---|
| 62 |  * This function atomically register a new thread in a given core scheduler. | 
|---|
| 63 |  * Note: There is no specific sched_remove_thread(), as a thread is always deleted | 
|---|
| 64 |  * by the ched_handle_signals() function, called by the sched_yield() function. | 
|---|
| 65 |  ********************************************************************************************* | 
|---|
| 66 |  * @ core    : local pointer on the core descriptor. | 
|---|
| 67 |  * @ thread  : local pointer on the thread descriptor. | 
|---|
| 68 |  ********************************************************************************************/   | 
|---|
| 69 | void sched_register_thread( struct core_s   * core, | 
|---|
| 70 |                             struct thread_s * thread ); | 
|---|
| 71 |  | 
|---|
| 72 | /*********************************************************************************************  | 
|---|
| 73 |  * This function is the only method to make a context switch. It is called in cas of TICK, | 
|---|
| 74 |  * or when a thread explicitely requires to be descheduled. | 
|---|
| 75 |  * It takes the scheduler busylock to atomically update the scheduled state. | 
|---|
| 76 |  * It calls the sched_select() private function to select a new thread. After switch, it  | 
|---|
| 77 |  * calls the sched_handle_signals() private function to handle the pending REQ_ACK and  | 
|---|
| 78 |  * REQ_DELETE flagss for all threads attached to the scheduler: it deletes all threads  | 
|---|
| 79 |  * marked for delete (and the process descriptor when the deleted thread is the main thread). | 
|---|
| 80 |  * As the REQ_DELETE flag can be asynchronously set (between the select and the handle), | 
|---|
| 81 |  * the sched_handle-signals() function check that the thread to delete is not the new thread, | 
|---|
| 82 |  * because a thread cannot delete itself. | 
|---|
| 83 |  * The cause argument is only used for debug by the sched_display() functions, and indicates | 
|---|
| 84 |  * the scheduling cause. | 
|---|
| 85 |  ********************************************************************************************* | 
|---|
| 86 |  * @ cause    : character string defining the scheduling cause. | 
|---|
| 87 |  ********************************************************************************************/ | 
|---|
| 88 | void sched_yield( const char * cause ); | 
|---|
| 89 |  | 
|---|
| 90 | /*********************************************************************************************  | 
|---|
| 91 |  * This debug function displays on TXT0 the internal state of a scheduler,  | 
|---|
| 92 |  * identified by the target cluster identifier <cxy> and the core local index <lid>. | 
|---|
| 93 |  * It can be called by a thread running in any cluster, as it uses remote accesses, | 
|---|
| 94 |  * to scan the scheduler lists of threads. | 
|---|
| 95 |  ********************************************************************************************* | 
|---|
| 96 |  * @ cxy      : target cluster identifier | 
|---|
| 97 |  * @ lid      : local index of target core. | 
|---|
| 98 |  ********************************************************************************************/ | 
|---|
| 99 | void sched_remote_display( cxy_t  cxy, | 
|---|
| 100 |                            lid_t  lid ); | 
|---|
| 101 |  | 
|---|
| 102 | #endif  /* _SCHEDULER_H_ */ | 
|---|