Use extra field for adapter-specific data

This makes sure that the "data" field on the asynchronous context can be
used for user-specific data.
This commit is contained in:
Pieter Noordhuis 2010-12-01 16:43:21 +01:00
parent 9af1574d6e
commit af7369a253
5 changed files with 16 additions and 12 deletions

View File

@ -72,7 +72,7 @@ int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {
redisAeEvents *e; redisAeEvents *e;
/* Nothing should be attached when something is already attached */ /* Nothing should be attached when something is already attached */
if (ac->data != NULL) if (ac->_adapter_data != NULL)
return REDIS_ERR; return REDIS_ERR;
/* Create container for context and r/w events */ /* Create container for context and r/w events */
@ -88,7 +88,7 @@ int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {
ac->evAddWrite = redisAeAddWrite; ac->evAddWrite = redisAeAddWrite;
ac->evDelWrite = redisAeDelWrite; ac->evDelWrite = redisAeDelWrite;
ac->evCleanup = redisAeCleanup; ac->evCleanup = redisAeCleanup;
ac->data = e; ac->_adapter_data = e;
return REDIS_OK; return REDIS_OK;
} }

View File

@ -82,7 +82,7 @@ int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
redisLibevEvents *e; redisLibevEvents *e;
/* Nothing should be attached when something is already attached */ /* Nothing should be attached when something is already attached */
if (ac->data != NULL) if (ac->_adapter_data != NULL)
return REDIS_ERR; return REDIS_ERR;
/* Create container for context and r/w events */ /* Create container for context and r/w events */
@ -103,7 +103,7 @@ int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
ac->evAddWrite = redisLibevAddWrite; ac->evAddWrite = redisLibevAddWrite;
ac->evDelWrite = redisLibevDelWrite; ac->evDelWrite = redisLibevDelWrite;
ac->evCleanup = redisLibevCleanup; ac->evCleanup = redisLibevCleanup;
ac->data = e; ac->_adapter_data = e;
/* Initialize read/write events */ /* Initialize read/write events */
ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ); ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);

View File

@ -52,7 +52,7 @@ int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
redisLibeventEvents *e; redisLibeventEvents *e;
/* Nothing should be attached when something is already attached */ /* Nothing should be attached when something is already attached */
if (ac->data != NULL) if (ac->_adapter_data != NULL)
return REDIS_ERR; return REDIS_ERR;
/* Create container for context and r/w events */ /* Create container for context and r/w events */
@ -65,7 +65,7 @@ int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {
ac->evAddWrite = redisLibeventAddWrite; ac->evAddWrite = redisLibeventAddWrite;
ac->evDelWrite = redisLibeventDelWrite; ac->evDelWrite = redisLibeventDelWrite;
ac->evCleanup = redisLibeventCleanup; ac->evCleanup = redisLibeventCleanup;
ac->data = e; ac->_adapter_data = e;
/* Initialize and install read/write events */ /* Initialize and install read/write events */
event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e); event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e);

13
async.c
View File

@ -41,6 +41,7 @@ static redisAsyncContext *redisAsyncInitialize(redisContext *c) {
ac->err = 0; ac->err = 0;
ac->errstr = NULL; ac->errstr = NULL;
ac->data = NULL; ac->data = NULL;
ac->_adapter_data = NULL;
ac->evAddRead = NULL; ac->evAddRead = NULL;
ac->evDelRead = NULL; ac->evDelRead = NULL;
ac->evAddWrite = NULL; ac->evAddWrite = NULL;
@ -162,7 +163,7 @@ static void __redisAsyncDisconnect(redisAsyncContext *ac) {
} }
/* Signal event lib to clean up */ /* Signal event lib to clean up */
if (ac->evCleanup) ac->evCleanup(ac->data); if (ac->evCleanup) ac->evCleanup(ac->_adapter_data);
/* Execute callback with proper status */ /* Execute callback with proper status */
if (ac->onDisconnect) ac->onDisconnect(ac,status); if (ac->onDisconnect) ac->onDisconnect(ac,status);
@ -215,7 +216,7 @@ void redisAsyncHandleRead(redisAsyncContext *ac) {
__redisAsyncDisconnect(ac); __redisAsyncDisconnect(ac);
} else { } else {
/* Always re-schedule reads */ /* Always re-schedule reads */
if (ac->evAddRead) ac->evAddRead(ac->data); if (ac->evAddRead) ac->evAddRead(ac->_adapter_data);
redisProcessCallbacks(ac); redisProcessCallbacks(ac);
} }
} }
@ -229,13 +230,13 @@ void redisAsyncHandleWrite(redisAsyncContext *ac) {
} else { } else {
/* Continue writing when not done, stop writing otherwise */ /* Continue writing when not done, stop writing otherwise */
if (!done) { if (!done) {
if (ac->evAddWrite) ac->evAddWrite(ac->data); if (ac->evAddWrite) ac->evAddWrite(ac->_adapter_data);
} else { } else {
if (ac->evDelWrite) ac->evDelWrite(ac->data); if (ac->evDelWrite) ac->evDelWrite(ac->_adapter_data);
} }
/* Always schedule reads when something was written */ /* Always schedule reads when something was written */
if (ac->evAddRead) ac->evAddRead(ac->data); if (ac->evAddRead) ac->evAddRead(ac->_adapter_data);
} }
} }
@ -258,7 +259,7 @@ static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void
__redisPushCallback(&ac->replies,&cb); __redisPushCallback(&ac->replies,&cb);
/* Always schedule a write when the write buffer is non-empty */ /* Always schedule a write when the write buffer is non-empty */
if (ac->evAddWrite) ac->evAddWrite(ac->data); if (ac->evAddWrite) ac->evAddWrite(ac->_adapter_data);
return REDIS_OK; return REDIS_OK;
} }

View File

@ -65,6 +65,9 @@ typedef struct redisAsyncContext {
/* Not used by hiredis */ /* Not used by hiredis */
void *data; void *data;
/* Used by the different event lib adapters to store their private data */
void *_adapter_data;
/* Called when the library expects to start reading/writing. /* Called when the library expects to start reading/writing.
* The supplied functions should be idempotent. */ * The supplied functions should be idempotent. */
void (*evAddRead)(void *privdata); void (*evAddRead)(void *privdata);