| [23] | 1 | /* |
|---|
| [669] | 2 | * alarm.h - timer based kernel alarm specification |
|---|
| [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 | |
|---|
| 24 | |
|---|
| [669] | 25 | #ifndef _ALARM_H_ |
|---|
| 26 | #define _ALARM_H_ |
|---|
| 27 | |
|---|
| 28 | #include <hal_kernel_types.h> |
|---|
| [23] | 29 | #include <list.h> |
|---|
| 30 | |
|---|
| [669] | 31 | /**** Forward declarations ****/ |
|---|
| [23] | 32 | |
|---|
| [669] | 33 | struct thread_s; |
|---|
| [23] | 34 | |
|---|
| [669] | 35 | /****************************************************************************************** |
|---|
| 36 | * This structure defines a generic, timer based, kernel alarm. |
|---|
| 37 | * |
|---|
| 38 | * - An alarm being attached to a given thread, the alarm descriptor is embedded in the |
|---|
| 39 | * thread descriptor. A client thread can use the alarm_start() function to dynamically |
|---|
| 40 | * activate the alarm. It can use the alarm_stop() function to desactivate this alarm. |
|---|
| 41 | * - This kernel alarm is generic, as the alarm handler (executed when the alarm rings), |
|---|
| 42 | * and the handler arguments are defined by two pointers <func_ptr> and <args_xp>. |
|---|
| 43 | * - When an alarm is created by a client thread, it is registered in the list of alarms |
|---|
| 44 | * rooted in the core running the client thread. When it is stopped, the alarm is simply |
|---|
| 45 | * removed from this list. |
|---|
| 46 | * - When creating an alarm, the client thread must define an absolute date (in cycles), |
|---|
| 47 | * the func_ptr local pointer, and the args_xp extended pointer. |
|---|
| 48 | * - The list of alarms is ordered by increasing dates. At each TICK received by a core, |
|---|
| 49 | * the date of the first registered alarm is compared to the current date (in the |
|---|
| 50 | * core_clock() function). The alarm handler is executed when current_date >= alarm_date. |
|---|
| 51 | * - It is the handler responsability to stop a ringing alarm, or update the date. |
|---|
| 52 | * |
|---|
| 53 | * This mechanism is used bi the almos_mkh implementation of the TCP protocoL. |
|---|
| 54 | ******************************************************************************************/ |
|---|
| [23] | 55 | |
|---|
| [669] | 56 | typedef struct alarm_s |
|---|
| [23] | 57 | { |
|---|
| [669] | 58 | cycle_t date; /*! absolute date for handler execution */ |
|---|
| 59 | void * func_ptr; /*! local pointer on alarm handler function */ |
|---|
| 60 | xptr_t args_xp; /*! local pointer on handler arguments */ |
|---|
| 61 | list_entry_t list; /*! all alarms attached to the same core */ |
|---|
| 62 | } |
|---|
| 63 | alarm_t; |
|---|
| [23] | 64 | |
|---|
| [669] | 65 | /******************************************************************************************* |
|---|
| 66 | * This defines the generic prototype for an alarm handler. |
|---|
| 67 | ******************************************************************************************/ |
|---|
| [23] | 68 | |
|---|
| [669] | 69 | typedef void (alarm_handler_t) ( xptr_t args_xp ); |
|---|
| [23] | 70 | |
|---|
| [669] | 71 | /******************************************************************************************* |
|---|
| 72 | * This function initializes the alarm descriptor embedded in the thread identified by the |
|---|
| 73 | * <thread> argument from the <date>, <func_ptr>, <args_ptr> arguments, and registers it |
|---|
| 74 | * in the ordered list rooted in the core running this <thread>. |
|---|
| 75 | ******************************************************************************************* |
|---|
| 76 | * @ date : absolute date (in cycles). |
|---|
| 77 | * @ func_ptr : local pointer on the handler to execute when the alarm rings. |
|---|
| 78 | * @ args_xp : extended pointer on the handler arguments. |
|---|
| 79 | * @ thread : local pointer on the client thread. |
|---|
| 80 | ******************************************************************************************/ |
|---|
| 81 | void alarm_start( cycle_t date, |
|---|
| 82 | void * func_ptr, |
|---|
| 83 | xptr_t args_xp, |
|---|
| 84 | struct thread_s * thread ); |
|---|
| [23] | 85 | |
|---|
| [669] | 86 | /******************************************************************************************* |
|---|
| 87 | * This function updates the date of the alarm attached to the thread identified by the |
|---|
| 88 | * <thread> argument. The list of alarms rooted in the core running the client thread |
|---|
| 89 | * is modified to respect the absolute dates ordering. |
|---|
| 90 | ******************************************************************************************* |
|---|
| 91 | * @ thread : local pointer on the client thread. |
|---|
| 92 | * @ new_date : absolute new date (in cycles). |
|---|
| 93 | ******************************************************************************************/ |
|---|
| 94 | void alarm_update( struct thread_s * thread, |
|---|
| 95 | cycle_t new_date ); |
|---|
| [23] | 96 | |
|---|
| [669] | 97 | /******************************************************************************************* |
|---|
| 98 | * This function unlink an alarm identified by the <thread> argument from the list of |
|---|
| 99 | * alarms rooted in the core descriptor. |
|---|
| 100 | ******************************************************************************************* |
|---|
| 101 | * @ thread : local pointer on the client thread. |
|---|
| 102 | ******************************************************************************************/ |
|---|
| 103 | void alarm_stop( struct thread_s * thread ); |
|---|
| [23] | 104 | |
|---|
| 105 | |
|---|
| [669] | 106 | #endif /* _ALARM_H_ */ |
|---|