/
geventfd.c
245 lines (208 loc) · 5.06 KB
/
geventfd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* eventfd GSource wrapper
*
* Copyright Red Hat, Inc. 2011
*
* Authors:
* Paolo Bonzini <pbonzini@redhat.com>
*
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
* See the COPYING.LIB file in the top-level directory.
*
*/
#include "geventfd.h"
#include <glib-object.h>
//#define HAVE_EVENTFD
#ifdef _WIN32
#include <windows.h>
#else
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#ifdef HAVE_EVENTFD
#include <sys/eventfd.h>
#endif
#endif
struct _GEventSource {
GSource source;
GPollFD poll;
#ifndef _WIN32
int wfd;
#endif
};
static gboolean
g_event_prepare (GSource *source,
gint *timeout)
{
*timeout = -1;
return FALSE;
}
static gboolean
g_event_check (GSource *source)
{
GEventSource *evsource = (GEventSource *) source;
return (evsource->poll.revents & G_IO_IN) != 0;
}
static gboolean
g_event_dispatch (GSource *source,
GSourceFunc callback,
gpointer user_data)
{
#ifndef _WIN32
GEventSource *evsource = (GEventSource *) source;
gboolean fire = FALSE;
char buf[512];
int rc;
do {
rc = read(evsource->poll.fd, buf, sizeof(buf));
fire |= (rc > 0);
} while (rc == sizeof (buf) || (rc == -1 && errno == EINTR));
if (rc < 0 && errno != EAGAIN) {
g_warning ("read from eventfd returned %zd: %m\n", rc);
return TRUE;
}
if (!fire)
return FALSE;
#endif
if (callback) {
if (user_data == NULL)
user_data = evsource;
callback (user_data);
} else {
g_warning ("Event source dispatched without callback\n"
"You must call g_source_set_callback().");
}
return FALSE;
}
static void
g_event_finalize (GSource *source)
{
#ifndef _WIN32
GEventSource *evsource = (GEventSource *) source;
close (evsource->poll.fd);
#endif
}
static void
g_event_closure_callback (gpointer data)
{
GClosure *closure = data;
g_closure_invoke (closure, NULL, 0, NULL, NULL);
}
static GSourceFuncs event_source_funcs = {
g_event_prepare,
g_event_check,
g_event_dispatch,
g_event_finalize,
(GSourceFunc) g_event_closure_callback,
(gpointer) g_cclosure_marshal_VOID__VOID
};
GEventSource *
g_event_source_new (void)
{
GEventSource *evsource;
#ifdef _WIN32
HANDLE hEvent;
hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
if (hEvent == NULL) {
return NULL;
}
#else
int rfd, wfd;
#if defined(HAVE_EVENTFD)
rfd = wfd = eventfd(0, 0);
#else
int fds[2];
if (pipe (fds) == -1) {
return NULL;
}
rfd = fds[0];
wfd = fds[1];
fcntl(wfd, F_SETFD, fcntl (wfd, F_GETFD) | FD_CLOEXEC);
fcntl(wfd, F_SETFL, fcntl (wfd, F_GETFL) | O_NONBLOCK);
#endif
if (rfd == -1) {
return NULL;
}
fcntl(rfd, F_SETFL, fcntl (rfd, F_GETFL) | O_NONBLOCK);
fcntl(rfd, F_SETFD, fcntl (rfd, F_GETFD) | FD_CLOEXEC);
#endif
evsource = (GEventSource *) g_source_new (&event_source_funcs,
sizeof (GEventSource));
#ifdef _WIN32
evsource->poll.fd = (LONG_PTR) hEvent;
#else
evsource->wfd = wfd;
evsource->poll.fd = rfd;
#endif
evsource->poll.events = G_IO_IN;
g_source_add_poll (&evsource->source, &evsource->poll);
return evsource;
}
void
g_event_source_notify (GEventSource *evsource)
{
#ifdef _WIN32
SetEvent ((HANDLE) evsource->poll.fd);
#else
guint64 buf = 1;
write (evsource->wfd, (char *) &buf, sizeof (buf));
#endif
}
void
g_event_source_unref (GEventSource *evsource)
{
g_source_unref (&evsource->source);
}
guint
g_event_add (GEventSource **source,
GSourceFunc func,
gpointer user_data)
{
return g_event_add_full (G_PRIORITY_DEFAULT, source, func, user_data, NULL);
}
guint
g_event_add_full (gint priority,
GEventSource **source,
GSourceFunc func,
gpointer user_data,
GDestroyNotify notify)
{
GEventSource *evsource;
guint id;
evsource = g_event_source_new ();
if (priority != G_PRIORITY_DEFAULT)
g_source_set_priority (&evsource->source, priority);
g_source_set_callback (&evsource->source, (GSourceFunc) func,
user_data, notify);
id = g_source_attach (&evsource->source, NULL);
*source = evsource;
return id;
}
#ifdef DEMO
#include <stdio.h>
gpointer wait_and_notify (gpointer user_data)
{
GEventSource *evsource = user_data;
int i;
for (i = 0; i < 3; i++) {
printf (".");
fflush (stdout);
g_usleep (1000000);
}
printf ("\n");
fflush (stdout);
g_event_source_notify (evsource);
g_source_unref ((GSource *) evsource);
return NULL;
}
int main()
{
GEventSource *evsource;
g_thread_init (NULL);
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
g_event_add (&evsource, (GSourceFunc) g_main_loop_quit, loop);
if (g_thread_create (wait_and_notify, evsource, FALSE, NULL) != NULL) {
g_main_loop_run (loop);
}
}
#endif