]> git.meshlink.io Git - catta/blob - avahi-common/timeval.c
0cfb1362902afd7f0991fb404c70db4f2b337763
[catta] / avahi-common / timeval.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pthread.h>
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include "timeval.h"
31
32 int avahi_timeval_compare(const struct timeval *a, const struct timeval *b) {
33     assert(a);
34     assert(b);
35
36     if (a->tv_sec < b->tv_sec)
37         return -1;
38
39     if (a->tv_sec > b->tv_sec)
40         return 1;
41
42     if (a->tv_usec < b->tv_usec)
43         return -1;
44
45     if (a->tv_usec > b->tv_usec)
46         return 1;
47
48     return 0;
49 }
50
51 AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b) {
52     assert(a);
53     assert(b);
54
55     if (avahi_timeval_compare(a, b) < 0)
56         return - avahi_timeval_diff(b, a);
57
58     return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
59 }
60
61 struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec) {
62     AvahiUsec u;
63     assert(a);
64
65     u = usec + a->tv_usec;
66
67     if (u < 0) {
68         a->tv_usec = (long) (1000000 + (u % 1000000));
69         a->tv_sec += (long) (-1 + (u / 1000000));
70     } else {
71         a->tv_usec = (long) (u % 1000000);
72         a->tv_sec += (long) (u / 1000000);
73     }
74
75     return a;
76 }
77
78 AvahiUsec avahi_age(const struct timeval *a) {
79     struct timeval now;
80
81     assert(a);
82
83     gettimeofday(&now, NULL);
84
85     return avahi_timeval_diff(&now, a);
86 }
87
88 struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter) {
89     assert(tv);
90
91     gettimeofday(tv, NULL);
92
93     if (msec)
94         avahi_timeval_add(tv, (AvahiUsec) msec*1000);
95
96     if (jitter) {
97         static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
98         static int last_rand;
99         static time_t timestamp = 0;
100
101         time_t now;
102         int r;
103
104         now = time(NULL);
105
106         pthread_mutex_lock(&mutex);
107         if (now >= timestamp + 10) {
108             timestamp = now;
109             last_rand = rand();
110         }
111
112         r = last_rand;
113
114         pthread_mutex_unlock(&mutex);
115
116         /* We use the same jitter for 10 seconds. That way our
117          * time events elapse in bursts which has the advantage that
118          * packet data can be aggregated better */
119
120         avahi_timeval_add(tv, (AvahiUsec) (jitter*1000.0*r/(RAND_MAX+1.0)));
121     }
122
123     return tv;
124 }
125