Portál AbcLinuxu, 11. května 2025 09:56
epoll_wait
vyplní poskytnuté pole epoll_event
ů událostmi na registrovaných deskriptorech. Pokud přes epoll_ctl
odeberete nějaký deskriptor, nebude epoll_wait
události k tomu deskriptoru vracet, takže ve vráceném poli epoll_data_t
asociovaný s tím deskriptorem nebude.
man epoll
:
If you use an event cache or store all the file descriptors returned from epoll_wait(2), then make sure to provide a way to mark its closure dynamically (i.e., caused by a previous event's processing). Suppose you receive 100 events from epoll_wait(2), and in event #47 a condition causes event #13 to be closed. If you remove the structure and close(2) the file descriptor for event #13, then your event cache might still say there are events waiting for that file descriptor causing confusion. One solution for this is to call, during the processing of event 47, epoll_ctl(EPOLL_CTL_DEL) to delete file descriptor 13 and close(2), then mark its associated data structure as removed and link it to a cleanup list. If you find another event for file descriptor 13 in your batch processing, you will discover the file descriptor had been previously removed and there will be no confusion.
#include <errno.h> #include <inttypes.h> #include <stdlib.h> #include <sys/epoll.h> #include <unistd.h> typedef struct JRPollable JRPollable; typedef void (*JRPollableReady)(JRPollable*, uint32_t); typedef struct JREpoll JREpoll; struct JRPollable { JRPollableReady jrCallback; int jrFD; uint32_t jrEvents; }; struct JREpoll { int jrEpollFD; int jrEventsAvail; struct epoll_event jrEvents[100]; }; int JREpoll_init(JREpoll *jrEpoll) { jrEpoll->jrEpollFD = epoll_create1(EPOLL_CLOEXEC); if (jrEpoll->jrEpollFD == -1) return errno; jrEpoll->jrEventsAvail = 0; return 0; } int JREpoll_add(JREpoll *jrEpoll, JRPollable *jrPollable) { struct epoll_event ee = { .events = jrPollable->jrEvents, .data = { .ptr = jrPollable } }; if (epoll_ctl(jrEpoll->jrEpollFD, EPOLL_CTL_ADD, jrPollable->jrFD, &ee) != 0) return errno; return 0; } int JREpoll_remove(JREpoll *jrEpoll, JRPollable *jrPollable) { struct epoll_event kernelBugDummyEv_lt2_6_9; if (epoll_ctl(jrEpoll->jrEpollFD, EPOLL_CTL_DEL, jrPollable->jrFD, &kernelBugDummyEv_lt2_6_9) != 0) return errno; int avail = jrEpoll->jrEventsAvail; struct epoll_event *events = jrEpoll->jrEvents; for (int i = 0; i < avail; ++i) { if (events[i].data.ptr == jrPollable) { events[i] = events[avail-1]; --jrEpoll->jrEventsAvail; break; } } return 0; } int JREpoll_destroy(JREpoll *jrEpoll) { if (close(jrEpoll->jrEpollFD) != 0) return errno; return 0; } int JREpoll_wait(JREpoll *jrEpoll) { int ret = epoll_wait(jrEpoll->jrEpollFD, jrEpoll->jrEvents, sizeof(jrEpoll->jrEvents) / sizeof(struct epoll_event), -1); if (ret < 0) { int errnoSave = errno; if (errnoSave != EINTR) return errnoSave; ret = 0; } jrEpoll->jrEventsAvail = ret; return 0; } void JREpoll_dispatch(JREpoll *jrEpoll) { struct epoll_event *events = jrEpoll->jrEvents; for (int avail = jrEpoll->jrEventsAvail; avail > 0; avail = jrEpoll->jrEventsAvail) { jrEpoll->jrEventsAvail = --avail; struct epoll_event ev = events[avail]; JRPollable *pollable = ev.data.ptr; pollable->jrCallback(pollable, ev.events); } }Ta struktura JRPollable může být pak součástí většího objektu, který v příslušném callbacku dostanu po "hrátkách" s offsetof(), nebo přetypováním, pokud je struktura jako první prvek. Schválně jsem to navrhl tímto stylem, abych se vyhnul extra alokacím. Strukturu JRPollable tedy vlastní samotný objekt, nikoliv ten epoll wrapper. Abych dosáhl tedy rychlejšího remove, tak se zdá, že se extra alokacím wrapperům nevyhnu, budu muset udržovat list odebraných položek a ty uvolňovat na konci cyklu, až projdu všechny položky.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.