summaryrefslogtreecommitdiff
path: root/milkway/mw-timeout-source.c
blob: 9790c1627d5b3de4a41b5cd56c190cbda68966a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* Milkway
 *
 * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "milkwayint.h"
#include "milkway/mw-timeout-source.h"

#include <stdlib.h>

#ifndef MIN
#define MIN(a, b) (a) > (b) ? (b) : (a)
#endif

#ifndef ABS
#define ABS(a) (a) > 0 ? (a) : (-(a))
#endif

static void
mw_timeout_set_expiration (mw_timeout_source_t *self,
			   mw_timeval_t         *current_time)
{
    static int timer_perturb = -1;
    mw_uint_t seconds = self->interval / 1000;
    mw_uint_t msecs = self->interval - seconds * 1000;

    self->expiration.tv_sec = current_time->tv_sec + seconds;
    self->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
    if (self->expiration.tv_usec >= 1000000) {
	self->expiration.tv_usec -= 1000000;
	self->expiration.tv_sec++;
    }

    if (timer_perturb == -1) {
      /*
       * we want a per machine/session unique 'random' value; try the dbus
       * address first, that has a UUID in it. If there is no dbus, use the
       * hostname for hashing.
       */
	const char *session_bus_address = getenv ("DBUS_SESSION_BUS_ADDRESS");
	if (!session_bus_address)
	    session_bus_address = getenv ("HOSTNAME");
	if (session_bus_address)
	    timer_perturb = ABS ((int) mw_str_hash ((void*)session_bus_address));
	else
	    timer_perturb = 0;
    }
    if (self->granularity) {
	int remainder;
	int gran; /* in usecs */
	int perturb;

	gran = self->granularity * 1000;
	perturb = timer_perturb % gran;
	/*
	 * We want to give each machine a per machine pertubation;
	 * shift time back first, and forward later after the rounding
	 */
	self->expiration.tv_usec -= perturb;
	if (self->expiration.tv_usec < 0) {
	    self->expiration.tv_usec += 1000000;
	    self->expiration.tv_sec--;
	}

	remainder = self->expiration.tv_usec % gran;
	if (remainder >= gran/4) /* round up */
	    self->expiration.tv_usec += gran;
	self->expiration.tv_usec -= remainder;
	/* shift back */
	self->expiration.tv_usec += perturb;

	/* the rounding may have overflown tv_usec */
	while (self->expiration.tv_usec > 1000000) {
	    self->expiration.tv_usec -= 1000000;
	    self->expiration.tv_sec++;
	}
    }
}

mw_timeout_source_t*
mw_timeout_source_init(mw_timeout_source_t *self,
		       int interval,
		       mw_source_func_t func,
		       mw_pointer_t user_data,
		       mw_destroy_func_t destroy_func)
{
    if (!mw_source_init(&self->base))
	return NULL;

    self->interval = interval;
    self->granularity = 0;
    self->func = func;
    self->user_data = user_data;
    self->destroy_func = destroy_func;

    mw_timeval_clear(&self->expiration);
    return self;
}

mw_timeout_source_t*
mw_timeout_source_new(int interval,
		      mw_source_func_t func,
		      mw_pointer_t user_data,
		      mw_destroy_func_t destroy_func)
{
    mw_timeout_source_t *self;

    self = mw_object_alloc(MW_TIMEOUT_SOURCE_TYPE);
    if (!self)
	return NULL;
    return mw_timeout_source_init(self, interval, func,
				  user_data, destroy_func);
}

static mw_bool_t
mw_timeout_source_prepare(mw_source_t *super,
			  int *timeout)
{
    mw_timeout_source_t* self = (mw_timeout_source_t*)super;
    mw_timeval_t current_time;
    long sec, msec;

    mw_source_get_current_time (super, &current_time);

    sec = self->expiration.tv_sec - current_time.tv_sec;
    msec = (self->expiration.tv_usec - current_time.tv_usec) / 1000;

    /* We do the following in a rather convoluted fashion to deal with
     * the fact that we don't have an integral type big enough to hold
     * the difference of two timevals in millseconds.
     */
    if (sec < 0 || (sec == 0 && msec < 0)) {
	msec = 0;
    } else {
      long interval_sec = self->interval / 1000;
      long interval_msec = self->interval % 1000;

      if (msec < 0) {
	  msec += 1000;
	  sec -= 1;
      }

      if (sec > interval_sec ||
	  (sec == interval_sec && msec > interval_msec)) {
	  /* The system time has been set backwards, so we
	   * reset the expiration time to now + self->interval;
	   * this at least avoids hanging for long periods of time.
	   */
	  mw_timeout_set_expiration (self, &current_time);
	  msec = MIN (INT_MAX, self->interval);
      } else {
	  msec = MIN (INT_MAX, (mw_uint_t)msec + 1000 * (mw_uint_t)sec);
      }
    }

    *timeout = (int)msec;
    return msec == 0;
}

static mw_bool_t
mw_timeout_source_check(mw_source_t *super)
{
    mw_timeout_source_t* self = (mw_timeout_source_t*)super;
    mw_timeval_t current_time;

    mw_source_get_current_time (super, &current_time);

    return ((self->expiration.tv_sec < current_time.tv_sec) ||
	    ((self->expiration.tv_sec == current_time.tv_sec) &&
	     (self->expiration.tv_usec <= current_time.tv_usec)));
}

static mw_bool_t
mw_timeout_source_dispatch(mw_source_t *super)
{
    mw_timeout_source_t* self = (mw_timeout_source_t*)super;

    if (!self->func)
      return MW_FALSE;

    if (self->func (super, self->user_data)) {
	mw_timeval_t current_time;

	mw_source_get_current_time (super, &current_time);
	mw_timeout_set_expiration (self, &current_time);

	return MW_TRUE;
    }

    return MW_FALSE;
}

static void
mw_timeout_source_finalize(mw_object_t *super)
{
    mw_timeout_source_t *self = (mw_timeout_source_t*)super;

    if (self->destroy_func)
	self->destroy_func(self->user_data);
    MW_SUPER_FINALIZE(super, MW_TIMEOUT_SOURCE_TYPE)(super);
}

static void
mw_timeout_source_type_init(mw_timeout_source_type_t *self)
{
    self->base.base.finalize = mw_timeout_source_finalize;
    self->base.prepare = mw_timeout_source_prepare;
    self->base.check = mw_timeout_source_check;
    self->base.dispatch = mw_timeout_source_dispatch;
}

MW_DEFINE_GET_TYPE(mw_timeout_source, mw_timeout_source_type_t,
		   MW_SOURCE_TYPE, "MWTimeoutSource", 0);