Added SILC Thread Queue API
[silc.git] / lib / silcutil / silctimer.c
1 /*
2
3   silctimer.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silc.h"
21
22 /* Start timer */
23
24 void silc_timer_start(SilcTimer timer)
25 {
26   struct timeval curtime;
27
28   silc_gettimeofday(&curtime);
29   timer->start_sec = curtime.tv_sec;
30   timer->start_usec = curtime.tv_usec;
31   timer->timer_sec = 0;
32   timer->timer_usec = 0;
33   timer->sync_diff = 0;
34   timer->sync_tdiff = 0;
35   timer->running = TRUE;
36 }
37
38 /* Stop timer */
39
40 void silc_timer_stop(SilcTimer timer)
41 {
42   struct timeval curtime;
43
44   silc_gettimeofday(&curtime);
45
46   if (curtime.tv_usec < timer->start_usec) {
47     curtime.tv_sec--;
48     curtime.tv_usec += 1000000L;
49   }
50   timer->timer_sec = curtime.tv_sec - timer->start_sec;
51   timer->timer_usec = curtime.tv_usec - timer->start_usec;
52   timer->timer_usec -= timer->sync_diff;
53
54   timer->running = FALSE;
55 }
56
57 /* Continue stopped timer */
58
59 void silc_timer_continue(SilcTimer timer)
60 {
61   struct timeval curtime;
62
63   if (timer->running)
64     return;
65
66   silc_gettimeofday(&curtime);
67
68   if (curtime.tv_usec < timer->timer_usec) {
69     curtime.tv_sec--;
70     curtime.tv_usec += 1000000L;
71   }
72   timer->start_sec = curtime.tv_sec - timer->timer_sec;
73   timer->start_usec = curtime.tv_usec - timer->timer_usec;
74
75   timer->running = TRUE;
76 }
77
78 /* Return timer value */
79
80 void silc_timer_value(SilcTimer timer,
81                       SilcUInt64 *elapsed_time_seconds,
82                       SilcUInt32 *elapsed_time_microseconds)
83 {
84   if (timer->running) {
85     struct timeval curtime;
86
87     silc_gettimeofday(&curtime);
88
89     if (curtime.tv_usec < timer->start_usec) {
90       curtime.tv_sec--;
91       curtime.tv_usec += 1000000L;
92     }
93     timer->timer_sec = curtime.tv_sec - timer->start_sec;
94     timer->timer_usec = curtime.tv_usec - timer->start_usec;
95     timer->timer_usec -= timer->sync_diff;
96   }
97
98   if (elapsed_time_seconds)
99     *elapsed_time_seconds = timer->timer_sec;
100   if (elapsed_time_microseconds)
101     *elapsed_time_microseconds = timer->timer_usec;
102 }
103
104 /* Return timer value */
105
106 void silc_timer_value_time(SilcTimer timer, SilcTime ret_time)
107 {
108   SilcUInt64 sec;
109   SilcUInt32 usec;
110
111   silc_timer_value(timer, &sec, &usec);
112   sec = ((timer->start_sec + sec) * (SilcUInt64)1000);
113   sec += ((timer->start_usec + usec) / 1000);
114   silc_time_value(sec, ret_time);
115 }
116
117 /* Return start time */
118
119 void silc_timer_start_time(SilcTimer timer, SilcTime ret_start_time)
120 {
121   silc_time_value(((timer->start_sec * (SilcUInt64)1000) +
122                    (timer->start_usec / 1000)), ret_start_time);
123 }
124
125 /* Return TRUE if timer is running */
126
127 SilcBool silc_timer_is_running(SilcTimer timer)
128 {
129   return timer->running;
130 }