-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-libevent.c
513 lines (445 loc) · 11.9 KB
/
lib-libevent.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* Copyright Neil Brown ©2015-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* libevent support for edlib.
*
* Register command "attach-libevent".
* When that is called, register:
* "event:read"
* "event:write"
* "event:signal"
* "event:run"
* "event:deactivate"
*
* When "event:deactivate" is called, cause event:run to abort.
*/
#include <stdlib.h>
#include <unistd.h>
#include <event.h>
#include <string.h>
#define PANE_DATA_TYPE struct event_info
#include "core.h"
#include "misc.h"
enum {
EV_LIST, /* Events handled by libevent */
POLL_LIST, /* Events to poll before calling event_base_loop */
PRIO_0_LIST, /* background task - run one per loop */
PRIO_1_LIST, /* non-trivial follow-up tasks, line pane_refresh */
PRIO_2_LIST, /* fast follow-up tasks like freeing memory */
NR_LISTS
};
struct event_info {
struct event_base *base;
struct list_head event_list[NR_LISTS];
struct pane *home safe;
int dont_block;
int deactivated;
struct command read, write, signal, timer, poll, on_idle,
run, deactivate, free, refresh, noblock;
};
#include "core-pane.h"
static struct map *libevent_map;
DEF_LOOKUP_CMD(libevent_handle, libevent_map);
struct evt {
struct event *l;
struct pane *home safe;
char *event safe;
struct command *comm safe;
struct list_head lst;
int active; /* Don't delete or free this event, it is running */
int num; /* signal or mseconds or fd */
};
static void call_event(int thing, short sev, void *evv)
{
struct evt *ev safe = safe_cast evv;
int type;
if (sev & EV_SIGNAL)
type = TIME_SIG;
else
type = TIME_READ;
ev->active = 1;
time_start(type);
if (comm_call(ev->comm, "callback:event", ev->home, thing) < 0 ||
ev->active == 2) {
event_del(ev->l);
event_free(ev->l);
list_del(&ev->lst);
command_put(ev->comm);
free(ev);
} else
ev->active = 0;
time_stop(type);
}
static void call_timeout_event(int thing, short sev, void *evv)
{
struct evt *ev safe = safe_cast evv;
ev->active = 1;
time_start(TIME_TIMER);
if (comm_call(ev->comm, "callback:event", ev->home, thing) < 0 ||
ev->active == 2) {
event_free(ev->l);
list_del(&ev->lst);
command_put(ev->comm);
free(ev);
} else {
struct timeval tv;
tv.tv_sec = ev->num / 1000;
tv.tv_usec = (ev->num % 1000) * 1000;
ev->active = 0;
event_add(ev->l, &tv);
}
time_stop(TIME_TIMER);
}
DEF_CB(libevent_read)
{
struct event_info *ei = container_of(ci->comm, struct event_info, read);
struct evt *ev;
if (!ci->comm2)
return Enoarg;
/* If there is already an event with this 'fd', we need
* to remove it now, else libevent gets confused.
* Presumably call_event() is now running and will clean up
* soon.
*/
list_for_each_entry(ev, &ei->event_list[EV_LIST], lst) {
int fd = event_get_fd(ev->l);
if (fd >= 0 && ci->num >= 0 && fd == ci->num)
event_del(ev->l);
}
ev = malloc(sizeof(*ev));
if (!ei->base)
ei->base = event_base_new();
ev->l = safe_cast event_new(ei->base, ci->num, EV_READ|EV_PERSIST,
call_event, ev);
ev->home = ci->focus;
ev->comm = command_get(ci->comm2);
ev->num = ci->num;
ev->active = 0;
ev->event = "event:read";
pane_add_notify(ei->home, ev->home, "Notify:Close");
list_add(&ev->lst, &ei->event_list[EV_LIST]);
event_add(ev->l, NULL);
return 1;
}
DEF_CB(libevent_write)
{
struct event_info *ei = container_of(ci->comm, struct event_info, write);
struct evt *ev;
if (!ci->comm2)
return Enoarg;
/* If there is already an event with this 'fd', we need
* to remove it now, else libevent gets confused.
* Presumably call_event() is now running and will clean up
* soon.
*/
list_for_each_entry(ev, &ei->event_list[EV_LIST], lst) {
int fd = event_get_fd(ev->l);
if (fd >= 0 && ci->num >= 0 && fd == ci->num)
event_del(ev->l);
}
ev = malloc(sizeof(*ev));
if (!ei->base)
ei->base = event_base_new();
ev->l = safe_cast event_new(ei->base, ci->num, EV_WRITE|EV_PERSIST,
call_event, ev);
ev->home = ci->focus;
ev->comm = command_get(ci->comm2);
ev->num = ci->num;
ev->active = 0;
ev->event = "event:write";
pane_add_notify(ei->home, ev->home, "Notify:Close");
list_add(&ev->lst, &ei->event_list[EV_LIST]);
event_add(ev->l, NULL);
return 1;
}
DEF_CB(libevent_signal)
{
struct event_info *ei = container_of(ci->comm, struct event_info, signal);
struct evt *ev;
if (!ci->comm2)
return Enoarg;
ev = malloc(sizeof(*ev));
if (!ei->base)
ei->base = event_base_new();
ev->l = safe_cast event_new(ei->base, ci->num, EV_SIGNAL|EV_PERSIST,
call_event, ev);
ev->home = ci->focus;
ev->comm = command_get(ci->comm2);
ev->num = ci->num;
ev->active = 0;
ev->event = "event:signal";
pane_add_notify(ei->home, ev->home, "Notify:Close");
list_add(&ev->lst, &ei->event_list[EV_LIST]);
event_add(ev->l, NULL);
return 1;
}
DEF_CB(libevent_timer)
{
struct event_info *ei = container_of(ci->comm, struct event_info, timer);
struct evt *ev;
struct timeval tv;
if (!ci->comm2)
return Enoarg;
ev = malloc(sizeof(*ev));
if (!ei->base)
ei->base = event_base_new();
ev->l = safe_cast event_new(ei->base, -1, 0,
call_timeout_event, ev);
ev->home = ci->focus;
ev->comm = command_get(ci->comm2);
ev->num = ci->num;
ev->active = 0;
ev->event = "event:timer";
pane_add_notify(ei->home, ev->home, "Notify:Close");
list_add(&ev->lst, &ei->event_list[EV_LIST]);
tv.tv_sec = ev->num / 1000;
tv.tv_usec = (ev->num % 1000) * 1000;
event_add(ev->l, &tv);
return 1;
}
DEF_CB(libevent_poll)
{
struct event_info *ei = container_of(ci->comm, struct event_info, poll);
struct evt *ev;
if (!ci->comm2)
return Enoarg;
ev = malloc(sizeof(*ev));
if (!ei->base)
ei->base = event_base_new();
ev->home = ci->focus;
ev->comm = command_get(ci->comm2);
ev->active = 0;
ev->event = "event:poll";
ev->num = -1;
pane_add_notify(ei->home, ev->home, "Notify:Close");
list_add(&ev->lst, &ei->event_list[POLL_LIST]);
return 1;
}
DEF_CMD(libevent_on_idle)
{
struct event_info *ei = container_of(ci->comm, struct event_info, on_idle);
struct evt *ev;
int prio = ci->num;
if (!ci->comm2)
return Enoarg;
ev = malloc(sizeof(*ev));
if (!ei->base)
ei->base = event_base_new();
ev->home = ci->focus;
pane_add_notify(ei->home, ev->home, "Notify:Close");
ev->comm = command_get(ci->comm2);
ev->active = 0;
ev->event = "event:on-idle";
if (prio < 0)
prio = 0;
if (prio > 2)
prio = 2;
ev->num = prio;
list_add(&ev->lst, &ei->event_list[PRIO_0_LIST + prio]);
return 1;
}
static int run_list(struct event_info *ei safe, int list, char *cb safe,
bool stop_on_first)
{
bool dont_block = False;
struct evt *ev;
list_for_each_entry(ev, &ei->event_list[list], lst) {
ev->active = 1;
if (comm_call(ev->comm, cb, ev->home, ev->num) >= 1)
dont_block = True;
if (ev->active == 2 || list >= PRIO_0_LIST) {
list_del(&ev->lst);
command_put(ev->comm);
free(ev);
break;
} else
ev->active = 0;
if (dont_block && stop_on_first)
/* Other things might have been removed from list */
break;
}
return dont_block;
}
DEF_CB(libevent_run)
{
struct event_info *ei = container_of(ci->comm, struct event_info, run);
struct event_base *b = ei->base;
int dont_block = ei->dont_block;
struct evt *ev;
int i;
ei->dont_block = 0;
if (ei->deactivated)
return Efallthrough;
if (!b) {
/* No events to wait for.. */
if (dont_block)
return 1;
return 0;
}
/* First run any 'poll' events */
if (run_list(ei, POLL_LIST, "callback:poll", True))
dont_block = True;
for (i = PRIO_0_LIST ; i <= PRIO_2_LIST; i++)
if (!list_empty(&ei->event_list[i]))
dont_block = 1;
/* Disable any alarm set by python (or other interpreter) */
alarm(0);
event_base_loop(b, EVLOOP_ONCE | (dont_block ? EVLOOP_NONBLOCK : 0));
time_start(TIME_IDLE);
/* Prio 2 comes first - unconditional */
run_list(ei, PRIO_2_LIST, "callback:on-idle", False);
/* Now prio1 */
run_list(ei, PRIO_1_LIST, "callback:on-idle", False);
/* Repeat PRIO_2 just in case */
run_list(ei, PRIO_2_LIST, "callback:on-idle", False);
/* And do one background task */
run_list(ei, PRIO_0_LIST, "callback:on-idle", True);
time_stop(TIME_IDLE);
/* Check if we have been deactivated. */
if (ei->base == b)
return 1;
for (i = 0 ; i < NR_LISTS; i++) {
while (!list_empty(&ei->event_list[i])) {
ev = list_first_entry(&ei->event_list[i], struct evt, lst);
list_del(&ev->lst);
if (i == EV_LIST) {
event_del(ev->l);
event_free(ev->l);
}
command_put(ev->comm);
free(ev);
}
}
event_base_free(b);
return Efail;
}
DEF_CB(libevent_deactivate)
{
struct event_info *ei = container_of(ci->comm, struct event_info, deactivate);
ei->base = NULL;
ei->deactivated = 1;
return 1;
}
DEF_CB(libevent_free)
{
/* destroy for ci->focus and, if comm2 given, which activate
* comm2
*/
struct evt *ev;
struct list_head *tmp;
struct event_info *ei = container_of(ci->comm, struct event_info, free);
int i;
for (i = 0; i < NR_LISTS; i++) {
list_for_each_entry_safe(ev, tmp, &ei->event_list[i], lst)
if (ev->home == ci->focus &&
(ci->comm2 == NULL || ev->comm == ci->comm2)) {
list_del_init(&ev->lst);
if (ev->active)
ev->active = 2;
else {
if (i == EV_LIST) {
event_del(ev->l);
event_free(ev->l);
}
command_put(ev->comm);
free(ev);
}
}
}
return 1;
}
DEF_CB(libevent_refresh)
{
struct evt *ev;
struct list_head *tmp;
struct event_info *ei = container_of(ci->comm, struct event_info, refresh);
struct list_head old;
int i;
for (i = 0; i < NR_LISTS; i++) {
list_add(&old, &ei->event_list[i]);
list_del_init(&ei->event_list[i]);
list_for_each_entry_safe(ev, tmp, &old, lst) {
if (i == EV_LIST) {
event_del(ev->l);
event_free(ev->l);
}
list_del(&ev->lst);
call_comm(ev->event, ev->home, ev->comm, ev->num);
command_put(ev->comm);
free(ev);
}
}
return Efallthrough;
}
DEF_CB(libevent_noblock)
{
struct event_info *ei = container_of(ci->comm, struct event_info,
noblock);
ei->dont_block = 1;
return 1;
}
DEF_CMD(libevent_notify)
{
struct event_info *ei = ci->home->data;
home_comm_call(ci->home, &ei->free, "free", ci->focus);
return 1;
}
DEF_CMD(libevent_activate)
{
struct event_info *ei;
struct pane *p;
int i;
p = pane_register(pane_root(ci->home), 0, &libevent_handle.c);
if (!p)
return Efail;
ei = p->data;
ei->home = p;
for (i = 0; i < NR_LISTS; i++)
INIT_LIST_HEAD(&ei->event_list[i]);
ei->read = libevent_read;
ei->write = libevent_write;
ei->signal = libevent_signal;
ei->timer = libevent_timer;
ei->poll = libevent_poll;
ei->on_idle = libevent_on_idle;
ei->run = libevent_run;
ei->deactivate = libevent_deactivate;
ei->free = libevent_free;
ei->refresh = libevent_refresh;
ei->noblock = libevent_noblock;
/* These are defaults, so make them sort late */
call_comm("global-set-command", ci->focus, &ei->read,
0, NULL, "event:read-zz");
call_comm("global-set-command", ci->focus, &ei->write,
0, NULL, "event:write-zz");
call_comm("global-set-command", ci->focus, &ei->signal,
0, NULL, "event:signal-zz");
call_comm("global-set-command", ci->focus, &ei->timer,
0, NULL, "event:timer-zz");
call_comm("global-set-command", ci->focus, &ei->poll,
0, NULL, "event:poll-zz");
call_comm("global-set-command", ci->focus, &ei->on_idle,
0, NULL, "event:on-idle-zz");
call_comm("global-set-command", ci->focus, &ei->run,
0, NULL, "event:run-zz");
call_comm("global-set-command", ci->focus, &ei->deactivate,
0, NULL, "event:deactivate-zz");
call_comm("global-set-command", ci->focus, &ei->free,
0, NULL, "event:free-zz");
call_comm("global-set-command", ci->focus, &ei->refresh,
0, NULL, "event:refresh-zz");
call_comm("global-set-command", ci->focus, &ei->noblock,
0, NULL, "event:noblock-zz");
call("event:refresh", ci->focus);
return 1;
}
void edlib_init(struct pane *ed safe)
{
call_comm("global-set-command", ed, &libevent_activate,
0, NULL, "attach-libevent");
if (libevent_map)
return;
libevent_map = key_alloc();
key_add(libevent_map, "Notify:Close", &libevent_notify);
}