2 * Copyright © 2011 Canonical Limited
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Ryan Lortie <desrt@desrt.ca>
26 /* gwakeup.c is special -- GIO and some test cases include it. As such,
27 * it cannot include other glib headers without triggering the single
28 * includes warnings. We have to manually include its dependencies here
29 * (and at all other use sites).
31 #ifdef GLIB_COMPILATION
43 * `GWakeup` is a simple and portable way of signaling events between
44 * different threads in a way that integrates nicely with g_poll().
45 * GLib uses it internally for cross-thread signalling in the
46 * implementation of #GMainContext and #GCancellable.
48 * You first create a #GWakeup with g_wakeup_new() and initialise a
49 * #GPollFD from it using g_wakeup_get_pollfd(). Polling on the created
50 * #GPollFD will block until g_wakeup_signal() is called, at which point
51 * it will immediately return. Future attempts to poll will continue to
52 * return until g_wakeup_acknowledge() is called. g_wakeup_free() is
53 * used to free a #GWakeup.
55 * On sufficiently modern Linux, this is implemented using eventfd. On
56 * Windows it is implemented using an event handle. On other systems it
57 * is implemented with a pair of pipes.
65 #ifdef GLIB_COMPILATION
66 #include "gmessages.h"
67 #include "giochannel.h"
76 wakeup = CreateEvent (NULL, TRUE, FALSE, NULL);
79 g_error ("Cannot create event for GWakeup: %s",
80 g_win32_error_message (GetLastError ()));
82 return (GWakeup *) wakeup;
86 g_wakeup_get_pollfd (GWakeup *wakeup,
89 poll_fd->fd = (gintptr) wakeup;
90 poll_fd->events = G_IO_IN;
94 g_wakeup_acknowledge (GWakeup *wakeup)
96 ResetEvent ((HANDLE) wakeup);
100 g_wakeup_signal (GWakeup *wakeup)
102 SetEvent ((HANDLE) wakeup);
106 g_wakeup_free (GWakeup *wakeup)
108 CloseHandle ((HANDLE) wakeup);
113 #include "glib-unix.h"
116 #if defined (HAVE_EVENTFD)
117 #include <sys/eventfd.h>
128 * Creates a new #GWakeup.
130 * You should use g_wakeup_free() to free it when you are done.
132 * Returns: a new #GWakeup
139 GError *error = NULL;
142 wakeup = g_slice_new (GWakeup);
144 /* try eventfd first, if we think we can */
145 #if defined (HAVE_EVENTFD)
146 #ifndef TEST_EVENTFD_FALLBACK
147 wakeup->fds[0] = eventfd (0, EFD_CLOEXEC | EFD_NONBLOCK);
152 if (wakeup->fds[0] != -1)
158 /* for any failure, try a pipe instead */
161 if (!g_unix_open_pipe (wakeup->fds, O_CLOEXEC | O_NONBLOCK, &error))
162 g_error ("Creating pipes for GWakeup: %s", error->message);
164 if (!g_unix_set_fd_nonblocking (wakeup->fds[0], TRUE, &error) ||
165 !g_unix_set_fd_nonblocking (wakeup->fds[1], TRUE, &error))
166 g_error ("Set pipes non-blocking for GWakeup: %s", error->message);
172 * g_wakeup_get_pollfd:
173 * @wakeup: a #GWakeup
174 * @poll_fd: a #GPollFD
176 * Prepares a @poll_fd such that polling on it will succeed when
177 * g_wakeup_signal() has been called on @wakeup.
179 * @poll_fd is valid until @wakeup is freed.
184 g_wakeup_get_pollfd (GWakeup *wakeup,
187 poll_fd->fd = wakeup->fds[0];
188 poll_fd->events = G_IO_IN;
192 * g_wakeup_acknowledge:
193 * @wakeup: a #GWakeup
195 * Acknowledges receipt of a wakeup signal on @wakeup.
197 * You must call this after @wakeup polls as ready. If not, it will
198 * continue to poll as ready until you do so.
200 * If you call this function and @wakeup is not signaled, nothing
206 g_wakeup_acknowledge (GWakeup *wakeup)
210 if (wakeup->fds[1] == -1)
214 /* eventfd() read resets counter */
216 res = read (wakeup->fds[0], &value, sizeof (value));
217 while (G_UNLIKELY (res == -1 && errno == EINTR));
223 /* read until it is empty */
225 res = read (wakeup->fds[0], &value, sizeof (value));
226 while (res == sizeof (value) || G_UNLIKELY (res == -1 && errno == EINTR));
232 * @wakeup: a #GWakeup
236 * Any future (or present) polling on the #GPollFD returned by
237 * g_wakeup_get_pollfd() will immediately succeed until such a time as
238 * g_wakeup_acknowledge() is called.
240 * This function is safe to call from a UNIX signal handler.
245 g_wakeup_signal (GWakeup *wakeup)
249 if (wakeup->fds[1] == -1)
253 /* eventfd() case. It requires a 64-bit counter increment value to be
256 res = write (wakeup->fds[0], &one, sizeof one);
257 while (G_UNLIKELY (res == -1 && errno == EINTR));
263 /* Non-eventfd() case. Only a single byte needs to be written, and it can
264 * have an arbitrary value. */
266 res = write (wakeup->fds[1], &one, sizeof one);
267 while (G_UNLIKELY (res == -1 && errno == EINTR));
273 * @wakeup: a #GWakeup
277 * You must not currently be polling on the #GPollFD returned by
278 * g_wakeup_get_pollfd(), or the result is undefined.
281 g_wakeup_free (GWakeup *wakeup)
283 close (wakeup->fds[0]);
285 if (wakeup->fds[1] != -1)
286 close (wakeup->fds[1]);
288 g_slice_free (GWakeup, wakeup);