1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* loop.c Main loop for daemon
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <dbus/dbus-list.h>
26 #include <dbus/dbus-sysdeps.h>
28 static DBusList *watches = NULL;
29 static int watch_list_serial = 0;
30 static dbus_bool_t exited = FALSE;
35 BusWatchFunction function;
37 DBusFreeFunction free_data_func;
41 bus_loop_add_watch (DBusWatch *watch,
42 BusWatchFunction function,
44 DBusFreeFunction free_data_func)
48 cb = dbus_new (WatchCallback, 1);
53 cb->function = function;
55 cb->free_data_func = free_data_func;
57 if (!_dbus_list_append (&watches, cb))
63 watch_list_serial += 1;
69 bus_loop_remove_watch (DBusWatch *watch,
70 BusWatchFunction function,
75 link = _dbus_list_get_first_link (&watches);
78 DBusList *next = _dbus_list_get_next_link (&watches, link);
79 WatchCallback *cb = link->data;
81 if (cb->watch == watch &&
82 cb->function == function &&
85 _dbus_list_remove_link (&watches, link);
87 watch_list_serial += 1;
89 if (cb->free_data_func)
90 (* cb->free_data_func) (cb->data);
99 _dbus_warn ("could not find watch %p function %p data %p to remove\n",
100 watch, function, data);
104 wait_for_memory (void)
106 _dbus_sleep_milliseconds (500);
116 WatchCallback **watches_for_fds;
123 watches_for_fds = NULL;
125 n_fds = _dbus_list_get_length (&watches);
133 fds = dbus_new0 (DBusPollFD, n_fds);
137 fds = dbus_new0 (DBusPollFD, n_fds);
140 watches_for_fds = dbus_new (WatchCallback*, n_fds);
141 while (watches_for_fds == NULL)
144 watches_for_fds = dbus_new (WatchCallback*, n_fds);
148 link = _dbus_list_get_first_link (&watches);
151 DBusList *next = _dbus_list_get_next_link (&watches, link);
152 WatchCallback *cb = link->data;
155 watches_for_fds[i] = cb;
157 flags = dbus_watch_get_flags (cb->watch);
159 fds[i].fd = dbus_watch_get_fd (cb->watch);
160 if (flags & DBUS_WATCH_READABLE)
161 fds[i].events |= _DBUS_POLLIN;
162 if (flags & DBUS_WATCH_WRITABLE)
163 fds[i].events |= _DBUS_POLLOUT;
169 n_ready = _dbus_poll (fds, n_fds, -1);
173 initial_serial = watch_list_serial;
177 /* FIXME I think this "restart if we change the watches"
178 * approach could result in starving watches
179 * toward the end of the list.
181 if (initial_serial != watch_list_serial)
187 if (fds[i].revents != 0)
190 unsigned int condition;
192 cb = watches_for_fds[i];
195 if (fds[i].revents & _DBUS_POLLIN)
196 condition |= DBUS_WATCH_READABLE;
197 if (fds[i].revents & _DBUS_POLLOUT)
198 condition |= DBUS_WATCH_WRITABLE;
199 if (fds[i].revents & _DBUS_POLLHUP)
200 condition |= DBUS_WATCH_HANGUP;
201 if (fds[i].revents & _DBUS_POLLERR)
202 condition |= DBUS_WATCH_ERROR;
204 /* condition may still be 0 if we got some
205 * weird POLLFOO thing like POLLWRBAND
209 (* cb->function) (cb->watch,
220 dbus_free (watches_for_fds);