| 1 |
|
| /* modified version of hiredis's libev adapter */ |
| 2 |
|
| #ifndef __HIREDIS_LIBEV_H__ |
| 3 |
|
| #define __HIREDIS_LIBEV_H__ |
| 4 |
|
| #include "EXTERN.h" |
| 5 |
|
| #include "perl.h" |
| 6 |
|
| #include "XSUB.h" |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| |
| 9 |
|
| #include "EVAPI.h" |
| 10 |
|
| |
| 11 |
|
| #include "hiredis.h" |
| 12 |
|
| #include "async.h" |
| 13 |
|
| |
| 14 |
|
| typedef struct redisLibevEvents { |
| 15 |
|
| redisAsyncContext *context; |
| 16 |
|
| struct ev_loop *loop; |
| 17 |
|
| int reading, writing, timing; |
| 18 |
|
| ev_io rev, wev; |
| 19 |
|
| ev_timer timer; |
| 20 |
|
| } redisLibevEvents; |
| 21 |
|
| |
| 22 |
|
| /* redisAsyncHandle* assert(!REDIS_IN_CALLBACK) — a nested event loop inside |
| 23 |
|
| * a callback would abort(). Skip instead; level-triggered io and the |
| 24 |
|
| * repeating timer re-deliver once the outer callback unwinds. */ |
| 25 |
|
| #define REDIS_LIBEV_IN_CALLBACK(e) ((e)->context->c.flags & REDIS_IN_CALLBACK) |
| 26 |
|
| |
| 27 |
|
| |
| 28 |
0 |
| static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) { |
| 29 |
|
| #if EV_MULTIPLICITY |
| 30 |
|
| ((void)loop); |
| 31 |
|
| #endif |
| 32 |
|
| ((void)revents); |
| 33 |
|
| |
| 34 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)watcher->data; |
| 35 |
0 |
▶ | if (e == NULL || e->context == NULL) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e == NULL || e->context == NULL) return; |
0 |
0 |
0 |
| if (e == NULL || e->context == NULL) return; |
0 |
0 |
0 |
|
| 36 |
0 |
▶ | if (REDIS_LIBEV_IN_CALLBACK(e)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (REDIS_LIBEV_IN_CALLBACK(e)) return; |
0 |
0 |
0 |
|
| 37 |
0 |
| redisAsyncHandleRead(e->context); |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
0 |
| static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) { |
| 41 |
|
| #if EV_MULTIPLICITY |
| 42 |
|
| ((void)loop); |
| 43 |
|
| #endif |
| 44 |
|
| ((void)revents); |
| 45 |
|
| |
| 46 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)watcher->data; |
| 47 |
0 |
▶ | if (e == NULL || e->context == NULL) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e == NULL || e->context == NULL) return; |
0 |
0 |
0 |
| if (e == NULL || e->context == NULL) return; |
0 |
0 |
0 |
|
| 48 |
0 |
▶ | if (REDIS_LIBEV_IN_CALLBACK(e)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (REDIS_LIBEV_IN_CALLBACK(e)) return; |
0 |
0 |
0 |
|
| 49 |
0 |
| redisAsyncHandleWrite(e->context); |
| 50 |
|
| } |
| 51 |
|
| |
| 52 |
0 |
| static void redisLibevAddRead(void *privdata) { |
| 53 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 54 |
|
| struct ev_loop *loop; |
| 55 |
0 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
0 |
0 |
|
| 56 |
0 |
| loop = e->loop; |
| 57 |
0 |
▶ | if (loop == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop == NULL) return; |
0 |
0 |
0 |
|
| 58 |
0 |
▶ | if (!e->reading) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e->reading) { |
0 |
0 |
0 |
|
| 59 |
0 |
| e->reading = 1; |
| 60 |
0 |
| ev_io_start(loop, &e->rev); |
| 61 |
|
| } |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
0 |
| static void redisLibevDelRead(void *privdata) { |
| 65 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 66 |
|
| struct ev_loop *loop; |
| 67 |
0 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
0 |
0 |
|
| 68 |
0 |
| loop = e->loop; |
| 69 |
0 |
▶ | if (e->reading) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->reading) { |
0 |
0 |
0 |
|
| 70 |
0 |
| e->reading = 0; |
| 71 |
0 |
▶ | if (loop != NULL) ev_io_stop(loop, &e->rev); |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop != NULL) ev_io_stop(loop, &e->rev); |
0 |
0 |
0 |
|
| 72 |
|
| } |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
0 |
| static void redisLibevAddWrite(void *privdata) { |
| 76 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 77 |
|
| struct ev_loop *loop; |
| 78 |
0 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
0 |
0 |
|
| 79 |
0 |
| loop = e->loop; |
| 80 |
0 |
▶ | if (loop == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop == NULL) return; |
0 |
0 |
0 |
|
| 81 |
0 |
▶ | if (!e->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e->writing) { |
0 |
0 |
0 |
|
| 82 |
0 |
| e->writing = 1; |
| 83 |
0 |
| ev_io_start(loop, &e->wev); |
| 84 |
|
| } |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
0 |
| static void redisLibevDelWrite(void *privdata) { |
| 88 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 89 |
|
| struct ev_loop *loop; |
| 90 |
0 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
0 |
0 |
|
| 91 |
0 |
| loop = e->loop; |
| 92 |
0 |
▶ | if (e->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->writing) { |
0 |
0 |
0 |
|
| 93 |
0 |
| e->writing = 0; |
| 94 |
0 |
▶ | if (loop != NULL) ev_io_stop(loop, &e->wev); |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop != NULL) ev_io_stop(loop, &e->wev); |
0 |
0 |
0 |
|
| 95 |
|
| } |
| 96 |
|
| } |
| 97 |
|
| |
| 98 |
0 |
| static void redisLibevCleanup(void *privdata) { |
| 99 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 100 |
|
| redisAsyncContext *ctx; |
| 101 |
|
| struct ev_loop *loop; |
| 102 |
0 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
0 |
0 |
|
| 103 |
|
| |
| 104 |
|
| /* Clear ev.data first to prevent double-cleanup if hiredis calls |
| 105 |
|
| * cleanup multiple times (e.g., disconnect then free). */ |
| 106 |
0 |
| ctx = e->context; |
| 107 |
0 |
▶ | if (ctx != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx != NULL) { |
0 |
0 |
0 |
|
| 108 |
0 |
| ctx->ev.data = NULL; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
0 |
| loop = e->loop; |
| 112 |
0 |
| e->loop = NULL; |
| 113 |
0 |
| e->context = NULL; |
| 114 |
|
| |
| 115 |
0 |
| e->rev.data = NULL; |
| 116 |
0 |
| e->wev.data = NULL; |
| 117 |
0 |
| e->timer.data = NULL; |
| 118 |
|
| |
| 119 |
|
| |
| 120 |
0 |
▶ | if (loop != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop != NULL) { |
0 |
0 |
0 |
|
| 121 |
0 |
| ev_io_stop(loop, &e->rev); |
| 122 |
0 |
| ev_io_stop(loop, &e->wev); |
| 123 |
0 |
| ev_timer_stop(loop, &e->timer); |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
0 |
| Safefree(e); |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
0 |
| static void redisLibevTimeout(EV_P_ ev_timer *timer, int revents) { |
| 130 |
|
| #if EV_MULTIPLICITY |
| 131 |
|
| ((void)loop); |
| 132 |
|
| #endif |
| 133 |
|
| ((void)revents); |
| 134 |
|
| |
| 135 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)timer->data; |
| 136 |
0 |
▶ | if (e == NULL || e->context == NULL) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e == NULL || e->context == NULL) return; |
0 |
0 |
0 |
| if (e == NULL || e->context == NULL) return; |
0 |
0 |
0 |
|
| 137 |
0 |
▶ | if (REDIS_LIBEV_IN_CALLBACK(e)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (REDIS_LIBEV_IN_CALLBACK(e)) return; |
0 |
0 |
0 |
|
| 138 |
0 |
| redisAsyncHandleTimeout(e->context); |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
0 |
| static void redisLibevSetTimeout(void *privdata, struct timeval tv) { |
| 142 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 143 |
|
| struct ev_loop *loop; |
| 144 |
0 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
0 |
0 |
|
| 145 |
0 |
| loop = e->loop; |
| 146 |
0 |
▶ | if (loop == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop == NULL) return; |
0 |
0 |
0 |
|
| 147 |
|
| |
| 148 |
0 |
| e->timing = 1; |
| 149 |
0 |
| e->timer.repeat = tv.tv_sec + tv.tv_usec / 1000000.00; |
| 150 |
0 |
| ev_timer_again(loop, &e->timer); |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
0 |
| static int redisLibevAttach(EV_P_ redisAsyncContext *ac) { |
| 154 |
0 |
| redisContext *c = &(ac->c); |
| 155 |
|
| redisLibevEvents *e; |
| 156 |
|
| |
| 157 |
|
| /* Nothing should be attached when something is already attached */ |
| 158 |
0 |
▶ | if (ac->ev.data != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ac->ev.data != NULL) |
0 |
0 |
0 |
|
| 159 |
0 |
| return REDIS_ERR; |
| 160 |
|
| |
| 161 |
|
| /* Create container for context and r/w events */ |
| 162 |
0 |
| Newx(e, 1, redisLibevEvents); |
| 163 |
0 |
| e->context = ac; |
| 164 |
|
| #if EV_MULTIPLICITY |
| 165 |
0 |
| e->loop = loop; |
| 166 |
|
| #else |
| 167 |
|
| #error "EV_MULTIPLICITY is required for EV::Redis libev adapter" |
| 168 |
|
| #endif |
| 169 |
0 |
| e->reading = e->writing = e->timing = 0; |
| 170 |
0 |
| e->rev.data = (void*)e; |
| 171 |
0 |
| e->wev.data = (void*)e; |
| 172 |
|
| |
| 173 |
|
| /* Register functions to start/stop listening for events */ |
| 174 |
0 |
| ac->ev.addRead = redisLibevAddRead; |
| 175 |
0 |
| ac->ev.delRead = redisLibevDelRead; |
| 176 |
0 |
| ac->ev.addWrite = redisLibevAddWrite; |
| 177 |
0 |
| ac->ev.delWrite = redisLibevDelWrite; |
| 178 |
0 |
| ac->ev.cleanup = redisLibevCleanup; |
| 179 |
0 |
| ac->ev.scheduleTimer = redisLibevSetTimeout; |
| 180 |
0 |
| ac->ev.data = e; |
| 181 |
|
| |
| 182 |
|
| /* Initialize read/write events */ |
| 183 |
0 |
| ev_io_init(&e->rev, redisLibevReadEvent, c->fd, EV_READ); |
| 184 |
0 |
| ev_io_init(&e->wev, redisLibevWriteEvent, c->fd, EV_WRITE); |
| 185 |
|
| |
| 186 |
|
| /* Initialize timer (but don't start it) so ev_set_priority is safe */ |
| 187 |
0 |
| ev_init(&e->timer, redisLibevTimeout); |
| 188 |
0 |
| e->timer.data = (void*)e; |
| 189 |
|
| |
| 190 |
0 |
| return REDIS_OK; |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
|
| /* Re-arm (or stop) the timeout timer from "now". hiredis's |
| 194 |
|
| * redisAsyncSetTimeout only stores the value; an already-armed timer keeps |
| 195 |
|
| * its old deadline until the next I/O event re-schedules it. */ |
| 196 |
0 |
| static void redisLibevRefreshTimeout(redisAsyncContext *ac, struct timeval tv) { |
| 197 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)ac->ev.data; |
| 198 |
|
| struct ev_loop *loop; |
| 199 |
0 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
0 |
0 |
|
| 200 |
0 |
| loop = e->loop; |
| 201 |
0 |
▶ | if (loop == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop == NULL) return; |
0 |
0 |
0 |
|
| 202 |
|
| |
| 203 |
0 |
▶ | if (tv.tv_sec || tv.tv_usec) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (tv.tv_sec || tv.tv_usec) { |
0 |
0 |
0 |
| if (tv.tv_sec || tv.tv_usec) { |
0 |
0 |
0 |
|
| 204 |
0 |
| e->timing = 1; |
| 205 |
0 |
| e->timer.repeat = tv.tv_sec + tv.tv_usec / 1000000.00; |
| 206 |
0 |
| ev_timer_again(loop, &e->timer); |
| 207 |
|
| } |
| 208 |
|
| else { |
| 209 |
0 |
| e->timing = 0; |
| 210 |
0 |
| e->timer.repeat = 0.; |
| 211 |
0 |
| ev_timer_stop(loop, &e->timer); |
| 212 |
|
| } |
| 213 |
|
| } |
| 214 |
|
| |
| 215 |
0 |
| static void redisLibevSetPriority(redisAsyncContext *ac, int priority) { |
| 216 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)ac->ev.data; |
| 217 |
|
| struct ev_loop *loop; |
| 218 |
0 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
0 |
0 |
|
| 219 |
|
| |
| 220 |
0 |
| loop = e->loop; |
| 221 |
0 |
▶ | if (loop == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop == NULL) return; |
0 |
0 |
0 |
|
| 222 |
|
| |
| 223 |
|
| /* Stop watchers, set priority, restart if they were running */ |
| 224 |
0 |
▶ | if (e->reading) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->reading) { |
0 |
0 |
0 |
|
| 225 |
0 |
| ev_io_stop(loop, &e->rev); |
| 226 |
0 |
| ev_set_priority(&e->rev, priority); |
| 227 |
0 |
| ev_io_start(loop, &e->rev); |
| 228 |
|
| } else { |
| 229 |
0 |
| ev_set_priority(&e->rev, priority); |
| 230 |
|
| } |
| 231 |
|
| |
| 232 |
0 |
▶ | if (e->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->writing) { |
0 |
0 |
0 |
|
| 233 |
0 |
| ev_io_stop(loop, &e->wev); |
| 234 |
0 |
| ev_set_priority(&e->wev, priority); |
| 235 |
0 |
| ev_io_start(loop, &e->wev); |
| 236 |
|
| } else { |
| 237 |
0 |
| ev_set_priority(&e->wev, priority); |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
0 |
▶ | if (e->timing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->timing) { |
0 |
0 |
0 |
|
| 241 |
0 |
| ev_timer_stop(loop, &e->timer); |
| 242 |
0 |
| ev_set_priority(&e->timer, priority); |
| 243 |
0 |
| ev_timer_again(loop, &e->timer); |
| 244 |
|
| } else { |
| 245 |
0 |
| ev_set_priority(&e->timer, priority); |
| 246 |
|
| } |
| 247 |
|
| } |
| 248 |
|
| |
| 249 |
|
| #endif |