2 * libvirt-glib-event.c: libvirt glib integration
4 * Copyright (C) 2008 Daniel P. Berrange
5 * Copyright (C) 2010-2011 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Author: Daniel P. Berrange <berrange@redhat.com>
30 #include <libvirt/libvirt.h>
32 #include "libvirt-glib/libvirt-glib.h"
35 * SECTION:libvirt-glib-event
36 * @short_description: Integrate libvirt with the GMain event framework
39 * @include: libvirt-glib/libvirt-glib.h
41 * The libvirt API has the ability to provide applications with asynchronous
42 * notifications of interesting events. To enable this functionality though,
43 * applications must provide libvirt with an event loop implementation. The
44 * libvirt-glib API provides such an implementation, which naturally integrates
45 * with the GMain event loop framework.
47 * To enable use of the GMain event loop glue, the <code>gvir_event_register()</code>
48 * should be invoked. Once this is done, it is mandatory to have the default
49 * GMain event loop run by a thread in the application, usually the primary
50 * thread, eg by using <code>gtk_main()</code> or <code>g_application_run()</code>
53 * <title>Registering for events with a GTK application</title>
54 * <programlisting><![CDATA[
55 * int main(int argc, char **argv) {
57 * gvir_event_register();
62 * ]]></programlisting>
66 * <title>Registering for events using Appplication</title>
67 * <programlisting><![CDATA[
68 * int main(int argc, char **argv) {
70 * GApplication *app = ...create some impl of GApplication...
71 * gvir_event_register();
73 * g_application_run(app);
76 * ]]></programlisting>
81 #if GLIB_CHECK_VERSION(2, 31, 0)
82 #define g_mutex_new() g_new0(GMutex, 1)
85 struct gvir_event_handle
93 virEventHandleCallback cb;
98 struct gvir_event_timeout
103 virEventTimeoutCallback cb;
108 GMutex *eventlock = NULL;
110 static int nextwatch = 1;
111 static GPtrArray *handles;
113 static int nexttimer = 1;
114 static GPtrArray *timeouts;
117 gvir_event_handle_dispatch(GIOChannel *source G_GNUC_UNUSED,
118 GIOCondition condition,
121 struct gvir_event_handle *data = opaque;
124 if (condition & G_IO_IN)
125 events |= VIR_EVENT_HANDLE_READABLE;
126 if (condition & G_IO_OUT)
127 events |= VIR_EVENT_HANDLE_WRITABLE;
128 if (condition & G_IO_HUP)
129 events |= VIR_EVENT_HANDLE_HANGUP;
130 if (condition & G_IO_ERR)
131 events |= VIR_EVENT_HANDLE_ERROR;
133 g_debug("Dispatch handler %d %d %p\n", data->fd, events, data->opaque);
135 (data->cb)(data->watch, data->fd, events, data->opaque);
142 gvir_event_handle_add(int fd,
144 virEventHandleCallback cb,
148 struct gvir_event_handle *data;
149 GIOCondition cond = 0;
152 g_mutex_lock(eventlock);
154 data = g_new0(struct gvir_event_handle, 1);
156 if (events & VIR_EVENT_HANDLE_READABLE)
158 if (events & VIR_EVENT_HANDLE_WRITABLE)
161 data->watch = nextwatch++;
163 data->events = events;
165 data->opaque = opaque;
166 data->channel = g_io_channel_unix_new(fd);
169 g_debug("Add handle %d %d %p\n", data->fd, events, data->opaque);
171 data->source = g_io_add_watch(data->channel,
173 gvir_event_handle_dispatch,
176 g_ptr_array_add(handles, data);
180 g_mutex_unlock(eventlock);
185 static struct gvir_event_handle *
186 gvir_event_handle_find(int watch, guint *idx)
190 for (i = 0 ; i < handles->len ; i++) {
191 struct gvir_event_handle *h = g_ptr_array_index(handles, i);
194 g_warn_if_reached ();
198 if (h->watch == watch) {
209 gvir_event_handle_update(int watch,
212 struct gvir_event_handle *data;
214 g_mutex_lock(eventlock);
216 data = gvir_event_handle_find(watch, NULL);
218 g_debug("Update for missing handle watch %d", watch);
223 GIOCondition cond = 0;
224 if (events == data->events)
228 g_source_remove(data->source);
231 if (events & VIR_EVENT_HANDLE_READABLE)
233 if (events & VIR_EVENT_HANDLE_WRITABLE)
235 data->source = g_io_add_watch(data->channel,
237 gvir_event_handle_dispatch,
239 data->events = events;
244 g_source_remove(data->source);
250 g_mutex_unlock(eventlock);
254 _event_handle_remove(gpointer data)
256 struct gvir_event_handle *h = data;
261 g_ptr_array_remove_fast(handles, h);
267 gvir_event_handle_remove(int watch)
269 struct gvir_event_handle *data;
273 g_mutex_lock(eventlock);
275 data = gvir_event_handle_find(watch, &idx);
277 g_debug("Remove of missing watch %d", watch);
281 g_debug("Remove handle %d %d\n", watch, data->fd);
286 g_source_remove(data->source);
289 g_idle_add(_event_handle_remove, data);
294 g_mutex_unlock(eventlock);
300 gvir_event_timeout_dispatch(void *opaque)
302 struct gvir_event_timeout *data = opaque;
303 g_debug("Dispatch timeout %p %p %d %p\n", data, data->cb, data->timer, data->opaque);
304 (data->cb)(data->timer, data->opaque);
310 gvir_event_timeout_add(int interval,
311 virEventTimeoutCallback cb,
315 struct gvir_event_timeout *data;
318 g_mutex_lock(eventlock);
320 data = g_new0(struct gvir_event_timeout, 1);
321 data->timer = nexttimer++;
322 data->interval = interval;
324 data->opaque = opaque;
327 data->source = g_timeout_add(interval,
328 gvir_event_timeout_dispatch,
331 g_ptr_array_add(timeouts, data);
333 g_debug("Add timeout %p %d %p %p %d\n", data, interval, cb, opaque, data->timer);
337 g_mutex_unlock(eventlock);
343 static struct gvir_event_timeout *
344 gvir_event_timeout_find(int timer, guint *idx)
348 g_return_val_if_fail(timeouts != NULL, NULL);
350 for (i = 0 ; i < timeouts->len ; i++) {
351 struct gvir_event_timeout *t = g_ptr_array_index(timeouts, i);
354 g_warn_if_reached ();
358 if (t->timer == timer) {
370 gvir_event_timeout_update(int timer,
373 struct gvir_event_timeout *data;
375 g_mutex_lock(eventlock);
377 data = gvir_event_timeout_find(timer, NULL);
379 g_debug("Update of missing timer %d", timer);
383 g_debug("Update timeout %p %d %d\n", data, timer, interval);
389 data->interval = interval;
390 data->source = g_timeout_add(data->interval,
391 gvir_event_timeout_dispatch,
397 g_source_remove(data->source);
402 g_mutex_unlock(eventlock);
406 _event_timeout_remove(gpointer data)
408 struct gvir_event_timeout *t = data;
413 g_ptr_array_remove_fast(timeouts, t);
419 gvir_event_timeout_remove(int timer)
421 struct gvir_event_timeout *data;
425 g_mutex_lock(eventlock);
427 data = gvir_event_timeout_find(timer, &idx);
429 g_debug("Remove of missing timer %d", timer);
433 g_debug("Remove timeout %p %d\n", data, timer);
438 g_source_remove(data->source);
440 g_idle_add(_event_timeout_remove, data);
445 g_mutex_unlock(eventlock);
450 static gpointer event_register_once(gpointer data G_GNUC_UNUSED)
452 eventlock = g_mutex_new();
453 timeouts = g_ptr_array_new_with_free_func(g_free);
454 handles = g_ptr_array_new_with_free_func(g_free);
455 virEventRegisterImpl(gvir_event_handle_add,
456 gvir_event_handle_update,
457 gvir_event_handle_remove,
458 gvir_event_timeout_add,
459 gvir_event_timeout_update,
460 gvir_event_timeout_remove);
466 * gvir_event_register:
468 * Registers a libvirt event loop implementation that is backed
469 * by the default <code>GMain</code> context. If invoked more
470 * than once this method will be a no-op. Applications should,
471 * however, take care not to register any another non-GLib
472 * event loop with libvirt.
474 * After invoking this method, it is mandatory to run the
475 * default GMain event loop. Typically this can be satisfied
476 * by invoking <code>gtk_main</code> or <code>g_application_run</code>
477 * in the application's main thread. Failure to run the event
478 * loop will mean no libvirt events get dispatched, and the
479 * libvirt keepalive timer will kill off libvirt connections
482 void gvir_event_register(void)
484 static GOnce once = G_ONCE_INIT;
486 g_once(&once, event_register_once, NULL);