diff options
Diffstat (limited to 'examples/redis-unstable/deps/hiredis/adapters/libsdevent.h')
| -rw-r--r-- | examples/redis-unstable/deps/hiredis/adapters/libsdevent.h | 177 |
1 files changed, 0 insertions, 177 deletions
diff --git a/examples/redis-unstable/deps/hiredis/adapters/libsdevent.h b/examples/redis-unstable/deps/hiredis/adapters/libsdevent.h deleted file mode 100644 index 1268ed9..0000000 --- a/examples/redis-unstable/deps/hiredis/adapters/libsdevent.h +++ /dev/null | |||
| @@ -1,177 +0,0 @@ | |||
| 1 | #ifndef HIREDIS_LIBSDEVENT_H | ||
| 2 | #define HIREDIS_LIBSDEVENT_H | ||
| 3 | #include <systemd/sd-event.h> | ||
| 4 | #include "../hiredis.h" | ||
| 5 | #include "../async.h" | ||
| 6 | |||
| 7 | #define REDIS_LIBSDEVENT_DELETED 0x01 | ||
| 8 | #define REDIS_LIBSDEVENT_ENTERED 0x02 | ||
| 9 | |||
| 10 | typedef struct redisLibsdeventEvents { | ||
| 11 | redisAsyncContext *context; | ||
| 12 | struct sd_event *event; | ||
| 13 | struct sd_event_source *fdSource; | ||
| 14 | struct sd_event_source *timerSource; | ||
| 15 | int fd; | ||
| 16 | short flags; | ||
| 17 | short state; | ||
| 18 | } redisLibsdeventEvents; | ||
| 19 | |||
| 20 | static void redisLibsdeventDestroy(redisLibsdeventEvents *e) { | ||
| 21 | if (e->fdSource) { | ||
| 22 | e->fdSource = sd_event_source_disable_unref(e->fdSource); | ||
| 23 | } | ||
| 24 | if (e->timerSource) { | ||
| 25 | e->timerSource = sd_event_source_disable_unref(e->timerSource); | ||
| 26 | } | ||
| 27 | sd_event_unref(e->event); | ||
| 28 | hi_free(e); | ||
| 29 | } | ||
| 30 | |||
| 31 | static int redisLibsdeventTimeoutHandler(sd_event_source *s, uint64_t usec, void *userdata) { | ||
| 32 | ((void)s); | ||
| 33 | ((void)usec); | ||
| 34 | redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; | ||
| 35 | redisAsyncHandleTimeout(e->context); | ||
| 36 | return 0; | ||
| 37 | } | ||
| 38 | |||
| 39 | static int redisLibsdeventHandler(sd_event_source *s, int fd, uint32_t event, void *userdata) { | ||
| 40 | ((void)s); | ||
| 41 | ((void)fd); | ||
| 42 | redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; | ||
| 43 | e->state |= REDIS_LIBSDEVENT_ENTERED; | ||
| 44 | |||
| 45 | #define CHECK_DELETED() if (e->state & REDIS_LIBSDEVENT_DELETED) {\ | ||
| 46 | redisLibsdeventDestroy(e);\ | ||
| 47 | return 0; \ | ||
| 48 | } | ||
| 49 | |||
| 50 | if ((event & EPOLLIN) && e->context && (e->state & REDIS_LIBSDEVENT_DELETED) == 0) { | ||
| 51 | redisAsyncHandleRead(e->context); | ||
| 52 | CHECK_DELETED(); | ||
| 53 | } | ||
| 54 | |||
| 55 | if ((event & EPOLLOUT) && e->context && (e->state & REDIS_LIBSDEVENT_DELETED) == 0) { | ||
| 56 | redisAsyncHandleWrite(e->context); | ||
| 57 | CHECK_DELETED(); | ||
| 58 | } | ||
| 59 | |||
| 60 | e->state &= ~REDIS_LIBSDEVENT_ENTERED; | ||
| 61 | #undef CHECK_DELETED | ||
| 62 | |||
| 63 | return 0; | ||
| 64 | } | ||
| 65 | |||
| 66 | static void redisLibsdeventAddRead(void *userdata) { | ||
| 67 | redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; | ||
| 68 | |||
| 69 | if (e->flags & EPOLLIN) { | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | e->flags |= EPOLLIN; | ||
| 74 | |||
| 75 | if (e->flags & EPOLLOUT) { | ||
| 76 | sd_event_source_set_io_events(e->fdSource, e->flags); | ||
| 77 | } else { | ||
| 78 | sd_event_add_io(e->event, &e->fdSource, e->fd, e->flags, redisLibsdeventHandler, e); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | static void redisLibsdeventDelRead(void *userdata) { | ||
| 83 | redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; | ||
| 84 | |||
| 85 | e->flags &= ~EPOLLIN; | ||
| 86 | |||
| 87 | if (e->flags) { | ||
| 88 | sd_event_source_set_io_events(e->fdSource, e->flags); | ||
| 89 | } else { | ||
| 90 | e->fdSource = sd_event_source_disable_unref(e->fdSource); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | static void redisLibsdeventAddWrite(void *userdata) { | ||
| 95 | redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; | ||
| 96 | |||
| 97 | if (e->flags & EPOLLOUT) { | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | |||
| 101 | e->flags |= EPOLLOUT; | ||
| 102 | |||
| 103 | if (e->flags & EPOLLIN) { | ||
| 104 | sd_event_source_set_io_events(e->fdSource, e->flags); | ||
| 105 | } else { | ||
| 106 | sd_event_add_io(e->event, &e->fdSource, e->fd, e->flags, redisLibsdeventHandler, e); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | static void redisLibsdeventDelWrite(void *userdata) { | ||
| 111 | redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; | ||
| 112 | |||
| 113 | e->flags &= ~EPOLLOUT; | ||
| 114 | |||
| 115 | if (e->flags) { | ||
| 116 | sd_event_source_set_io_events(e->fdSource, e->flags); | ||
| 117 | } else { | ||
| 118 | e->fdSource = sd_event_source_disable_unref(e->fdSource); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | static void redisLibsdeventCleanup(void *userdata) { | ||
| 123 | redisLibsdeventEvents *e = (redisLibsdeventEvents*)userdata; | ||
| 124 | |||
| 125 | if (!e) { | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | if (e->state & REDIS_LIBSDEVENT_ENTERED) { | ||
| 130 | e->state |= REDIS_LIBSDEVENT_DELETED; | ||
| 131 | } else { | ||
| 132 | redisLibsdeventDestroy(e); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | static void redisLibsdeventSetTimeout(void *userdata, struct timeval tv) { | ||
| 137 | redisLibsdeventEvents *e = (redisLibsdeventEvents *)userdata; | ||
| 138 | |||
| 139 | uint64_t usec = tv.tv_sec * 1000000 + tv.tv_usec; | ||
| 140 | if (!e->timerSource) { | ||
| 141 | sd_event_add_time_relative(e->event, &e->timerSource, CLOCK_MONOTONIC, usec, 1, redisLibsdeventTimeoutHandler, e); | ||
| 142 | } else { | ||
| 143 | sd_event_source_set_time_relative(e->timerSource, usec); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | static int redisLibsdeventAttach(redisAsyncContext *ac, struct sd_event *event) { | ||
| 148 | redisContext *c = &(ac->c); | ||
| 149 | redisLibsdeventEvents *e; | ||
| 150 | |||
| 151 | /* Nothing should be attached when something is already attached */ | ||
| 152 | if (ac->ev.data != NULL) | ||
| 153 | return REDIS_ERR; | ||
| 154 | |||
| 155 | /* Create container for context and r/w events */ | ||
| 156 | e = (redisLibsdeventEvents*)hi_calloc(1, sizeof(*e)); | ||
| 157 | if (e == NULL) | ||
| 158 | return REDIS_ERR; | ||
| 159 | |||
| 160 | /* Initialize and increase event refcount */ | ||
| 161 | e->context = ac; | ||
| 162 | e->event = event; | ||
| 163 | e->fd = c->fd; | ||
| 164 | sd_event_ref(event); | ||
| 165 | |||
| 166 | /* Register functions to start/stop listening for events */ | ||
| 167 | ac->ev.addRead = redisLibsdeventAddRead; | ||
| 168 | ac->ev.delRead = redisLibsdeventDelRead; | ||
| 169 | ac->ev.addWrite = redisLibsdeventAddWrite; | ||
| 170 | ac->ev.delWrite = redisLibsdeventDelWrite; | ||
| 171 | ac->ev.cleanup = redisLibsdeventCleanup; | ||
| 172 | ac->ev.scheduleTimer = redisLibsdeventSetTimeout; | ||
| 173 | ac->ev.data = e; | ||
| 174 | |||
| 175 | return REDIS_OK; | ||
| 176 | } | ||
| 177 | #endif | ||
