source: select.c @ 86bf047

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