1 /*******************************************************************
2 uLan Utilities Library - C library of basic reusable constructions
4 ul_htimmstime.c - standard hierarchical timer for microsecond
7 (C) Copyright 2003-2009 by Pavel Pisa - Originator
9 The uLan utilities library can be used, copied and modified under
11 - GPL - GNU General Public License
12 - LGPL - GNU Lesser General Public License
13 - MPL - Mozilla Public License
14 - and other licenses added by project originators
15 Code can be modified and re-distributed under any combination
16 of the above listed licenses. If contributor does not agree with
17 some of the licenses, he/she can delete appropriate line.
18 Warning, if you delete all lines, you are not allowed to
19 distribute source code and/or binaries utilizing code.
21 See files COPYING and README for details.
23 *******************************************************************/
27 #include "ul_gavlflesint.h"
28 #include "ul_htimer.h"
30 #ifdef UL_HTIMER_WITH_MSTIME
35 ul_htimer_queue_t ul_root_htimer;
37 ul_mstime_t ul_mstime_last;
38 ul_mstime_t ul_mstime_next;
40 static ul_mstime_t ul_mstime_base_offs;
46 void ul_mstime_now(ul_mstime_t *mstm)
48 struct timeval tv_actual;
49 ul_mstime_t ms_actual;
51 gettimeofday(&tv_actual,NULL);
53 ms_actual=sec*1000+tv_actual.tv_usec/1000+ul_mstime_base_offs;
58 #include <sys/timeb.h>
60 void ul_mstime_now(ul_mstime_t *mstm)
62 struct timeb tb_actual;
63 ul_mstime_t ms_actual;
67 ms_actual=sec*1000+tb_actual.millitm+ul_mstime_base_offs;
72 void ul_mstime_update(void)
74 ul_mstime_now(&ul_mstime_last);
77 void ul_mstime_init(void)
81 ul_mstime_base_offs=-ul_mstime_last;
87 ul_get_log_time_str(char str[30])
93 log_tm=localtime(&log_time);
94 sprintf(str,"%04d-%02d-%02d %02d:%02d:%02d",
95 (int)log_tm->tm_year+1900,(int)log_tm->tm_mon+1,
96 (int)log_tm->tm_mday,(int)log_tm->tm_hour,
97 (int)log_tm->tm_min,(int)log_tm->tm_sec);
100 void ul_compute_mstime_next(void)
102 ul_htim_time_t htim_next;
103 if(ul_htimer_next_expire(&ul_root_htimer, &htim_next))
104 ul_htime2mstime(&ul_mstime_next, &htim_next);
106 ul_mstime_next=ul_mstime_last+0x10000000;
109 #ifdef UL_HTIMER_WITH_STD_TYPE
111 static int ul_mstime_root_htimer_init(int options, void *context)
120 static ul_htimer_queue_t *ul_mstime_root_htimer_get(int options, void *context)
125 return &ul_root_htimer;
128 static void ul_mstime_root_htimer_put(ul_htimer_queue_t *queue)
133 static int ul_mstime_root_htimer_current_time(int options, ul_htim_time_t *htimer_time)
138 ul_mstime_now(&mstime);
139 ul_mstime2htime(htimer_time, &mstime);
143 ul_root_htimer_ops_t ul_mstime_root_htimer_ops = {
144 .timer_root_init = ul_mstime_root_htimer_init,
145 .timer_root_get = ul_mstime_root_htimer_get,
146 .timer_root_put = ul_mstime_root_htimer_put,
147 .current_time = ul_mstime_root_htimer_current_time,
150 #endif /*UL_HTIMER_WITH_STD_TYPE*/
152 #endif /*UL_HTIMER_WITH_MSTIME*/