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
RevLine 
[9c7a701]1#include "owl.h"
2
3static const char fileIdent[] = "$Id: select.c 894 2008-01-17 07:13:44Z asedeno $";
4
[1895c29]5static int dispatch_active = 0;
6
[58d1f8a]7int _owl_select_timer_cmp(owl_timer *t1, owl_timer *t2) {
[ca1fc33a]8  return t1->time - t2->time;
[b7bb454]9}
10
[58d1f8a]11int _owl_select_timer_eq(owl_timer *t1, owl_timer *t2) {
[ca1fc33a]12  return t1 == t2;
[b7bb454]13}
14
[c675b39]15owl_timer *owl_select_add_timer(int after, int interval, void (*cb)(owl_timer *, void *), void (*destroy)(owl_timer*), void *data)
[b7bb454]16{
[ca1fc33a]17  owl_timer *t = owl_malloc(sizeof(owl_timer));
[58d1f8a]18  GList **timers = owl_global_get_timerlist(&g);
[b7bb454]19
[ca1fc33a]20  t->time = time(NULL) + after;
21  t->interval = interval;
22  t->callback = cb;
[c675b39]23  t->destroy = destroy;
[ca1fc33a]24  t->data = data;
[b7bb454]25
[58d1f8a]26  *timers = g_list_insert_sorted(*timers, t,
27                                 (GCompareFunc)_owl_select_timer_cmp);
[ca1fc33a]28  return t;
[b7bb454]29}
30
31void owl_select_remove_timer(owl_timer *t)
32{
[58d1f8a]33  GList **timers = owl_global_get_timerlist(&g);
34  if (t && g_list_find(*timers, t)) {
35    *timers = g_list_remove(*timers, t);
[c675b39]36    if(t->destroy) {
37      t->destroy(t);
38    }
[ca1fc33a]39    owl_free(t);
40  }
[b7bb454]41}
42
43void owl_select_process_timers(struct timeval *timeout)
44{
[ca1fc33a]45  time_t now = time(NULL);
[58d1f8a]46  GList **timers = owl_global_get_timerlist(&g);
[b7bb454]47
[58d1f8a]48  while(*timers) {
49    owl_timer *t = (*timers)->data;
[c675b39]50    int remove = 0;
[b7bb454]51
[ca1fc33a]52    if(t->time > now)
53      break;
[b7bb454]54
[ca1fc33a]55    /* Reschedule if appropriate */
56    if(t->interval > 0) {
57      t->time = now + t->interval;
[58d1f8a]58      *timers = g_list_remove(*timers, t);
59      *timers = g_list_insert_sorted(*timers, t,
60                                     (GCompareFunc)_owl_select_timer_cmp);
[ca1fc33a]61    } else {
[c675b39]62      remove = 1;
[ca1fc33a]63    }
[b7bb454]64
[ca1fc33a]65    /* Do the callback */
[c675b39]66    t->callback(t, t->data);
67    if(remove) {
68      owl_select_remove_timer(t);
69    }
[ca1fc33a]70  }
[b7bb454]71
[58d1f8a]72  if(*timers) {
73    owl_timer *t = (*timers)->data;
[ca1fc33a]74    timeout->tv_sec = t->time - now;
[58d1f8a]75    if (timeout->tv_sec > 60)
76      timeout->tv_sec = 60;
77  } else {
78    timeout->tv_sec = 60;
[ca1fc33a]79  }
[b7bb454]80
[ca1fc33a]81  timeout->tv_usec = 0;
[b7bb454]82}
83
[9c7a701]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
[1895c29]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
[9c7a701]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
[1895c29]119  d->needs_gc = 0;
120
[9c7a701]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) {
[1895c29]130      owl_select_remove_dispatch_at(elt);
[9c7a701]131    }
132  }
[1895c29]133  owl_list_append_element(dl, d);
[9c7a701]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;
[1895c29]141  owl_dispatch *d;
[9c7a701]142
143  elt = owl_select_find_dispatch(fd);
[1895c29]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);
[9c7a701]149    d = (owl_dispatch*)owl_list_get_element(dl, elt);
[1895c29]150    d->needs_gc = 1;
151  } else {
152    owl_select_remove_dispatch_at(elt);
[9c7a701]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);
[f36cd97]168    if (d->cfunc != owl_perlconfig_dispatch) {
[9c7a701]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;
[f36cd97]176  d->cfunc = owl_perlconfig_dispatch;
177  d->destroy = owl_perlconfig_dispatch_free;
178  d->data = cb;
[9c7a701]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);
[f36cd97]191    if (d->cfunc == owl_perlconfig_dispatch) {
[1895c29]192      owl_select_remove_dispatch_at(elt);
[9c7a701]193      return 0;
194    }
195  }
196  return 1;
197}
198
[2f69081]199int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
[9c7a701]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
[1895c29]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
[9c7a701]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();
[1895c29]245
246  dispatch_active = 1;
247
[9c7a701]248  for(i = 0; i < len; i++) {
249    d = (owl_dispatch*)owl_list_get_element(dl, i);
[18a54ee]250    /* While d shouldn't normally be null, the list may be altered by
251     * functions we dispatch to. */
[1895c29]252    if (d != NULL && !d->needs_gc && FD_ISSET(d->fd, fds)) {
[9c7a701]253      if (d->cfunc != NULL) {
[f36cd97]254        d->cfunc(d);
[9c7a701]255      }
256    }
257  }
[1895c29]258
259  dispatch_active = 0;
260  owl_select_gc();
[9c7a701]261}
262
[2f69081]263int owl_select_aim_hack(fd_set *rfds, fd_set *wfds)
[9c7a701]264{
265  aim_conn_t *cur;
266  aim_session_t *sess;
267  int max_fd;
268
[2f69081]269  FD_ZERO(rfds);
270  FD_ZERO(wfds);
[9c7a701]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) {
[2f69081]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     
[9c7a701]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;
[2f69081]294  fd_set aim_rfds, aim_wfds;
[9c7a701]295  struct timeval timeout;
296
[b7bb454]297  owl_select_process_timers(&timeout);
[9c7a701]298
[2f69081]299  max_fd = owl_select_dispatch_prepare_fd_sets(&r, &e);
[9c7a701]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;
[2f69081]312  FD_ZERO(&aim_rfds);
313  FD_ZERO(&aim_wfds);
[9c7a701]314  if (owl_global_is_doaimevents(&g)) {
315    aim_done = 0;
[2f69081]316    aim_max_fd = owl_select_aim_hack(&aim_rfds, &aim_wfds);
[9c7a701]317    if (max_fd < aim_max_fd) max_fd = aim_max_fd;
318    for(i = 0; i <= aim_max_fd; i++) {
[2f69081]319      if (FD_ISSET(i, &aim_rfds)) {
[9c7a701]320        FD_SET(i, &r);
321        FD_SET(i, &e);
322      }
323    }
324  }
325  /* END AIM HACK */
[2f69081]326
327  if ( select(max_fd+1, &r, &aim_wfds, &e, &timeout) ) {
[9c7a701]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. */
[2f69081]332      if (FD_ISSET(i, &r) || FD_ISSET(i, &aim_wfds) || FD_ISSET(i, &e)) {
[9c7a701]333        /* AIM HACK: no separate dispatch, just process here if
334           needed, and only once per run through. */
[2f69081]335        if (!aim_done && (FD_ISSET(i, &aim_rfds) || FD_ISSET(i, &aim_wfds))) {
[9c7a701]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.