[23] | 1 | /* |
---|
[669] | 2 | * alarm.c - timer based kernel alarm implementation |
---|
[23] | 3 | * |
---|
[669] | 4 | * Author Alain Greiner (2016,2017,2018,2019,2020) |
---|
[23] | 5 | * |
---|
[669] | 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 |
---|
[23] | 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 | * |
---|
[669] | 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[23] | 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 |
---|
[669] | 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[23] | 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[669] | 24 | #include <kernel_config.h> |
---|
| 25 | #include <hal_kernel_types.h> |
---|
| 26 | #include <printk.h> |
---|
| 27 | #include <list.h> |
---|
[23] | 28 | #include <thread.h> |
---|
[669] | 29 | #include <core.h> |
---|
| 30 | #include <alarm.h> |
---|
[23] | 31 | |
---|
[669] | 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 ) |
---|
[23] | 43 | { |
---|
[669] | 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 |
---|
[23] | 48 | |
---|
[669] | 49 | bool_t done = false; |
---|
[23] | 50 | |
---|
[669] | 51 | // get pointers on root of alarms and lock |
---|
| 52 | list_entry_t * root = &core->alarms_root; |
---|
| 53 | busylock_t * lock = &core->alarms_lock; |
---|
[23] | 54 | |
---|
[669] | 55 | // get pointer on new_alarm list_entry |
---|
| 56 | list_entry_t * new_entry = &new_alarm->list; |
---|
[23] | 57 | |
---|
[669] | 58 | // get new_alarm date |
---|
| 59 | cycle_t new_date = new_alarm->date; |
---|
[23] | 60 | |
---|
[669] | 61 | // take the lock |
---|
| 62 | busylock_acquire( lock ); |
---|
[23] | 63 | |
---|
[669] | 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; |
---|
[23] | 77 | |
---|
[669] | 78 | // get pointer on current alarm |
---|
| 79 | current_alarm = LIST_ELEMENT( current , alarm_t , list ); |
---|
[23] | 80 | |
---|
[669] | 81 | // get date for current alarm |
---|
| 82 | current_date = current_alarm->date; |
---|
[23] | 83 | |
---|
[669] | 84 | if( current_date > new_date ) // insert new alarm just before current |
---|
| 85 | { |
---|
| 86 | new_entry->next = current; |
---|
| 87 | new_entry->pred = previous; |
---|
[23] | 88 | |
---|
[669] | 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 | } |
---|
[23] | 101 | |
---|
[669] | 102 | // release the lock |
---|
| 103 | busylock_release( lock ); |
---|
[23] | 104 | |
---|
[669] | 105 | } // end alarm_register() |
---|
[23] | 106 | |
---|
[669] | 107 | ////////////////////////////////////// |
---|
| 108 | void alarm_start( cycle_t date, |
---|
| 109 | void * func_ptr, |
---|
| 110 | xptr_t args_xp, |
---|
| 111 | thread_t * thread ) |
---|
[23] | 112 | { |
---|
[669] | 113 | // get pointer on alarm |
---|
| 114 | alarm_t * alarm = &thread->alarm; |
---|
[23] | 115 | |
---|
[669] | 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 ); |
---|
[23] | 123 | |
---|
[669] | 124 | } // end alarm_start() |
---|
[23] | 125 | |
---|
[669] | 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; |
---|
[23] | 132 | |
---|
[669] | 133 | // get pointer on core |
---|
| 134 | core_t * core = thread->core; |
---|
[23] | 135 | |
---|
[669] | 136 | // get pointer on lock protecting the alarms list |
---|
| 137 | busylock_t * lock = &core->alarms_lock; |
---|
[23] | 138 | |
---|
[669] | 139 | // unlink the alarm from the core list |
---|
| 140 | busylock_acquire( lock ); |
---|
| 141 | list_unlink( &alarm->list ); |
---|
| 142 | busylock_release( lock ); |
---|
[23] | 143 | |
---|
[669] | 144 | // update the alarm date |
---|
| 145 | alarm->date = new_date; |
---|
[23] | 146 | |
---|
[669] | 147 | // register alarm in core list |
---|
| 148 | alarm_register( alarm , core ); |
---|
| 149 | |
---|
| 150 | } // end alarm_update() |
---|
[23] | 151 | |
---|
[669] | 152 | //////////////////////////////////// |
---|
| 153 | void alarm_stop( thread_t * thread ) |
---|
[23] | 154 | { |
---|
[669] | 155 | // get pointer on alarm |
---|
| 156 | alarm_t * alarm = &thread->alarm; |
---|
[23] | 157 | |
---|
[669] | 158 | // get pointer on core |
---|
| 159 | core_t * core = thread->core; |
---|
[23] | 160 | |
---|
[669] | 161 | // get pointer on lock protecting the alarms list |
---|
| 162 | busylock_t * lock = &core->alarms_lock; |
---|
[23] | 163 | |
---|
[669] | 164 | // unlink the alarm from the list rooted in core |
---|
| 165 | busylock_acquire( lock ); |
---|
| 166 | list_unlink( &alarm->list ); |
---|
| 167 | busylock_release( lock ); |
---|
[23] | 168 | |
---|
[669] | 169 | } // end alarm_stop() |
---|
| 170 | |
---|