File Coverage

src/libev_adapter.h
Criterion Covered Total %
statement 0 52 0.0
branch 0 14 0.0
condition n/a
subroutine n/a
pod n/a
total 0 66 0.0


line stmt bran cond sub pod time code
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
10             #include
11              
12             #include "EVAPI.h"
13              
14             #include "hiredis.h"
15             #include "async.h"
16              
17             typedef struct redisLibevEvents {
18             redisAsyncContext *context;
19             struct ev_loop *loop;
20             int reading, writing;
21             ev_io rev, wev;
22             } redisLibevEvents;
23              
24 0           static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) {
25             #if EV_MULTIPLICITY
26             ((void)loop);
27             #endif
28             ((void)revents);
29              
30 0           redisLibevEvents *e = (redisLibevEvents*)watcher->data;
31 0           redisAsyncHandleRead(e->context);
32 0           }
33              
34 0           static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) {
35             #if EV_MULTIPLICITY
36             ((void)loop);
37             #endif
38             ((void)revents);
39              
40 0           redisLibevEvents *e = (redisLibevEvents*)watcher->data;
41 0           redisAsyncHandleWrite(e->context);
42 0           }
43              
44 0           static void redisLibevAddRead(void *privdata) {
45             redisLibevEvents *e = (redisLibevEvents*)privdata;
46 0           struct ev_loop *loop = e->loop;
47             ((void)loop);
48 0 0         if (!e->reading) {
49 0           e->reading = 1;
50 0           ev_io_start(loop, &e->rev);
51             }
52 0           }
53              
54 0           static void redisLibevDelRead(void *privdata) {
55             redisLibevEvents *e = (redisLibevEvents*)privdata;
56 0           struct ev_loop *loop = e->loop;
57             ((void)loop);
58 0 0         if (e->reading) {
    0          
59 0           e->reading = 0;
60 0           ev_io_stop(loop, &e->rev);
61             }
62 0           }
63              
64 0           static void redisLibevAddWrite(void *privdata) {
65             redisLibevEvents *e = (redisLibevEvents*)privdata;
66 0           struct ev_loop *loop = e->loop;
67             ((void)loop);
68 0 0         if (!e->writing) {
69 0           e->writing = 1;
70 0           ev_io_start(loop, &e->wev);
71             }
72 0           }
73              
74 0           static void redisLibevDelWrite(void *privdata) {
75             redisLibevEvents *e = (redisLibevEvents*)privdata;
76 0           struct ev_loop *loop = e->loop;
77             ((void)loop);
78 0 0         if (e->writing) {
    0          
79 0           e->writing = 0;
80 0           ev_io_stop(loop, &e->wev);
81             }
82 0           }
83              
84 0           static void redisLibevCleanup(void *privdata) {
85             redisLibevEvents *e = (redisLibevEvents*)privdata;
86             redisLibevDelRead(privdata);
87             redisLibevDelWrite(privdata);
88 0           free(e);
89 0           }
90              
91 0           static int redisLibevAttach(EV_P_ redisAsyncContext *ac) {
92             redisContext *c = &(ac->c);
93             redisLibevEvents *e;
94              
95             /* Nothing should be attached when something is already attached */
96 0 0         if (ac->ev.data != NULL)
97             return REDIS_ERR;
98              
99             /* Create container for context and r/w events */
100 0           e = (redisLibevEvents*)malloc(sizeof(*e));
101 0           e->context = ac;
102             #if EV_MULTIPLICITY
103 0           e->loop = loop;
104             #else
105             e->loop = NULL;
106             #endif
107 0           e->reading = e->writing = 0;
108 0           e->rev.data = (SV*)e;
109 0           e->wev.data = (SV*)e;
110              
111             /* Register functions to start/stop listening for events */
112 0           ac->ev.addRead = redisLibevAddRead;
113 0           ac->ev.delRead = redisLibevDelRead;
114 0           ac->ev.addWrite = redisLibevAddWrite;
115 0           ac->ev.delWrite = redisLibevDelWrite;
116 0           ac->ev.cleanup = redisLibevCleanup;
117 0           ac->ev.data = e;
118              
119             /* Initialize read/write events */
120 0           ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);
121 0           ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE);
122 0           return REDIS_OK;
123             }
124              
125             #endif