Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use macro's for event loop hooks

  • Loading branch information...
commit 026d5ae750a576b43a4d76ef7a97e5d46b8158bb 1 parent 3cc6a7f
Pieter Noordhuis pietern authored

Showing 1 changed file with 25 additions and 10 deletions. Show diff stats Hide diff stats

  1. +25 10 async.c
35 async.c
@@ -38,6 +38,22 @@
38 38 #include "dict.c"
39 39 #include "sds.h"
40 40
  41 +#define _EL_ADD_READ(ctx) do { \
  42 + if ((ctx)->ev.addRead) (ctx)->ev.addRead((ctx)->ev.data); \
  43 + } while(0)
  44 +#define _EL_DEL_READ(ctx) do { \
  45 + if ((ctx)->ev.delRead) (ctx)->ev.delRead((ctx)->ev.data); \
  46 + } while(0)
  47 +#define _EL_ADD_WRITE(ctx) do { \
  48 + if ((ctx)->ev.addWrite) (ctx)->ev.addWrite((ctx)->ev.data); \
  49 + } while(0)
  50 +#define _EL_DEL_WRITE(ctx) do { \
  51 + if ((ctx)->ev.delWrite) (ctx)->ev.delWrite((ctx)->ev.data); \
  52 + } while(0)
  53 +#define _EL_CLEANUP(ctx) do { \
  54 + if ((ctx)->ev.cleanup) (ctx)->ev.cleanup((ctx)->ev.data); \
  55 + } while(0);
  56 +
41 57 /* Forward declaration of function in hiredis.c */
42 58 void __redisAppendCommand(redisContext *c, char *cmd, size_t len);
43 59
@@ -143,7 +159,7 @@ int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn
143 159 /* The common way to detect an established connection is to wait for
144 160 * the first write event to be fired. This assumes the related event
145 161 * library functions are already set. */
146   - if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);
  162 + _EL_ADD_WRITE(ac);
147 163 return REDIS_OK;
148 164 }
149 165 return REDIS_ERR;
@@ -231,7 +247,7 @@ static void __redisAsyncFree(redisAsyncContext *ac) {
231 247 dictRelease(ac->sub.patterns);
232 248
233 249 /* Signal event lib to clean up */
234   - if (ac->ev.cleanup) ac->ev.cleanup(ac->ev.data);
  250 + _EL_CLEANUP(ac);
235 251
236 252 /* Execute disconnect callback. When redisAsyncFree() initiated destroying
237 253 * this context, the status will always be REDIS_OK. */
@@ -413,7 +429,7 @@ void redisAsyncHandleRead(redisAsyncContext *ac) {
413 429 __redisAsyncDisconnect(ac);
414 430 } else {
415 431 /* Always re-schedule reads */
416   - if (ac->ev.addRead) ac->ev.addRead(ac->ev.data);
  432 + _EL_ADD_READ(ac);
417 433 redisProcessCallbacks(ac);
418 434 }
419 435 }
@@ -426,14 +442,13 @@ void redisAsyncHandleWrite(redisAsyncContext *ac) {
426 442 __redisAsyncDisconnect(ac);
427 443 } else {
428 444 /* Continue writing when not done, stop writing otherwise */
429   - if (!done) {
430   - if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);
431   - } else {
432   - if (ac->ev.delWrite) ac->ev.delWrite(ac->ev.data);
433   - }
  445 + if (!done)
  446 + _EL_ADD_WRITE(ac);
  447 + else
  448 + _EL_DEL_WRITE(ac);
434 449
435 450 /* Always schedule reads after writes */
436   - if (ac->ev.addRead) ac->ev.addRead(ac->ev.data);
  451 + _EL_ADD_READ(ac);
437 452
438 453 /* Fire onConnect when this is the first write event. */
439 454 if (!(c->flags & REDIS_CONNECTED)) {
@@ -517,7 +532,7 @@ static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void
517 532 __redisAppendCommand(c,cmd,len);
518 533
519 534 /* Always schedule a write when the write buffer is non-empty */
520   - if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);
  535 + _EL_ADD_WRITE(ac);
521 536
522 537 return REDIS_OK;
523 538 }

0 comments on commit 026d5ae

Please sign in to comment.
Something went wrong with that request. Please try again.