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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/* $%BEGINLICENSE%$
Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; version 2 of the
License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
$%ENDLICENSE%$ */
#include <glib.h>
#include <errno.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* for write() */
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> /* for SOCK_STREAM and AF_UNIX/AF_INET */
#endif
#ifdef WIN32
#include <winsock2.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h> /* for write, read, _pipe etc */
#include <fcntl.h>
#undef WIN32_LEAN_AND_MEAN
#endif
#include <event.h>
#include "chassis-event-thread.h"
#define C(x) x, sizeof(x) - 1
#ifndef WIN32
#define closesocket(x) close(x)
#endif
/**
* create a new event-op
*
* event-ops are async requests around event_add()
*/
chassis_event_op_t *chassis_event_op_new() {
chassis_event_op_t *e;
e = g_slice_new0(chassis_event_op_t);
return e;
}
/**
* free a event-op
*/
void chassis_event_op_free(chassis_event_op_t *e) {
if (!e) return;
g_slice_free(chassis_event_op_t, e);
}
/**
* execute a event-op on a event-base
*
* @see: chassis_event_add_local(), chassis_threaded_event_op()
*/
void chassis_event_op_apply(chassis_event_op_t *op, struct event_base *event_base) {
switch (op->type) {
case CHASSIS_EVENT_OP_ADD:
event_base_set(event_base, op->ev);
event_add(op->ev, NULL);
break;
case CHASSIS_EVENT_OP_UNSET:
g_assert_not_reached();
break;
}
}
/**
* add a event asynchronously
*
* the event is added to the global event-queue and a fd-notification is sent allowing any
* of the event-threads to handle it
*
* @see network_mysqld_con_handle()
*/
void chassis_event_add(chassis *chas, struct event *ev) {
chassis_event_op_t *op = chassis_event_op_new();
op->type = CHASSIS_EVENT_OP_ADD;
op->ev = ev;
g_async_queue_push(chas->threads->event_queue, op);
send(chas->threads->event_notify_fds[1], C("."), 0); /* ping the event handler */
}
GPrivate *tls_event_base_key = NULL;
/**
* add a event to the current thread
*
* needs event-base stored in the thread local storage
*
* @see network_connection_pool_lua_add_connection()
*/
void chassis_event_add_local(chassis G_GNUC_UNUSED *chas, struct event *ev) {
struct event_base *event_base = ev->ev_base;
chassis_event_op_t *op;
if (!event_base) event_base = g_private_get(tls_event_base_key);
g_assert(event_base); /* the thread-local event-base has to be initialized */
op = chassis_event_op_new();
op->type = CHASSIS_EVENT_OP_ADD;
op->ev = ev;
chassis_event_op_apply(op, event_base);
chassis_event_op_free(op);
}
/**
* handled events sent through the global event-queue
*
* each event-thread has its own listener on the event-queue and
* calls chassis_event_handle() with its own event-base
*
* @see chassis_event_add()
*/
void chassis_event_handle(int G_GNUC_UNUSED event_fd, short G_GNUC_UNUSED events, void *user_data) {
chassis_event_thread_t *event_thread = user_data;
struct event_base *event_base = event_thread->event_base;
chassis *chas = event_thread->chas;
chassis_event_op_t *op;
char ping[1024];
guint received = 0;
gssize removed;
while ((op = g_async_queue_try_pop(chas->threads->event_queue))) {
chassis_event_op_apply(op, event_base);
chassis_event_op_free(op);
received++;
}
/* the pipe has one . per event, remove as many as we received */
while (received > 0 &&
(removed = recv(event_thread->notify_fd, ping, MIN(received, sizeof(ping)), 0)) > 0) {
received -= removed;
}
}
/**
* create the data structure for a new event-thread
*/
chassis_event_thread_t *chassis_event_thread_new() {
chassis_event_thread_t *event_thread;
event_thread = g_new0(chassis_event_thread_t, 1);
return event_thread;
}
/**
* free the data-structures for a event-thread
*
* joins the event-thread, closes notification-pipe and free's the event-base
*/
void chassis_event_thread_free(chassis_event_thread_t *event_thread) {
gboolean is_thread = (event_thread->thr != NULL);
if (!event_thread) return;
if (event_thread->thr) g_thread_join(event_thread->thr);
if (event_thread->notify_fd != -1) {
event_del(&(event_thread->notify_fd_event));
closesocket(event_thread->notify_fd);
}
/* we don't want to free the global event-base */
if (is_thread && event_thread->event_base) event_base_free(event_thread->event_base);
g_free(event_thread);
}
/**
* set the event-based for the current event-thread
*
* @see chassis_event_add_local()
*/
void chassis_event_thread_set_event_base(chassis_event_thread_t G_GNUC_UNUSED *e, struct event_base *event_base) {
g_private_set(tls_event_base_key, event_base);
}
/**
* create the event-threads handler
*
* provides the event-queue that is contains the event_ops from the event-threads
* and notifies all the idling event-threads for the new event-ops to process
*/
chassis_event_threads_t *chassis_event_threads_new() {
chassis_event_threads_t *threads;
tls_event_base_key = g_private_new(NULL);
threads = g_new0(chassis_event_threads_t, 1);
/* create the ping-fds
*
* the event-thread write a byte to the ping-pipe to trigger a fd-event when
* something is available in the event-async-queues
*/
if (0 != evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, threads->event_notify_fds)) {
int err;
#ifdef WIN32
err = WSAGetLastError();
#else
err = errno;
#endif
g_error("%s: evutil_socketpair() failed: %s (%d)",
G_STRLOC,
g_strerror(err),
err);
}
threads->event_threads = g_ptr_array_new();
threads->event_queue = g_async_queue_new();
return threads;
}
/**
* free all event-threads
*
* frees all the registered event-threads and event-queue
*/
void chassis_event_threads_free(chassis_event_threads_t *threads) {
guint i;
chassis_event_op_t *op;
if (!threads) return;
/* all threads are running, now wait until they are down again */
for (i = 0; i < threads->event_threads->len; i++) {
chassis_event_thread_t *event_thread = threads->event_threads->pdata[i];
chassis_event_thread_free(event_thread);
}
g_ptr_array_free(threads->event_threads, TRUE);
/* free the events that are still in the queue */
while ((op = g_async_queue_try_pop(threads->event_queue))) {
chassis_event_op_free(op);
}
g_async_queue_unref(threads->event_queue);
/* close the notification pipe */
if (threads->event_notify_fds[0] != -1) {
closesocket(threads->event_notify_fds[0]);
}
if (threads->event_notify_fds[1] != -1) {
closesocket(threads->event_notify_fds[1]);
}
g_free(threads);
}
/**
* add a event-thread to the event-threads handler
*/
void chassis_event_threads_add(chassis_event_threads_t *threads, chassis_event_thread_t *thread) {
g_ptr_array_add(threads->event_threads, thread);
}
/**
* setup the notification-fd of a event-thread
*
* all event-threads listen on the same notification pipe
*
* @see chassis_event_handle()
*/
int chassis_event_threads_init_thread(chassis_event_threads_t *threads, chassis_event_thread_t *event_thread, chassis *chas) {
#ifdef WIN32
LPWSAPROTOCOL_INFO lpProtocolInfo;
#endif
event_thread->event_base = event_base_new();
event_thread->chas = chas;
#ifdef WIN32
lpProtocolInfo = g_malloc(sizeof(WSAPROTOCOL_INFO));
if (SOCKET_ERROR == WSADuplicateSocket(threads->event_notify_fds[0], GetCurrentProcessId(), lpProtocolInfo)) {
g_error("%s: Could not duplicate socket: %s (%d)", G_STRLOC, g_strerror(WSAGetLastError()), WSAGetLastError());
}
event_thread->notify_fd = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, lpProtocolInfo, 0, 0);
if (INVALID_SOCKET == event_thread->notify_fd) {
g_error("%s: Could not create duplicated socket: %s (%d)", G_STRLOC, g_strerror(WSAGetLastError()), WSAGetLastError());
}
g_free(lpProtocolInfo);
#else
event_thread->notify_fd = dup(threads->event_notify_fds[0]);
#endif
#if 0
evutil_make_socket_nonblocking(event_thread->notify_fd);
#endif
event_set(&(event_thread->notify_fd_event), event_thread->notify_fd, EV_READ | EV_PERSIST, chassis_event_handle, event_thread);
event_base_set(event_thread->event_base, &(event_thread->notify_fd_event));
event_add(&(event_thread->notify_fd_event), NULL);
return 0;
}
/**
* event-handler thread
*
*/
void *chassis_event_thread_loop(chassis_event_thread_t *event_thread) {
chassis_event_thread_set_event_base(event_thread, event_thread->event_base);
/**
* check once a second if we shall shutdown the proxy
*/
while (!chassis_is_shutdown()) {
struct timeval timeout;
int r;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
g_assert(event_base_loopexit(event_thread->event_base, &timeout) == 0);
r = event_base_dispatch(event_thread->event_base);
if (r == -1) {
#ifdef WIN32
errno = WSAGetLastError();
#endif
if (errno == EINTR) continue;
g_critical("%s: leaving chassis_event_thread_loop early, errno != EINTR was: %s (%d)", G_STRLOC, g_strerror(errno), errno);
break;
}
}
return NULL;
}
/**
* start all the event-threads
*
* starts all the event-threads that got added by chassis_event_threads_add()
*
* @see chassis_event_threads_add
*/
void chassis_event_threads_start(chassis_event_threads_t *threads) {
guint i;
g_message("%s: starting %d threads", G_STRLOC, threads->event_threads->len - 1);
for (i = 1; i < threads->event_threads->len; i++) { /* the 1st is the main-thread and already set up */
chassis_event_thread_t *event_thread = threads->event_threads->pdata[i];
GError *gerr = NULL;
event_thread->thr = g_thread_create((GThreadFunc)chassis_event_thread_loop, event_thread, TRUE, &gerr);
if (gerr) {
g_critical("%s: %s", G_STRLOC, gerr->message);
g_error_free(gerr);
gerr = NULL;
}
}
}
|