[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 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[683] | 33 | // This static function registers the alarm identified by the <alarm> & <cxy> arguments |
---|
[669] | 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 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[683] | 38 | // @ cxy : cluster containing both the new alarm and the core. |
---|
| 39 | // @ alarm : local pointer on the alarm. |
---|
| 40 | // @ core : local pointer on the core. |
---|
[669] | 41 | //////////////////////////////////////////////////////////////////////////////////////////// |
---|
[683] | 42 | static void alarm_register( cxy_t cxy, |
---|
| 43 | alarm_t * alarm, |
---|
[669] | 44 | core_t * core ) |
---|
[23] | 45 | { |
---|
[683] | 46 | // get alarm date |
---|
| 47 | cycle_t new_date = hal_remote_l64( XPTR( cxy , &alarm->date ) ); |
---|
[23] | 48 | |
---|
[683] | 49 | // build local pointer on root of alarms list |
---|
[669] | 50 | list_entry_t * root = &core->alarms_root; |
---|
[23] | 51 | |
---|
[683] | 52 | // build local pointer on new alarm list_entry |
---|
| 53 | list_entry_t * new = &alarm->list; |
---|
[23] | 54 | |
---|
[669] | 55 | // insert new alarm to respect dates order |
---|
[683] | 56 | if( list_remote_is_empty( cxy , &core->alarms_root ) ) // list empty |
---|
[669] | 57 | { |
---|
[683] | 58 | list_remote_add_first( cxy , root , new ); |
---|
[669] | 59 | } |
---|
[683] | 60 | else // list non empty |
---|
[669] | 61 | { |
---|
[683] | 62 | list_entry_t * iter; // local pointer on current list_entry in existing list |
---|
| 63 | alarm_t * iter_alarm; // local pointer on current alarm in existing list |
---|
| 64 | cycle_t iter_date; // date of current alarm in existing list |
---|
| 65 | bool_t done = false; |
---|
| 66 | |
---|
| 67 | for( iter = hal_remote_lpt( XPTR( cxy , &root->next ) ) ; |
---|
| 68 | (iter != root) && (done == false) ; |
---|
| 69 | iter = hal_remote_lpt( XPTR( cxy , &iter->next ) ) ) |
---|
[669] | 70 | { |
---|
[683] | 71 | // get local pointer on pred and next for iter |
---|
| 72 | list_entry_t * prev = hal_remote_lpt( XPTR( cxy , &iter->pred ) ); |
---|
[23] | 73 | |
---|
[683] | 74 | // get local pointer on current alarm |
---|
| 75 | iter_alarm = LIST_ELEMENT( iter , alarm_t , list ); |
---|
[23] | 76 | |
---|
[669] | 77 | // get date for current alarm |
---|
[683] | 78 | iter_date = hal_remote_l64( XPTR( cxy , &iter_alarm->date ) ); |
---|
[23] | 79 | |
---|
[683] | 80 | // insert new alarm just before current when required |
---|
| 81 | if( iter_date > new_date ) |
---|
[669] | 82 | { |
---|
[683] | 83 | hal_remote_spt( XPTR( cxy , &new->next ) , iter ); |
---|
| 84 | hal_remote_spt( XPTR( cxy , &new->pred ) , prev ); |
---|
[23] | 85 | |
---|
[683] | 86 | hal_remote_spt( XPTR( cxy , &iter->pred ) , new ); |
---|
| 87 | hal_remote_spt( XPTR( cxy , &prev->next ) , new ); |
---|
[669] | 88 | |
---|
| 89 | done = true; |
---|
| 90 | } |
---|
| 91 | } // end for |
---|
| 92 | |
---|
| 93 | if( done == false ) // new_date is larger than all registered dates |
---|
| 94 | { |
---|
[683] | 95 | list_remote_add_last( cxy, root , new ); |
---|
[669] | 96 | } |
---|
| 97 | } |
---|
[683] | 98 | } // end alarm_register() |
---|
| 99 | |
---|
[23] | 100 | |
---|
[683] | 101 | /////////////////////////////////// |
---|
| 102 | void alarm_init( alarm_t * alarm ) |
---|
| 103 | { |
---|
| 104 | alarm->linked = false; |
---|
| 105 | list_entry_init( &alarm->list ); |
---|
| 106 | } |
---|
[23] | 107 | |
---|
[683] | 108 | /////////////////////////////////////// |
---|
| 109 | void alarm_start( xptr_t thread_xp, |
---|
| 110 | cycle_t date, |
---|
[669] | 111 | void * func_ptr, |
---|
[683] | 112 | xptr_t args_xp ) |
---|
[23] | 113 | { |
---|
[683] | 114 | // get cluster and local pointer on target thread |
---|
| 115 | thread_t * tgt_ptr = GET_PTR( thread_xp ); |
---|
| 116 | cxy_t tgt_cxy = GET_CXY( thread_xp ); |
---|
[23] | 117 | |
---|
[683] | 118 | // check alarm state |
---|
| 119 | assert( __FUNCTION__ , (hal_remote_l32( XPTR(tgt_cxy,&tgt_ptr->alarm.linked)) == false ), |
---|
| 120 | "alarm already started"); |
---|
| 121 | |
---|
| 122 | // get local pointer on core running target thread |
---|
| 123 | core_t * core = hal_remote_lpt( XPTR( tgt_cxy , &tgt_ptr->core ) ); |
---|
| 124 | |
---|
| 125 | // build extended pointer on lock protecting alarms list |
---|
| 126 | xptr_t lock_xp = XPTR( tgt_cxy , &core->alarms_lock ); |
---|
| 127 | |
---|
[669] | 128 | // initialize alarm descriptor |
---|
[683] | 129 | hal_remote_s64( XPTR( tgt_cxy , &tgt_ptr->alarm.date ) , date ); |
---|
| 130 | hal_remote_spt( XPTR( tgt_cxy , &tgt_ptr->alarm.func_ptr ) , func_ptr ); |
---|
| 131 | hal_remote_s64( XPTR( tgt_cxy , &tgt_ptr->alarm.args_xp ) , args_xp ); |
---|
| 132 | hal_remote_s32( XPTR( tgt_cxy , &tgt_ptr->alarm.linked ) , true ); |
---|
| 133 | |
---|
| 134 | // take the lock |
---|
| 135 | remote_busylock_acquire( lock_xp ); |
---|
| 136 | |
---|
[669] | 137 | // register alarm in core list |
---|
[683] | 138 | alarm_register( tgt_cxy , &tgt_ptr->alarm , core ); |
---|
[23] | 139 | |
---|
[683] | 140 | //release the lock |
---|
| 141 | remote_busylock_release( lock_xp ); |
---|
| 142 | |
---|
[669] | 143 | } // end alarm_start() |
---|
[23] | 144 | |
---|
[683] | 145 | |
---|
[669] | 146 | ///////////////////////////////////// |
---|
[683] | 147 | void alarm_stop( xptr_t thread_xp ) |
---|
[669] | 148 | { |
---|
[683] | 149 | // get cluster and local pointer on target thread |
---|
| 150 | thread_t * tgt_ptr = GET_PTR( thread_xp ); |
---|
| 151 | cxy_t tgt_cxy = GET_CXY( thread_xp ); |
---|
[23] | 152 | |
---|
[683] | 153 | // get local pointer on core running target thread |
---|
| 154 | core_t * core = hal_remote_lpt( XPTR( tgt_cxy , &tgt_ptr->core ) ); |
---|
[23] | 155 | |
---|
[683] | 156 | // build extended pointer on lock protecting alarms list |
---|
| 157 | xptr_t lock_xp = XPTR( tgt_cxy , &core->alarms_lock ); |
---|
| 158 | |
---|
| 159 | // take the lock |
---|
| 160 | remote_busylock_acquire( lock_xp ); |
---|
[23] | 161 | |
---|
[683] | 162 | // unlink the alarm from the list rooted in core |
---|
| 163 | list_remote_unlink( tgt_cxy , &tgt_ptr->alarm.list ); |
---|
[23] | 164 | |
---|
[683] | 165 | // update alarm state |
---|
| 166 | hal_remote_s32( XPTR( tgt_cxy , &tgt_ptr->alarm.linked ) , false ); |
---|
[23] | 167 | |
---|
[683] | 168 | //release the lock |
---|
| 169 | remote_busylock_release( lock_xp ); |
---|
[23] | 170 | |
---|
[683] | 171 | } // end alarm_stop() |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | ////////////////////////////////////// |
---|
| 175 | void alarm_update( xptr_t thread_xp, |
---|
| 176 | cycle_t new_date ) |
---|
[23] | 177 | { |
---|
[683] | 178 | // get cluster and local pointer on target thread |
---|
| 179 | thread_t * tgt_ptr = GET_PTR( thread_xp ); |
---|
| 180 | cxy_t tgt_cxy = GET_CXY( thread_xp ); |
---|
[23] | 181 | |
---|
[683] | 182 | // get local pointer on core running target thread |
---|
| 183 | core_t * core = hal_remote_lpt( XPTR( tgt_cxy , &tgt_ptr->core ) ); |
---|
[23] | 184 | |
---|
[683] | 185 | // build extended pointer on lock protecting alarms list |
---|
| 186 | xptr_t lock_xp = XPTR( tgt_cxy , &core->alarms_lock ); |
---|
| 187 | |
---|
| 188 | // take the lock |
---|
| 189 | remote_busylock_acquire( lock_xp ); |
---|
[23] | 190 | |
---|
[683] | 191 | // unlink the alarm from the core list |
---|
| 192 | list_remote_unlink( tgt_cxy , &tgt_ptr->alarm.list ); |
---|
[23] | 193 | |
---|
[683] | 194 | // update the alarm date and state |
---|
| 195 | hal_remote_s64( XPTR( tgt_cxy , &tgt_ptr->alarm.date ) , new_date ); |
---|
| 196 | hal_remote_s32( XPTR( tgt_cxy , &tgt_ptr->alarm.linked ) , true ); |
---|
[669] | 197 | |
---|
[683] | 198 | // register alarm in core list |
---|
| 199 | alarm_register( tgt_cxy , &tgt_ptr->alarm , core ); |
---|
| 200 | |
---|
| 201 | // release the lock |
---|
| 202 | remote_busylock_release( lock_xp ); |
---|
| 203 | |
---|
| 204 | } // end alarm_update() |
---|
| 205 | |
---|
| 206 | |
---|