source: select.c @ 64638d9

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 64638d9 was 1895c29, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Fix owl_select_remove_dispatch bugs. Fix two bugs in owl_select_remove_dispatch: (1) Removing a dispatch from that dispatch's callback was bad, because it mutated the dispatch list while it was being traversed. Fix that by noting if we're in the middle of a dispatch, and deferring cleanup until later. (2) Calling owl_free on dispatches in select.c is a strange API, since it doesn't allocate them in the first place. Since we now have destructors, make freeing the dispatch their job.
  • Property mode set to 100644
File size: 8.3 KB
Line 
1#include "owl.h"
2
3static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $";
4
5static int dispatch_active = 0;
6
7int _owl_select_timer_cmp(owl_timer *t1, owl_timer *t2) {
8  return t1->time - t2->time;
9}
10
11int _owl_select_timer_eq(owl_timer *t1, owl_timer *t2) {
12  return t1 == t2;
13}
14
15owl_timer *owl_select_add_timer(int after, int interval, void (*cb)(owl_timer *, void *), void (*destroy)(owl_timer*), void *data)
16{
17  owl_timer *t = owl_malloc(sizeof(owl_timer));
18  GList **timers = owl_global_get_timerlist(&g);
19
20  t->time = time(NULL) + after;
21  t->interval = interval;
22  t->callback = cb;
23  t->destroy = destroy;
24  t->data = data;
25
26  *timers = g_list_insert_sorted(*timers, t,
27                                 (GCompareFunc)_owl_select_timer_cmp);
28  return t;
29}
30
31void owl_select_remove_timer(owl_timer *t)
32{
33  GList **timers = owl_global_get_timerlist(&g);
34  if (t && g_list_find(*timers, t)) {
35    *timers = g_list_remove(*timers, t);
36    if(t->destroy) {
37      t->destroy(t);
38    }
39    owl_free(t);
40  }
41}
42
43void owl_select_process_timers(struct timeval *timeout)
44{
45  time_t now = time(NULL);
46  GList **timers = owl_global_get_timerlist(&g);
47
48  while(*timers) {
49    owl_timer *t = (*timers)->data;
50    int remove = 0;
51
52    if(t->time > now)
53      break;
54
55    /* Reschedule if appropriate */
56    if(t->interval > 0) {
57      t->time = now + t->interval;
58      *timers = g_list_remove(*timers, t);
59      *timers = g_list_insert_sorted(*timers, t,
60                                     (GCompareFunc)_owl_select_timer_cmp);
61    } else {
62      remove = 1;
63    }
64
65    /* Do the callback */
66    t->callback(t, t->data);
67    if(remove) {
68      owl_select_remove_timer(t);
69    }
70  }
71
72  if(*timers) {
73    owl_timer *t = (*timers)->data;
74    timeout->tv_sec = t->time - now;
75    if (timeout->tv_sec > 60)
76      timeout->tv_sec = 60;
77  } else {
78    timeout->tv_sec = 60;
79  }
80
81  timeout->tv_usec = 0;
82}
83
84/* Returns the index of the dispatch for the file descriptor. */
85int owl_select_find_dispatch(int fd)
86{
87  int i, len;
88  owl_list *dl;
89  owl_dispatch *d;
90 
91  dl = owl_global_get_dispatchlist(&g);
92  len = owl_list_get_size(dl);
93  for(i = 0; i < len; i++) {
94    d = (owl_dispatch*)owl_list_get_element(dl, i);
95    if (d->fd == fd) return i;
96  }
97  return -1;
98}
99
100void owl_select_remove_dispatch_at(int elt) /* noproto */
101{
102  owl_list *dl;
103  owl_dispatch *d;
104
105  dl = owl_global_get_dispatchlist(&g);
106  d = (owl_dispatch*)owl_list_get_element(dl, elt);
107  owl_list_remove_element(dl, elt);
108  if (d->destroy) {
109    d->destroy(d);
110  }
111}
112
113/* Adds a new owl_dispatch to the list, replacing existing ones if needed. */
114void owl_select_add_dispatch(owl_dispatch *d)
115{
116  int elt;
117  owl_list *dl;
118
119  d->needs_gc = 0;
120
121  elt = owl_select_find_dispatch(d->fd);
122  dl = owl_global_get_dispatchlist(&g);
123 
124  if (elt != -1) {  /* If we have a dispatch for this FD */
125    owl_dispatch *d_old;
126    d_old = (owl_dispatch*)owl_list_get_element(dl, elt);
127    /* Ignore if we're adding the same dispatch again.  Otherwise
128       replace the old dispatch. */
129    if (d_old != d) {
130      owl_select_remove_dispatch_at(elt);
131    }
132  }
133  owl_list_append_element(dl, d);
134}
135
136/* Removes an owl_dispatch to the list, based on it's file descriptor. */
137void owl_select_remove_dispatch(int fd)
138{
139  int elt;
140  owl_list *dl;
141  owl_dispatch *d;
142
143  elt = owl_select_find_dispatch(fd);
144  if(elt == -1) {
145    return;
146  } else if(dispatch_active) {
147    /* Defer the removal until dispatch is done walking the list */
148    dl = owl_global_get_dispatchlist(&g);
149    d = (owl_dispatch*)owl_list_get_element(dl, elt);
150    d->needs_gc = 1;
151  } else {
152    owl_select_remove_dispatch_at(elt);
153  }
154}
155
156int owl_select_dispatch_count()
157{
158  return owl_list_get_size(owl_global_get_dispatchlist(&g));
159}
160
161int owl_select_add_perl_dispatch(int fd, SV *cb)
162{
163  int elt;
164  owl_dispatch *d;
165  elt = owl_select_find_dispatch(fd);
166  if (elt != -1) {
167    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
168    if (d->cfunc != owl_perlconfig_dispatch) {
169      /* don't mess with non-perl dispatch functions from here. */
170      return 1;
171    }
172  }
173
174  d = malloc(sizeof(owl_dispatch));
175  d->fd = fd;
176  d->cfunc = owl_perlconfig_dispatch;
177  d->destroy = owl_perlconfig_dispatch_free;
178  d->data = cb;
179  owl_select_add_dispatch(d);
180  return 0;
181}
182
183int owl_select_remove_perl_dispatch(int fd)
184{
185  int elt;
186  owl_dispatch *d;
187 
188  elt = owl_select_find_dispatch(fd);
189  if (elt != -1) {
190    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
191    if (d->cfunc == owl_perlconfig_dispatch) {
192      owl_select_remove_dispatch_at(elt);
193      return 0;
194    }
195  }
196  return 1;
197}
198
199int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
200{
201  int i, len, max_fd;
202  owl_dispatch *d;
203  owl_list *dl;
204
205  dl = owl_global_get_dispatchlist(&g);
206  FD_ZERO(r);
207  FD_ZERO(e);
208  max_fd = 0;
209  len = owl_select_dispatch_count(g);
210  for(i = 0; i < len; i++) {
211    d = (owl_dispatch*)owl_list_get_element(dl, i);
212    FD_SET(d->fd, r);
213    FD_SET(d->fd, e);
214    if (max_fd < d->fd) max_fd = d->fd;
215  }
216  return max_fd + 1;
217}
218
219void owl_select_gc()
220{
221  int i;
222  owl_list *dl;
223
224  dl = owl_global_get_dispatchlist(&g);
225  /*
226   * Count down so we aren't set off by removing items from the list
227   * during the iteration.
228   */
229  for(i = owl_list_get_size(dl) - 1; i >= 0; i--) {
230    owl_dispatch *d = owl_list_get_element(dl, i);
231    if(d->needs_gc) {
232      owl_select_remove_dispatch_at(i);
233    }
234  }
235}
236
237void owl_select_dispatch(fd_set *fds, int max_fd)
238{
239  int i, len;
240  owl_dispatch *d;
241  owl_list *dl;
242
243  dl = owl_global_get_dispatchlist(&g);
244  len = owl_select_dispatch_count();
245
246  dispatch_active = 1;
247
248  for(i = 0; i < len; i++) {
249    d = (owl_dispatch*)owl_list_get_element(dl, i);
250    /* While d shouldn't normally be null, the list may be altered by
251     * functions we dispatch to. */
252    if (d != NULL && !d->needs_gc && FD_ISSET(d->fd, fds)) {
253      if (d->cfunc != NULL) {
254        d->cfunc(d);
255      }
256    }
257  }
258
259  dispatch_active = 0;
260  owl_select_gc();
261}
262
263int owl_select_aim_hack(fd_set *rfds, fd_set *wfds)
264{
265  aim_conn_t *cur;
266  aim_session_t *sess;
267  int max_fd;
268
269  FD_ZERO(rfds);
270  FD_ZERO(wfds);
271  max_fd = 0;
272  sess = owl_global_get_aimsess(&g);
273  for (cur = sess->connlist, max_fd = 0; cur; cur = cur->next) {
274    if (cur->fd != -1) {
275      FD_SET(cur->fd, rfds);
276      if (cur->status & AIM_CONN_STATUS_INPROGRESS) {
277        /* Yes, we're checking writable sockets here. Without it, AIM
278           login is really slow. */
279        FD_SET(cur->fd, wfds);
280      }
281     
282      if (cur->fd > max_fd)
283        max_fd = cur->fd;
284    }
285  }
286  return max_fd;
287}
288
289void owl_select()
290{
291  int i, max_fd, aim_max_fd, aim_done;
292  fd_set r;
293  fd_set e;
294  fd_set aim_rfds, aim_wfds;
295  struct timeval timeout;
296
297  owl_select_process_timers(&timeout);
298
299  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
300
301  /* AIM HACK:
302   *
303   *  The problem - I'm not sure where to hook into the owl/faim
304   *  interface to keep track of when the AIM socket(s) open and
305   *  close. In particular, the bosconn thing throws me off. So,
306   *  rather than register particular dispatchers for AIM, I look up
307   *  the relevant FDs and add them to select's watch lists, then
308   *  check for them individually before moving on to the other
309   *  dispatchers. --asedeno
310   */
311  aim_done = 1;
312  FD_ZERO(&aim_rfds);
313  FD_ZERO(&aim_wfds);
314  if (owl_global_is_doaimevents(&g)) {
315    aim_done = 0;
316    aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds);
317    if (max_fd < aim_max_fd) max_fd = aim_max_fd;
318    for(i = 0; i <= aim_max_fd; i++) {
319      if (FD_ISSET(i, &aim_rfds)) {
320        FD_SET(i, &r);
321        FD_SET(i, &e);
322      }
323    }
324  }
325  /* END AIM HACK */
326
327  if ( select(max_fd+1, &r, &aim_wfds, &e, &timeout) ) {
328    /* Merge fd_sets and clear AIM FDs. */
329    for(i = 0; i <= max_fd; i++) {
330      /* Merge all interesting FDs into one set, since we have a
331         single dispatch per FD. */
332      if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) {
333        /* AIM HACK: no separate dispatch, just process here if
334           needed, and only once per run through. */
335        if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) {
336          owl_process_aim();
337          aim_done = 1;
338        }
339        else {
340          FD_SET(i, &r);
341        }
342      }
343    }
344    /* NOTE: the same dispatch function is called for both exceptional
345       and read ready FDs. */
346    owl_select_dispatch(&r, max_fd);
347  }
348}
Note: See TracBrowser for help on using the repository browser.