GWakeup: make it private API
[platform/upstream/glib.git] / glib / gwakeup.c
1 /*
2  * Copyright © 2011 Canonical Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24
25 /* gwakeup.h is special -- GIO and some test cases include it.  As such,
26  * it cannot include other glib headers without triggering the single
27  * includes warnings.  We have to manually include its dependencies here
28  * (and at all other use sites).
29  */
30 #ifdef GLIB_COMPILATION
31 #include "gtypes.h"
32 #include "gpoll.h"
33 #else
34 #include <glib.h>
35 #endif
36
37 #include "gwakeup.h"
38
39 /**
40  * SECTION:gwakeup
41  * @title: GWakeup
42  * @short_description: portable cross-thread event signal mechanism
43  *
44  * #GWakeup is a simple and portable way of signaling events between
45  * different threads in a way that integrates nicely with g_poll().
46  * GLib uses it internally for cross-thread signalling in the
47  * implementation of #GMainContext and #GCancellable.
48  *
49  * You first create a #GWakeup with g_wakeup_new() and initialise a
50  * #GPollFD from it using g_wakeup_get_pollfd().  Polling on the created
51  * #GPollFD will block until g_wakeup_signal() is called, at which point
52  * it will immediately return.  Future attempts to poll will continue to
53  * return until g_wakeup_acknowledge() is called.  g_wakeup_free() is
54  * used to free a #GWakeup.
55  *
56  * On sufficiently modern Linux, this is implemented using eventfd.  On
57  * Windows it is implemented using an event handle.  On other systems it
58  * is implemented with a pair of pipes.
59  *
60  * Since: 2.30
61  **/
62 #ifdef _WIN32
63
64 #include <windows.h>
65 #include "gmessages.h"
66 #include "giochannel.h"
67 #include "gwin32.h"
68
69 GWakeup *
70 g_wakeup_new (void)
71 {
72   HANDLE wakeup;
73
74   wakeup = CreateEvent (NULL, TRUE, FALSE, NULL);
75
76   if (wakeup == NULL)
77     g_error ("Cannot create event for GWakeup: %s",
78              g_win32_error_message (GetLastError ()));
79
80   return (GWakeup *) wakeup;
81 }
82
83 void
84 g_wakeup_get_pollfd (GWakeup *wakeup,
85                      GPollFD *poll_fd)
86 {
87   poll_fd->fd = (gintptr) wakeup;
88   poll_fd->events = G_IO_IN;
89 }
90
91 void
92 g_wakeup_acknowledge (GWakeup *wakeup)
93 {
94   ResetEvent ((HANDLE) wakeup);
95 }
96
97 void
98 g_wakeup_signal (GWakeup *wakeup)
99 {
100   SetEvent ((HANDLE) wakeup);
101 }
102
103 void
104 g_wakeup_free (GWakeup *wakeup)
105 {
106   CloseHandle ((HANDLE) wakeup);
107 }
108
109 #else
110
111 #include "glib-unix.h"
112 #include <fcntl.h>
113
114 #if defined (HAVE_EVENTFD)
115 #include <sys/eventfd.h>
116 #endif
117
118 struct _GWakeup
119 {
120   gint fds[2];
121 };
122
123 /**
124  * g_wakeup_new:
125  *
126  * Creates a new #GWakeup.
127  *
128  * You should use g_wakeup_free() to free it when you are done.
129  *
130  * Returns: a new #GWakeup
131  *
132  * Since: 2.30
133  **/
134 GWakeup *
135 g_wakeup_new (void)
136 {
137   GError *error = NULL;
138   GWakeup *wakeup;
139
140   wakeup = g_slice_new (GWakeup);
141
142   /* try eventfd first, if we think we can */
143 #if defined (HAVE_EVENTFD)
144 #ifndef TEST_EVENTFD_FALLBACK
145   wakeup->fds[0] = eventfd (0, EFD_CLOEXEC | EFD_NONBLOCK);
146 #else
147   wakeup->fds[0] = -1;
148 #endif
149
150   if (wakeup->fds[0] != -1)
151     {
152       wakeup->fds[1] = -1;
153       return wakeup;
154     }
155
156   /* for any failure, try a pipe instead */
157 #endif
158
159   if (!g_unix_open_pipe (wakeup->fds, FD_CLOEXEC, &error))
160     g_error ("Creating pipes for GWakeup: %s\n", error->message);
161
162   if (!g_unix_set_fd_nonblocking (wakeup->fds[0], TRUE, &error) ||
163       !g_unix_set_fd_nonblocking (wakeup->fds[1], TRUE, &error))
164     g_error ("Set pipes non-blocking for GWakeup: %s\n", error->message);
165
166   return wakeup;
167 }
168
169 /**
170  * g_wakeup_get_pollfd:
171  * @wakeup: a #GWakeup
172  * @poll_fd: a #GPollFD
173  *
174  * Prepares a @poll_fd such that polling on it will succeed when
175  * g_wakeup_signal() has been called on @wakeup.
176  *
177  * @poll_fd is valid until @wakeup is freed.
178  *
179  * Since: 2.30
180  **/
181 void
182 g_wakeup_get_pollfd (GWakeup *wakeup,
183                      GPollFD *poll_fd)
184 {
185   poll_fd->fd = wakeup->fds[0];
186   poll_fd->events = G_IO_IN;
187 }
188
189 /**
190  * g_wakeup_acknowledge:
191  * @wakeup: a #GWakeup
192  *
193  * Acknowledges receipt of a wakeup signal on @wakeup.
194  *
195  * You must call this after @wakeup polls as ready.  If not, it will
196  * continue to poll as ready until you do so.
197  *
198  * If you call this function and @wakeup is not signaled, nothing
199  * happens.
200  *
201  * Since: 2.30
202  **/
203 void
204 g_wakeup_acknowledge (GWakeup *wakeup)
205 {
206   char buffer[16];
207
208   /* read until it is empty */
209   while (read (wakeup->fds[0], buffer, sizeof buffer) == sizeof buffer);
210 }
211
212 /**
213  * g_wakeup_signal:
214  * @wakeup: a #GWakeup
215  *
216  * Signals @wakeup.
217  *
218  * Any future (or present) polling on the #GPollFD returned by
219  * g_wakeup_get_pollfd() will immediately succeed until such a time as
220  * g_wakeup_acknowledge() is called.
221  *
222  * This function is safe to call from a UNIX signal handler.
223  *
224  * Since: 2.30
225  **/
226 void
227 g_wakeup_signal (GWakeup *wakeup)
228 {
229   guint64 one = 1;
230
231   if (wakeup->fds[1] == -1)
232     write (wakeup->fds[0], &one, sizeof one);
233   else
234     write (wakeup->fds[1], &one, 1);
235 }
236
237 /**
238  * g_wakeup_free:
239  * @wakeup: a #GWakeup
240  *
241  * Frees @wakeup.
242  *
243  * You must not currently be polling on the #GPollFD returned by
244  * g_wakeup_get_pollfd(), or the result is undefined.
245  **/
246 void
247 g_wakeup_free (GWakeup *wakeup)
248 {
249   close (wakeup->fds[0]);
250
251   if (wakeup->fds[1] != -1)
252     close (wakeup->fds[1]);
253
254   g_slice_free (GWakeup, wakeup);
255 }
256
257 #endif /* !_WIN32 */