source: select.c @ f36cd97

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since f36cd97 was f36cd97, checked in by Nelson Elhage <nelhage@mit.edu>, 16 years ago
Add a void* data to owl_dispatch and pass it around. Add a void* data field for the use of dispatches, and pass around the owl_dispatch. This allows one dispatch function to be used for multiple fd's, and lets us eliminate perl-specific code from the dispatch loop. In addition, we need to add a destructor to owl_dispatch so that perl dispatches can have their SV* decref'd on destroy.
  • 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->destroy) {
136      d->destroy(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->cfunc != owl_perlconfig_dispatch) {
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 = owl_perlconfig_dispatch;
163  d->destroy = owl_perlconfig_dispatch_free;
164  d->data = cb;
165  owl_select_add_dispatch(d);
166  return 0;
167}
168
169int owl_select_remove_perl_dispatch(int fd)
170{
171  int elt;
172  owl_dispatch *d;
173 
174  elt = owl_select_find_dispatch(fd);
175  if (elt != -1) {
176    d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt);
177    if (d->cfunc == owl_perlconfig_dispatch) {
178      owl_select_remove_dispatch(fd);
179      return 0;
180    }
181  }
182  return 1;
183}
184
185int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e)
186{
187  int i, len, max_fd;
188  owl_dispatch *d;
189  owl_list *dl;
190
191  dl = owl_global_get_dispatchlist(&g);
192  FD_ZERO(r);
193  FD_ZERO(e);
194  max_fd = 0;
195  len = owl_select_dispatch_count(g);
196  for(i = 0; i < len; i++) {
197    d = (owl_dispatch*)owl_list_get_element(dl, i);
198    FD_SET(d->fd, r);
199    FD_SET(d->fd, e);
200    if (max_fd < d->fd) max_fd = d->fd;
201  }
202  return max_fd + 1;
203}
204
205void owl_select_dispatch(fd_set *fds, int max_fd)
206{
207  int i, len;
208  owl_dispatch *d;
209  owl_list *dl;
210
211  dl = owl_global_get_dispatchlist(&g);
212  len = owl_select_dispatch_count();
213  for(i = 0; i < len; i++) {
214    d = (owl_dispatch*)owl_list_get_element(dl, i);
215    /* While d shouldn't normally be null, the list may be altered by
216     * functions we dispatch to. */
217    if (d != NULL && FD_ISSET(d->fd, fds)) {
218      if (d->cfunc != NULL) {
219        d->cfunc(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.