source: trunk/kernel/kern/alarm.c @ 687

Last change on this file since 687 was 683, checked in by alain, 4 years ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

File size: 7.2 KB
Line 
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 by the <alarm> & <cxy> arguments
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// @ cxy    : cluster containing both the new alarm and the core.
39// @ alarm  : local pointer on the alarm.
40// @ core   : local pointer on the core.
41////////////////////////////////////////////////////////////////////////////////////////////
42static void alarm_register( cxy_t     cxy,
43                            alarm_t * alarm,
44                            core_t  * core )
45{
46    // get alarm date
47    cycle_t new_date = hal_remote_l64( XPTR( cxy , &alarm->date ) );
48
49    // build local pointer on root of alarms list
50    list_entry_t * root = &core->alarms_root;
51
52    // build local pointer on new alarm list_entry
53    list_entry_t * new  = &alarm->list;
54
55    // insert new alarm to respect dates order
56    if( list_remote_is_empty( cxy , &core->alarms_root ) )  // list empty
57    {
58        list_remote_add_first( cxy , root , new ); 
59    }
60    else                                                   // list non empty
61    {
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 ) ) )
70        {
71            // get local pointer on pred and next for iter
72            list_entry_t * prev = hal_remote_lpt( XPTR( cxy , &iter->pred ) );
73
74            // get local pointer on current alarm
75            iter_alarm = LIST_ELEMENT( iter , alarm_t , list );
76
77            // get date for current alarm
78            iter_date = hal_remote_l64( XPTR( cxy , &iter_alarm->date ) );
79
80            // insert new alarm just before current when required
81            if( iter_date > new_date ) 
82            {
83                hal_remote_spt( XPTR( cxy , &new->next ) , iter );
84                hal_remote_spt( XPTR( cxy , &new->pred ) , prev );
85
86                hal_remote_spt( XPTR( cxy , &iter->pred ) , new );
87                hal_remote_spt( XPTR( cxy , &prev->next ) , new );
88               
89                done = true;
90            }
91        }  // end for
92       
93        if( done == false ) // new_date is larger than all registered dates
94        {
95            list_remote_add_last( cxy, root , new );
96        }
97    }
98}  // end alarm_register()
99           
100
101///////////////////////////////////
102void alarm_init( alarm_t *  alarm )
103{
104    alarm->linked   = false;
105    list_entry_init( &alarm->list );
106}
107
108///////////////////////////////////////
109void alarm_start( xptr_t     thread_xp,
110                  cycle_t    date,
111                  void     * func_ptr,
112                  xptr_t     args_xp )
113{
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 );
117
118// check alarm state
119assert( __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 
128    // initialize alarm descriptor
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
137    // register alarm in core list
138    alarm_register( tgt_cxy , &tgt_ptr->alarm , core );
139
140    //release the lock
141    remote_busylock_release( lock_xp );
142
143}  // end alarm_start()
144
145
146/////////////////////////////////////
147void alarm_stop( xptr_t   thread_xp )
148{
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 );
152
153    // get local pointer on core running target thread
154    core_t * core = hal_remote_lpt( XPTR( tgt_cxy , &tgt_ptr->core ) );
155
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 );
161
162    // unlink the alarm from the list rooted in core
163    list_remote_unlink( tgt_cxy , &tgt_ptr->alarm.list );
164
165    // update alarm state
166    hal_remote_s32( XPTR( tgt_cxy , &tgt_ptr->alarm.linked ) , false );
167
168    //release the lock
169    remote_busylock_release( lock_xp );
170
171}  // end alarm_stop()
172
173
174//////////////////////////////////////
175void alarm_update( xptr_t   thread_xp,
176                   cycle_t  new_date )
177{
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 );
181
182    // get local pointer on core running target thread
183    core_t * core = hal_remote_lpt( XPTR( tgt_cxy , &tgt_ptr->core ) );
184
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 );
190
191    // unlink the alarm from the core list
192    list_remote_unlink( tgt_cxy , &tgt_ptr->alarm.list );
193
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 );
197
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
Note: See TracBrowser for help on using the repository browser.