source: select.c @ c675b39

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