Index: trunk/kernel/kern/alarm.h
===================================================================
--- trunk/kernel/kern/alarm.h	(revision 23)
+++ trunk/kernel/kern/alarm.h	(revision 669)
@@ -1,15 +1,16 @@
 /*
- * time.h: thread time related management
- * 
- * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
- * Copyright (c) 2011,2012 UPMC Sorbonne Universites
+ * alarm.h - timer based kernel alarm specification            
  *
- * This file is part of ALMOS-kernel.
+ * Author     Alain Greiner (2016,2017,2018,2019,2020)
  *
- * ALMOS-kernel is free software; you can redistribute it and/or modify it
+ * Copyright (c) UPMC Sorbonne Universites
+ *
+ * This file is part of ALMOS-MKH.
+ *
+ * ALMOS-MKH is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by
  * the Free Software Foundation; version 2.0 of the License.
  *
- * ALMOS-kernel is distributed in the hope that it will be useful, but
+ * ALMOS-MKH is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
@@ -17,94 +18,89 @@
  *
  * You should have received a copy of the GNU General Public License
- * along with ALMOS-kernel; if not, write to the Free Software Foundation,
+ * along with ALMOS-MKH; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#ifndef _TIME_H_
-#define _TIME_H_
 
-#include <types.h>
+#ifndef _ALARM_H_
+#define _ALARM_H_
+
+#include <hal_kernel_types.h>
 #include <list.h>
-#include <device.h>
 
-struct event_s;
+/****  Forward declarations  ****/
 
-struct alarm_info_s 
+struct  thread_s;
+
+/******************************************************************************************
+ *   This structure defines a generic, timer based, kernel alarm.
+ *
+ * - An alarm being attached to a given thread, the alarm descriptor is embedded in the
+ *   thread descriptor. A client thread can use the alarm_start() function to dynamically
+ *   activate the alarm. It can use the alarm_stop() function to desactivate this alarm.
+ * - This kernel alarm is generic, as the alarm handler (executed when the alarm rings),
+ *   and the handler arguments are defined by two pointers <func_ptr> and <args_xp>.
+ * - When an alarm is created by a client thread, it is registered in the list of alarms
+ *   rooted in the core running the client thread. When it is stopped, the alarm is simply
+ *   removed from this list.
+ * - When creating an alarm, the client thread must define an absolute date (in cycles),
+ *   the func_ptr local pointer, and the args_xp extended pointer.
+ * - The list of alarms is ordered by increasing dates. At each TICK received by a core,
+ *   the date of the first registered alarm is compared to the current date (in the 
+ *   core_clock() function). The alarm handler is executed when current_date >= alarm_date.
+ * - It is the handler responsability to stop a ringing alarm, or update the date.  
+ * 
+ * This mechanism is used bi the almos_mkh implementation of the TCP protocoL.
+ ******************************************************************************************/
+
+typedef struct alarm_s
 {
-	uint_t signature;
+    cycle_t        date;           /*! absolute date for handler execution                */
+    void         * func_ptr;       /*! local pointer on alarm handler function            */
+    xptr_t         args_xp;        /*! local pointer on handler arguments                 */
+    list_entry_t   list;           /*! all alarms attached to the same core               */
+}
+alarm_t;
 
-	/* Public members */
-	struct event_s *event;
+/*******************************************************************************************
+ * This defines the generic prototype for an alarm handler.
+ ******************************************************************************************/
 
-	/* Private members */
-	uint_t tm_wakeup;
-	struct list_entry list;
-};
+typedef void (alarm_handler_t) ( xptr_t args_xp );
 
-struct alarm_s
-{ 
-	struct list_entry wait_queue;
-};
+/*******************************************************************************************
+ * This function initializes the alarm descriptor embedded in the thread identified by the
+ * <thread> argument from the <date>, <func_ptr>, <args_ptr> arguments, and registers it
+ * in the ordered list rooted in the core running this <thread>.
+ *******************************************************************************************
+ * @ date       : absolute date (in cycles).
+ * @ func_ptr   : local pointer on the handler to execute when the alarm rings.
+ * @ args_xp    : extended pointer on the handler arguments.
+ * @ thread     : local pointer on the client thread.
+ ******************************************************************************************/
+void alarm_start( cycle_t           date,
+                  void            * func_ptr,
+                  xptr_t            args_xp,
+                  struct thread_s * thread );
+
+/*******************************************************************************************
+ * This function updates the date of the alarm attached to the thread identified by the
+ * <thread> argument. The list of alarms rooted in the core running the client thread 
+ * is modified to respect the absolute dates ordering.
+ *******************************************************************************************
+ * @ thread     : local pointer on the client thread.
+ * @ new_date   : absolute new date (in cycles).
+ ******************************************************************************************/
+void alarm_update( struct thread_s * thread,
+                   cycle_t           new_date );
+
+/*******************************************************************************************
+ * This function unlink an alarm identified by the <thread> argument from the list of
+ * alarms rooted in the core descriptor.
+ *******************************************************************************************
+ * @ thread     : local pointer on the client thread.
+ ******************************************************************************************/
+void alarm_stop( struct thread_s * thread );
 
 
-struct timeb {
-	time_t         time;
-	unsigned short millitm;
-	short          timezone;
-	short          dstflag;
-};
-
-struct timeval {
-	clock_t tv_sec;    /* secondes */
-	clock_t tv_usec;   /* microsecondes */
-};
-
-struct timezone {
-	int tz_minuteswest;     /* minutes west of Greenwich */
-	int tz_dsttime;         /* type of DST correction */
-};
-
-
-struct tms 
-{
-       clock_t tms_utime;  /* user time */
-       clock_t tms_stime;  /* system time */
-       clock_t tms_cutime; /* user time of children */
-       clock_t tms_cstime; /* system time of children */
-};
-
-
-error_t alarm_manager_init(struct alarm_s *alarm);
-error_t alarm_wait(struct alarm_info_s *info, uint_t msec);
-
-void alarm_clock(struct alarm_s *alarm, uint_t ticks_nr);
-
-int sys_clock (uint64_t *val);
-int sys_alarm (unsigned nb_sec);
-int sys_ftime (struct timeb *utime);
-int sys_times(struct tms *utms);
-int sys_gettimeofday(struct timeval *tv, struct timezone *tz);
-
-#if CONFIG_THREAD_TIME_STAT
-struct thread_s;
-inline void tm_sleep_compute(struct thread_s *thread);
-inline void tm_usr_compute(struct thread_s *thread);
-inline void tm_sys_compute(struct thread_s *thread);
-inline void tm_wait_compute(struct thread_s *thread);
-inline void tm_exit_compute(struct thread_s *thread);
-inline void tm_born_compute(struct thread_s *thread);
-inline void tm_create_compute(struct thread_s *thread);
-
-#else
-
-#define tm_sleep_compute(thread)
-#define tm_usr_compute(thread)
-#define tm_sys_compute(thread)
-#define tm_wait_compute(thread)
-#define tm_exit_compute(thread)
-#define tm_born_compute(thread)
-#define tm_create_compute(thread)
-
-#endif	/* CONFIG_SCHED_STAT */
-
-#endif	/* _TIME_H_ */
+#endif	/* _ALARM_H_ */
