| 1 | /* |
|---|
| 2 | * alarm.c - timer based kernel alarm implementation |
|---|
| 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 | #include <kernel_config.h> |
|---|
| 25 | #include <hal_kernel_types.h> |
|---|
| 26 | #include <printk.h> |
|---|
| 27 | #include <list.h> |
|---|
| 28 | #include <thread.h> |
|---|
| 29 | #include <core.h> |
|---|
| 30 | #include <alarm.h> |
|---|
| 31 | |
|---|
| 32 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 33 | // This static function registers the alarm identified ny the <new_alarm> argument |
|---|
| 34 | // in the list of alarms rooted in the core identified by the <core> argument. |
|---|
| 35 | // When the existing list of alarms is not empty, it scan the list to insert the new |
|---|
| 36 | // alarm in the right place to respect the absolute dates ordering. |
|---|
| 37 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 38 | // @ new_alarm : local pointer on the new alarm. |
|---|
| 39 | // @ core : local pointer on the target core. |
|---|
| 40 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 41 | static void alarm_register( alarm_t * new_alarm, |
|---|
| 42 | core_t * core ) |
|---|
| 43 | { |
|---|
| 44 | list_entry_t * current; // pointer on current list_entry in existing list |
|---|
| 45 | list_entry_t * previous; // pointer on previous list_entry in existing list |
|---|
| 46 | alarm_t * current_alarm; // pointer on current alarm in existing list |
|---|
| 47 | cycle_t current_date; // date of current alarm in existing list |
|---|
| 48 | |
|---|
| 49 | bool_t done = false; |
|---|
| 50 | |
|---|
| 51 | // get pointers on root of alarms and lock |
|---|
| 52 | list_entry_t * root = &core->alarms_root; |
|---|
| 53 | busylock_t * lock = &core->alarms_lock; |
|---|
| 54 | |
|---|
| 55 | // get pointer on new_alarm list_entry |
|---|
| 56 | list_entry_t * new_entry = &new_alarm->list; |
|---|
| 57 | |
|---|
| 58 | // get new_alarm date |
|---|
| 59 | cycle_t new_date = new_alarm->date; |
|---|
| 60 | |
|---|
| 61 | // take the lock |
|---|
| 62 | busylock_acquire( lock ); |
|---|
| 63 | |
|---|
| 64 | // insert new alarm to respect dates order |
|---|
| 65 | if( list_is_empty( root ) ) // list empty |
|---|
| 66 | { |
|---|
| 67 | list_add_first( root , new_entry ); |
|---|
| 68 | } |
|---|
| 69 | else // list non empty |
|---|
| 70 | { |
|---|
| 71 | for( current = root->next ; |
|---|
| 72 | (current != root) && (done == false) ; |
|---|
| 73 | current = current->next ) |
|---|
| 74 | { |
|---|
| 75 | // get pointer on previous entry in existing list |
|---|
| 76 | previous = current->pred; |
|---|
| 77 | |
|---|
| 78 | // get pointer on current alarm |
|---|
| 79 | current_alarm = LIST_ELEMENT( current , alarm_t , list ); |
|---|
| 80 | |
|---|
| 81 | // get date for current alarm |
|---|
| 82 | current_date = current_alarm->date; |
|---|
| 83 | |
|---|
| 84 | if( current_date > new_date ) // insert new alarm just before current |
|---|
| 85 | { |
|---|
| 86 | new_entry->next = current; |
|---|
| 87 | new_entry->pred = previous; |
|---|
| 88 | |
|---|
| 89 | current->pred = new_entry; |
|---|
| 90 | previous->next = new_entry; |
|---|
| 91 | |
|---|
| 92 | done = true; |
|---|
| 93 | } |
|---|
| 94 | } // end for |
|---|
| 95 | |
|---|
| 96 | if( done == false ) // new_date is larger than all registered dates |
|---|
| 97 | { |
|---|
| 98 | list_add_last( root , new_entry ); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | // release the lock |
|---|
| 103 | busylock_release( lock ); |
|---|
| 104 | |
|---|
| 105 | } // end alarm_register() |
|---|
| 106 | |
|---|
| 107 | ////////////////////////////////////// |
|---|
| 108 | void alarm_start( cycle_t date, |
|---|
| 109 | void * func_ptr, |
|---|
| 110 | xptr_t args_xp, |
|---|
| 111 | thread_t * thread ) |
|---|
| 112 | { |
|---|
| 113 | // get pointer on alarm |
|---|
| 114 | alarm_t * alarm = &thread->alarm; |
|---|
| 115 | |
|---|
| 116 | // initialize alarm descriptor |
|---|
| 117 | alarm->date = date; |
|---|
| 118 | alarm->func_ptr = func_ptr; |
|---|
| 119 | alarm->args_xp = args_xp; |
|---|
| 120 | |
|---|
| 121 | // register alarm in core list |
|---|
| 122 | alarm_register( alarm , thread->core ); |
|---|
| 123 | |
|---|
| 124 | } // end alarm_start() |
|---|
| 125 | |
|---|
| 126 | ///////////////////////////////////// |
|---|
| 127 | void alarm_update( thread_t * thread, |
|---|
| 128 | cycle_t new_date ) |
|---|
| 129 | { |
|---|
| 130 | // get pointer on alarm |
|---|
| 131 | alarm_t * alarm = &thread->alarm; |
|---|
| 132 | |
|---|
| 133 | // get pointer on core |
|---|
| 134 | core_t * core = thread->core; |
|---|
| 135 | |
|---|
| 136 | // get pointer on lock protecting the alarms list |
|---|
| 137 | busylock_t * lock = &core->alarms_lock; |
|---|
| 138 | |
|---|
| 139 | // unlink the alarm from the core list |
|---|
| 140 | busylock_acquire( lock ); |
|---|
| 141 | list_unlink( &alarm->list ); |
|---|
| 142 | busylock_release( lock ); |
|---|
| 143 | |
|---|
| 144 | // update the alarm date |
|---|
| 145 | alarm->date = new_date; |
|---|
| 146 | |
|---|
| 147 | // register alarm in core list |
|---|
| 148 | alarm_register( alarm , core ); |
|---|
| 149 | |
|---|
| 150 | } // end alarm_update() |
|---|
| 151 | |
|---|
| 152 | //////////////////////////////////// |
|---|
| 153 | void alarm_stop( thread_t * thread ) |
|---|
| 154 | { |
|---|
| 155 | // get pointer on alarm |
|---|
| 156 | alarm_t * alarm = &thread->alarm; |
|---|
| 157 | |
|---|
| 158 | // get pointer on core |
|---|
| 159 | core_t * core = thread->core; |
|---|
| 160 | |
|---|
| 161 | // get pointer on lock protecting the alarms list |
|---|
| 162 | busylock_t * lock = &core->alarms_lock; |
|---|
| 163 | |
|---|
| 164 | // unlink the alarm from the list rooted in core |
|---|
| 165 | busylock_acquire( lock ); |
|---|
| 166 | list_unlink( &alarm->list ); |
|---|
| 167 | busylock_release( lock ); |
|---|
| 168 | |
|---|
| 169 | } // end alarm_stop() |
|---|
| 170 | |
|---|