]> rtime.felk.cvut.cz Git - ulut.git/commitdiff
The uLUt root htimer can be redirected through ul_root_htimer_ops.
authorppisa <ppisa>
Tue, 27 Jan 2009 20:34:04 +0000 (20:34 +0000)
committerppisa <ppisa>
Tue, 27 Jan 2009 20:34:04 +0000 (20:34 +0000)
These changes are necessary to allow integration into another
root main loop and events processing framework in future.

The types ul_mstime_t/ul_msdiff_t and ul_htim_time_t/ul_htim_diff_t
are not intermixed in the code and they could be separated in the
future if there appears need to do so.

ulut/Makefile.omk
ulut/ul_htimchk.c
ulut/ul_htimdefault.c [new file with mode: 0644]
ulut/ul_htimer.h
ulut/ul_htimmstime.c
ulut/ul_htimroot.c [new file with mode: 0644]

index 47ae1f35f2b3c1f18686c9e813b75a309fc8c5dd..6ac157eedae7ca39f382b76aeee14f428102ba80 100644 (file)
@@ -21,7 +21,8 @@ endif
 
 ulut_SOURCES = ul_dbufbase.c ul_dbufmore.c ul_gsa.c ul_gsacust.c \
               ul_gavlprim.c ul_gavl.c ul_hptree.c \
-              ul_htimer.c ul_htimbase.c ul_htimmstime.c \
+              ul_htimer.c ul_htimbase.c ul_htimmstime.c ul_htimroot.c \
+              ul_htimdefault.c \
               ul_evcbase.c ul_uniqid.c ul_dbufflog.c ul_logbase.c \
               ul_logreg.c ul_cbuff.c
 
index 832ef4426f5467fed6b210e7fc45ce1ab4b7efa1..2b485e6e1ccc8b9c484c04aa31daa2130e23af9e 100644 (file)
@@ -10,6 +10,9 @@
 #include <malloc.h>
 #else /*_WIN32*/
 #include <malloc.h>
+#include <sys/time.h>
+#include <windows.h>
+#define WITHOUT_SYS_SELECT
 #endif /*_WIN32*/
 #include "ul_htimer.h"
 
@@ -39,17 +42,17 @@ void timing_test(void)
   ul_htimer_t *items;
   ul_htimer_t *p;
   ul_htim_time_t cmp_time;
-  
+  ul_htimer_queue_t *root_htimer;
   struct timeval time_start, time_stop;
 
   printf("\nRunning htimer timing test for %d items\n",items_cnt);
-  
+
   items=malloc(items_cnt*sizeof(ul_htimer_t));
   if(!items){
     printf("malloc items failed\n");
     return;
   }
-  
+
   for(i=0;i<items_cnt;i++){
     ul_htimer_init_detached(&items[i]);
     items[i].function=NULL;
@@ -64,20 +67,22 @@ void timing_test(void)
     }
   }
   cmp_time=0x7fffffff;
-      
+
+  root_htimer = ul_root_htimer_get(0, NULL);
+
   gettimeofday(&time_start,NULL);
   for(i=0;i<items_cnt;i++){
   //for(i=items_cnt;i-->0;){
-    if(ul_htimer_add(&ul_root_htimer, items+i)<0)
+    if(ul_htimer_add(root_htimer, items+i)<0)
       printf("ul_htimer_add is buggy\n");
   }
   gettimeofday(&time_stop,NULL);
   timing_test_print(&time_start,&time_stop,"htimer insert");
-  
+
   r=0;
   gettimeofday(&time_start,NULL);
   for(i=0;i<items_cnt;i++){
-    if(!(p=ul_htimer_cut_expired(&ul_root_htimer, &cmp_time)))
+    if(!(p=ul_htimer_cut_expired(root_htimer, &cmp_time)))
       printf("ul_htimer_cut_expired NULL\n");
     else{
       if(0){
@@ -91,6 +96,8 @@ void timing_test(void)
   gettimeofday(&time_stop,NULL);
   timing_test_print(&time_start,&time_stop,"cut expired");
 
+  ul_root_htimer_put(root_htimer);
+
   free(items);
 }
 
@@ -100,14 +107,22 @@ void timing_test(void)
 void test_htimer_fnc(unsigned long data)
 {
   char s[30];
+  ul_htim_time_t actual_time;
   ul_get_log_time_str(s);
-  printf("%6ld : ms %8ld real %s\n",data,ul_mstime_last,s);
+  ul_root_htimer_current_time(0, &actual_time);
+  printf("%6ld : ms %8ld real %s\n",data,actual_time,s);
 }
 
 int mstime_test(void)
 {
   int i;
-  
+  ul_htimer_queue_t *root_htimer;
+  ul_htim_time_t actual_time;
+
+  ul_root_htimer_current_time(0, &actual_time);
+  actual_time += 1000 - 1 - (actual_time - 1) % 1000;
+
+  root_htimer = ul_root_htimer_get(0, NULL);
   for(i=0;i<100;i++){
     ul_htimer_t *timer;
     /*allocate new timer*/
@@ -116,18 +131,27 @@ int mstime_test(void)
     ul_htimer_init_detached(timer);
     timer->function=test_htimer_fnc;
     timer->data=i;
-    ul_htimer_set_expire(timer,(i&~1)*500);
-    if(ul_htimer_add(&ul_root_htimer, timer)<0)
+    ul_htimer_set_expire(timer,actual_time + (i&~1)*500);
+    if(ul_htimer_add(root_htimer, timer)<0)
       printf("ul_htimer_add is buggy\n");
   }
-  
+  ul_root_htimer_put(root_htimer);
+
   while(1){
+    ul_htim_time_t next_expire;
     ul_msdiff_t ms_sleep;
-    ul_mstime_update();
-    ul_htimer_run_expired(&ul_root_htimer,&ul_mstime_last);
-    ul_mstime_update();
-    ul_compute_mstime_next();
-    ms_sleep=ul_mstime_next-ul_mstime_last;
+
+    ul_root_htimer_current_time(0, &actual_time);
+
+    root_htimer = ul_root_htimer_get(0, NULL);
+    ul_htimer_run_expired(root_htimer,&actual_time);
+    if(!ul_htimer_next_expire(root_htimer,&next_expire))
+      next_expire=actual_time+0x10000000;
+    ul_root_htimer_put(root_htimer);
+
+    ul_root_htimer_current_time(0, &actual_time);
+
+    ul_htime_sub2ms(&ms_sleep, &next_expire, &actual_time);
     if(ms_sleep<0) ms_sleep=0;
     /* we do not want to overflow usec field of timeout.tv_usec */
     if(ms_sleep>(~(ul_mstime_t)0/2001)) ms_sleep=~(ul_mstime_t)0/2001;
@@ -182,8 +206,7 @@ int mstime_test(void)
 
 int main(int argc, char *argv[])
 {
-  ul_mstime_init();
-  ul_htimer_init_queue(&ul_root_htimer);
+  ul_root_htimer_init(0, NULL);
 
   timing_test();
   mstime_test();
diff --git a/ulut/ul_htimdefault.c b/ulut/ul_htimdefault.c
new file mode 100644 (file)
index 0000000..058b910
--- /dev/null
@@ -0,0 +1,38 @@
+/*******************************************************************
+  uLan Utilities Library - C library of basic reusable constructions
+
+  ul_htimdefault.h  - hierarchical timer compile time default ops
+
+  (C) Copyright 2003-2009 by Pavel Pisa - Originator
+
+  The uLan utilities library can be used, copied and modified under
+  next licenses
+    - GPL - GNU General Public License
+    - LGPL - GNU Lesser General Public License
+    - MPL - Mozilla Public License
+    - and other licenses added by project originators
+  Code can be modified and re-distributed under any combination
+  of the above listed licenses. If contributor does not agree with
+  some of the licenses, he/she can delete appropriate line.
+  Warning, if you delete all lines, you are not allowed to
+  distribute source code and/or binaries utilizing code.
+  
+  See files COPYING and README for details.
+
+ *******************************************************************/
+
+#include <string.h>
+#include "ul_htimer.h"
+
+#ifdef UL_HTIMER_WITH_MSTIME
+
+#ifdef UL_HTIMER_WITH_STD_TYPE
+
+extern ul_root_htimer_ops_t ul_mstime_root_htimer_ops;
+
+ul_root_htimer_ops_t *ul_root_htimer_ops_compile_default =
+                                      &ul_mstime_root_htimer_ops;
+
+#endif /*UL_HTIMER_WITH_STD_TYPE*/
+
+#endif /*UL_HTIMER_WITH_MSTIME*/
index 6bdd6693d0b6a7e07135eb0f3cbddf9ebb307d6d..ebe8f81735e1c5df2b2cd7edbb33eb7b7f21c39b 100644 (file)
@@ -3,7 +3,7 @@
 
   ul_htimer.h  - hierarchical timer basic declarations
 
-  (C) Copyright 2003-2004 by Pavel Pisa - Originator
+  (C) Copyright 2003-2009 by Pavel Pisa - Originator
 
   The uLan utilities library can be used, copied and modified under
   next licenses
@@ -216,27 +216,109 @@ void ul_htimer_run_expired(ul_htimer_queue_t *queue, ul_htim_time_t *pact_time);
  * int ul_htimer_add(ul_htimer_queue_t *queue, ul_htimer_t *timer);
  * int ul_htimer_detach(ul_htimer_queue_t *queue, ul_htimer_t *timer);
  * int ul_htimer_first_changed(ul_htimer_queue_t *queue);
- * int ul_htimer_next_expire(ul_htimer_queue_t *queue, ul_htimer_time_t *pnext_time);
- * ul_htimer_t *ul_htimer_cut_expired(ul_htimer_queue_t *queue, ul_htimer_time_t *pact_time);
+ * int ul_htimer_next_expire(ul_htimer_queue_t *queue, ul_htim_time_t *pnext_time);
+ * ul_htimer_t *ul_htimer_cut_expired(ul_htimer_queue_t *queue, ul_htim_time_t *pact_time);
  * void ul_htimer_init_detached(ul_htimer_t *timer);
- * void ul_htimer_set_expire(ul_htimer_t *timer, ul_htimer_time_t expire);
- * ul_htimer_time_t ul_htimer_get_expire(ul_htimer_t *timer);
+ * void ul_htimer_set_expire(ul_htimer_t *timer, ul_htim_time_t expire);
+ * ul_htim_time_t ul_htimer_get_expire(ul_htimer_t *timer);
  */
 
 #endif /*UL_HTIMER_WITH_STD_TYPE*/
 
+/*===========================================================*/
+/*  Standard timer support to provide default root timer */
+
+#ifdef UL_HTIMER_WITH_STD_TYPE
+
+typedef struct ul_root_htimer_ops_t {
+  int (*timer_root_init)(int options, void *context);
+  ul_htimer_queue_t *(*timer_root_get)(int options, void *context);
+  void (*timer_root_put)(ul_htimer_queue_t *queue);
+  int (*current_time)(int options, ul_htim_time_t *htimer_time);
+} ul_root_htimer_ops_t;
+
+extern ul_root_htimer_ops_t *ul_root_htimer_ops;
+extern ul_root_htimer_ops_t *ul_root_htimer_ops_compile_default;
+
+int ul_root_htimer_init(int options, void *context);
+
+static inline ul_htimer_queue_t *ul_root_htimer_get(int options, void *context)
+{
+  return ul_root_htimer_ops->timer_root_get(options, context);
+}
+
+static inline void ul_root_htimer_put(ul_htimer_queue_t *queue)
+{
+  ul_root_htimer_ops->timer_root_put(queue);
+}
+
+static inline int ul_root_htimer_current_time(int options, ul_htim_time_t *htimer_time)
+{
+  return ul_root_htimer_ops->current_time(options, htimer_time);
+}
+
+int ul_root_htimer_add(ul_htimer_t *timer);
+
+int ul_root_htimer_detach(ul_htimer_t *timer);
+#endif /*UL_HTIMER_WITH_STD_TYPE*/
+
 #ifdef UL_HTIMER_WITH_MSTIME
 #ifdef UL_HTIMER_WITH_STD_TYPE
-ul_htimer_queue_t ul_root_htimer;
+static inline int ul_htimdiff2ms(ul_msdiff_t *ms, const ul_htim_diff_t *htimdiff)
+{
+  *ms = *htimdiff;
+  return 0;
+}
+
+static inline int ul_ms2htimdiff(ul_htim_diff_t *htimdiff, const ul_msdiff_t *ms)
+{
+  *htimdiff = *ms;
+  return 0;
+}
+
+static inline int ul_htime2mstime(ul_mstime_t *ms, const ul_htim_time_t *htime)
+{
+  *ms = *htime;
+  return 0;
+}
+
+static inline int ul_mstime2htime(ul_htim_time_t *htime, const ul_mstime_t *ms)
+{
+  *htime = *ms;
+  return 0;
+}
+
+static inline int ul_htime_sub2ms(ul_msdiff_t *ms, const ul_htim_time_t *htimto,
+                                const ul_htim_time_t *htimfrom)
+{
+  ul_htim_diff_t htimdiff = *htimto - *htimfrom;
+  return ul_htimdiff2ms(ms, &htimdiff);
+}
+
+static inline int ul_htime_sub(ul_htim_diff_t *diff, const ul_htim_time_t *htimto,
+                                const ul_htim_time_t *htimfrom)
+{
+  *diff = *htimto - *htimfrom;
+  return 0;
+}
+
+static inline int ul_htime_add(ul_htim_diff_t *sum, const ul_htim_time_t *htimfrom,
+                                const ul_htim_diff_t *diff)
+{
+  *sum = *htimfrom + *diff;
+  return 0;
+}
+
 #endif /*UL_HTIMER_WITH_STD_TYPE*/
-ul_mstime_t ul_mstime_last;
-ul_mstime_t ul_mstime_next;
+
+//ul_mstime_t ul_mstime_last;
+//ul_mstime_t ul_mstime_next;
 
 void ul_mstime_now(ul_mstime_t *mstm);
-void ul_mstime_update(void);
-void ul_mstime_init(void);
+//void ul_mstime_update(void);
+//void ul_mstime_init(void);
 void ul_get_log_time_str(char str[30]);
-void ul_compute_mstime_next(void);
+//void ul_compute_mstime_next(void);
 
 #endif /*UL_HTIMER_WITH_MSTIME*/
 
index cc79e4237507acfa724a6e8a2de407e57f66b24a..ff2c2d8a5f3be12c749dc994f1a7d4920cbe5520 100644 (file)
@@ -4,7 +4,7 @@
   ul_htimmstime.c   - standard hierarchical timer for microsecond
                      time resolution
 
-  (C) Copyright 2003 by Pavel Pisa - Originator
+  (C) Copyright 2003-2009 by Pavel Pisa - Originator
 
   The uLan utilities library can be used, copied and modified under
   next licenses
@@ -77,8 +77,10 @@ void ul_mstime_update(void)
 void ul_mstime_init(void)
 {
   ul_mstime_update();
+  /*
   ul_mstime_base_offs=-ul_mstime_last;
   ul_mstime_update();
+  */
 }
 
 void
@@ -87,7 +89,7 @@ ul_get_log_time_str(char str[30])
   time_t log_time;
   struct tm *log_tm;
   time(&log_time);
-  
+
   log_tm=localtime(&log_time);
   sprintf(str,"%04d-%02d-%02d %02d:%02d:%02d",
           (int)log_tm->tm_year+1900,(int)log_tm->tm_mon+1,
@@ -97,9 +99,45 @@ ul_get_log_time_str(char str[30])
 
 void ul_compute_mstime_next(void)
 {
-  if(!ul_htimer_next_expire(&ul_root_htimer,&ul_mstime_next))
+  ul_htim_time_t htim_next;
+  if(ul_htimer_next_expire(&ul_root_htimer, &htim_next))
+    ul_htime2mstime(&ul_mstime_next, &htim_next);
+  else
     ul_mstime_next=ul_mstime_last+0x10000000;
 }
-  
+
+#ifdef UL_HTIMER_WITH_STD_TYPE
+
+int ul_mstime_root_htimer_init(int options, void *context)
+{
+  ul_mstime_init();
+  return 0;
+}
+
+inline ul_htimer_queue_t *ul_mstime_root_htimer_get(int options, void *context)
+{
+  return &ul_root_htimer;
+}
+
+void ul_mstime_root_htimer_put(ul_htimer_queue_t *queue)
+{
+}
+
+int ul_mstime_root_htimer_current_time(int options, ul_htim_time_t *htimer_time)
+{
+  ul_mstime_t mstime;
+  ul_mstime_now(&mstime);
+  ul_mstime2htime(htimer_time, &mstime);
+  return 0;
+}
+
+ul_root_htimer_ops_t ul_mstime_root_htimer_ops = {
+  .timer_root_init = ul_mstime_root_htimer_init,
+  .timer_root_get = ul_mstime_root_htimer_get,
+  .timer_root_put = ul_mstime_root_htimer_put,
+  .current_time = ul_mstime_root_htimer_current_time,
+};
+
+#endif /*UL_HTIMER_WITH_STD_TYPE*/
 
 #endif /*UL_HTIMER_WITH_MSTIME*/
diff --git a/ulut/ul_htimroot.c b/ulut/ul_htimroot.c
new file mode 100644 (file)
index 0000000..2cdff67
--- /dev/null
@@ -0,0 +1,56 @@
+/*******************************************************************
+  uLan Utilities Library - C library of basic reusable constructions
+
+  ul_htimroot.h  - hierarchical timer support for root htimer queue
+
+  (C) Copyright 2003-2009 by Pavel Pisa - Originator
+
+  The uLan utilities library can be used, copied and modified under
+  next licenses
+    - GPL - GNU General Public License
+    - LGPL - GNU Lesser General Public License
+    - MPL - Mozilla Public License
+    - and other licenses added by project originators
+  Code can be modified and re-distributed under any combination
+  of the above listed licenses. If contributor does not agree with
+  some of the licenses, he/she can delete appropriate line.
+  Warning, if you delete all lines, you are not allowed to
+  distribute source code and/or binaries utilizing code.
+  
+  See files COPYING and README for details.
+
+ *******************************************************************/
+
+#include <string.h>
+#include "ul_htimer.h"
+
+#ifdef UL_HTIMER_WITH_STD_TYPE
+
+ul_root_htimer_ops_t *ul_root_htimer_ops;
+
+int ul_root_htimer_add(ul_htimer_t *timer)
+{
+  int ret;
+  ul_htimer_queue_t *root_htimer = ul_root_htimer_get(0, NULL);
+  ret = ul_htimer_add(root_htimer, timer);
+  ul_root_htimer_put(root_htimer);
+  return ret;
+}
+
+int ul_root_htimer_detach(ul_htimer_t *timer)
+{
+  int ret;
+  ul_htimer_queue_t *root_htimer = ul_root_htimer_get(0, NULL);
+  ret = ul_htimer_detach(root_htimer, timer);
+  ul_root_htimer_put(root_htimer);
+  return ret;
+}
+
+int ul_root_htimer_init(int options, void *context)
+{
+  if(ul_root_htimer_ops == NULL)
+    ul_root_htimer_ops = ul_root_htimer_ops_compile_default;
+  return ul_root_htimer_ops->timer_root_init(options, context);
+}
+
+#endif /*UL_HTIMER_WITH_STD_TYPE*/