2 * Copyright © 2011 Canonical Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
29 #include "gmessages.h"
30 #include "giochannel.h"
38 wakeup = CreateEvent (NULL, TRUE, FALSE, NULL);
41 g_error ("Cannot create event for GWakeup: %s",
42 g_win32_error_message (GetLastError ()));
44 return (GWakeup *) wakeup;
48 g_wakeup_get_pollfd (GWakeup *wakeup,
51 fd->fd = (gintptr) wakeup;
56 g_wakeup_acknowledge (GWakeup *wakeup)
58 ResetEvent ((HANDLE) wakeup);
62 g_wakeup_signal (GWakeup *wakeup)
64 SetEvent ((HANDLE) wakeup);
68 g_wakeup_free (GWakeup *wakeup)
70 CloseHandle ((HANDLE) wakeup);
75 #include "glib-unix.h"
78 #if defined (HAVE_EVENTFD)
79 #include <sys/eventfd.h>
93 wakeup = g_slice_new (GWakeup);
95 /* try eventfd first, if we think we can */
96 #if defined (HAVE_EVENTFD)
97 wakeup->fds[0] = eventfd (0, EFD_CLOEXEC | EFD_NONBLOCK);
99 if (wakeup->fds[0] != -1)
105 /* for any failure, try a pipe instead */
108 if (!g_unix_open_pipe (wakeup->fds, FD_CLOEXEC, &error))
109 g_error ("Creating pipes for GWakeup: %s\n", error->message);
111 if (!g_unix_set_fd_nonblocking (wakeup->fds[0], TRUE, &error) ||
112 !g_unix_set_fd_nonblocking (wakeup->fds[1], TRUE, &error))
113 g_error ("Set pipes non-blocking for GWakeup: %s\n", error->message);
119 g_wakeup_get_pollfd (GWakeup *wakeup,
122 fd->fd = wakeup->fds[0];
123 fd->events = G_IO_IN;
127 g_wakeup_acknowledge (GWakeup *wakeup)
131 /* read until it is empty */
132 while (read (wakeup->fds[0], buffer, sizeof buffer) == sizeof buffer);
136 g_wakeup_signal (GWakeup *wakeup)
140 if (wakeup->fds[1] == -1)
141 write (wakeup->fds[0], &one, sizeof one);
143 write (wakeup->fds[1], &one, 1);
147 g_wakeup_free (GWakeup *wakeup)
149 close (wakeup->fds[0]);
151 if (wakeup->fds[1] != -1)
152 close (wakeup->fds[1]);
154 g_slice_free (GWakeup, wakeup);