|
| 1 | +#include "hevent.h" |
| 2 | + |
| 3 | +#ifdef EVENT_EPOLL |
| 4 | +#include "hplatform.h" |
| 5 | +#ifdef OS_LINUX |
| 6 | +#include <sys/epoll.h> |
| 7 | +#endif |
| 8 | + |
| 9 | +#include "hdef.h" |
| 10 | + |
| 11 | +#define INIT_EVENTS_NUM 64 |
| 12 | + |
| 13 | +typedef struct epoll_ctx_s { |
| 14 | + int epfd; |
| 15 | + int capacity; |
| 16 | + int nevents; |
| 17 | + struct epoll_event* events; |
| 18 | +} epoll_ctx_t; |
| 19 | + |
| 20 | +static void epoll_ctx_resize(epoll_ctx_t* epoll_ctx, int size) { |
| 21 | + int bytes = sizeof(struct epoll_event) * size; |
| 22 | + epoll_ctx->events = (struct epoll_event*)realloc(epoll_ctx->events, bytes); |
| 23 | + epoll_ctx->capacity = size; |
| 24 | +} |
| 25 | + |
| 26 | +int _event_init(hloop_t* loop) { |
| 27 | + if (loop->event_ctx) return 0; |
| 28 | + epoll_ctx_t* epoll_ctx = (epoll_ctx_t*)malloc(sizeof(epoll_ctx_t)); |
| 29 | + epoll_ctx->epfd = epoll_create(INIT_EVENTS_NUM); |
| 30 | + epoll_ctx->capacity = INIT_EVENTS_NUM; |
| 31 | + epoll_ctx->nevents = 0; |
| 32 | + int bytes = sizeof(struct epoll_event) * epoll_ctx->capacity; |
| 33 | + epoll_ctx->events = (struct epoll_event*)malloc(bytes); |
| 34 | + memset(epoll_ctx->events, 0, bytes); |
| 35 | + loop->event_ctx = epoll_ctx; |
| 36 | + return 0; |
| 37 | +} |
| 38 | + |
| 39 | +int _event_cleanup(hloop_t* loop) { |
| 40 | + if (loop->event_ctx == NULL) return 0; |
| 41 | + epoll_ctx_t* epoll_ctx = (epoll_ctx_t*)loop->event_ctx; |
| 42 | + close(epoll_ctx->epfd); |
| 43 | + SAFE_FREE(epoll_ctx->events); |
| 44 | + SAFE_FREE(loop->event_ctx); |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +int _add_event(hevent_t* event, int type) { |
| 49 | + hloop_t* loop = event->loop; |
| 50 | + if (loop->event_ctx == NULL) { |
| 51 | + hloop_event_init(loop); |
| 52 | + } |
| 53 | + epoll_ctx_t* epoll_ctx = (epoll_ctx_t*)loop->event_ctx; |
| 54 | + int op = event->events == 0 ? EPOLL_CTL_ADD : EPOLL_CTL_MOD; |
| 55 | + if (type & READ_EVENT) { |
| 56 | + event->events |= EPOLLIN; |
| 57 | + } |
| 58 | + if (type & WRITE_EVENT) { |
| 59 | + event->events |= EPOLLOUT; |
| 60 | + } |
| 61 | + struct epoll_event ee; |
| 62 | + ee.events = event->events; |
| 63 | + ee.data.fd = event->fd; |
| 64 | + epoll_ctl(epoll_ctx->epfd, op, event->fd, &ee); |
| 65 | + if (op == EPOLL_CTL_ADD) { |
| 66 | + if (epoll_ctx->nevents == epoll_ctx->capacity) { |
| 67 | + epoll_ctx_resize(epoll_ctx, epoll_ctx->capacity*2); |
| 68 | + } |
| 69 | + epoll_ctx->nevents++; |
| 70 | + } |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +int _del_event(hevent_t* event, int type) { |
| 75 | + hloop_t* loop = event->loop; |
| 76 | + epoll_ctx_t* epoll_ctx = (epoll_ctx_t*)loop->event_ctx; |
| 77 | + if (epoll_ctx == NULL) return 0; |
| 78 | + |
| 79 | + if (event->events == 0) return 0; |
| 80 | + if (type & READ_EVENT) { |
| 81 | + event->events &= ~EPOLLIN; |
| 82 | + } |
| 83 | + if (type & WRITE_EVENT) { |
| 84 | + event->events &= ~EPOLLOUT; |
| 85 | + } |
| 86 | + int op = event->events == 0 ? EPOLL_CTL_DEL : EPOLL_CTL_MOD; |
| 87 | + struct epoll_event ee; |
| 88 | + ee.events = event->events; |
| 89 | + ee.data.fd = event->fd; |
| 90 | + epoll_ctl(epoll_ctx->epfd, op, event->fd, &ee); |
| 91 | + if (op == EPOLL_CTL_DEL) { |
| 92 | + epoll_ctx->nevents--; |
| 93 | + } |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +int _handle_events(hloop_t* loop, int timeout) { |
| 98 | + epoll_ctx_t* epoll_ctx = (epoll_ctx_t*)loop->event_ctx; |
| 99 | + if (epoll_ctx == NULL) return 0; |
| 100 | + if (epoll_ctx->nevents == 0) return 0; |
| 101 | + int nepoll = epoll_wait(epoll_ctx->epfd, epoll_ctx->events, epoll_ctx->nevents, timeout); |
| 102 | + if (nepoll < 0) { |
| 103 | + perror("epoll"); |
| 104 | + return nepoll; |
| 105 | + } |
| 106 | + if (nepoll == 0) return 0; |
| 107 | + int nevent = 0; |
| 108 | + for (int i = 0; i < epoll_ctx->nevents; ++i) { |
| 109 | + if (nevent == nepoll) break; |
| 110 | + int fd = epoll_ctx->events[i].data.fd; |
| 111 | + uint32_t revents = epoll_ctx->events[i].events; |
| 112 | + if (revents) { |
| 113 | + ++nevent; |
| 114 | + auto iter = loop->events.find(fd); |
| 115 | + if (iter == loop->events.end()) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + hevent_t* event = iter->second; |
| 119 | + if (revents & EPOLLIN) { |
| 120 | + _on_read(event); |
| 121 | + } |
| 122 | + if (revents & EPOLLOUT) { |
| 123 | + _on_write(event); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return nevent; |
| 128 | +} |
| 129 | +#endif |
0 commit comments