aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/deps/hiredis/adapters/libuv.h
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/deps/hiredis/adapters/libuv.h')
-rw-r--r--examples/redis-unstable/deps/hiredis/adapters/libuv.h171
1 files changed, 0 insertions, 171 deletions
diff --git a/examples/redis-unstable/deps/hiredis/adapters/libuv.h b/examples/redis-unstable/deps/hiredis/adapters/libuv.h
deleted file mode 100644
index 268edab..0000000
--- a/examples/redis-unstable/deps/hiredis/adapters/libuv.h
+++ /dev/null
@@ -1,171 +0,0 @@
1#ifndef __HIREDIS_LIBUV_H__
2#define __HIREDIS_LIBUV_H__
3#include <stdlib.h>
4#include <uv.h>
5#include "../hiredis.h"
6#include "../async.h"
7#include <string.h>
8
9typedef struct redisLibuvEvents {
10 redisAsyncContext* context;
11 uv_poll_t handle;
12 uv_timer_t timer;
13 int events;
14} redisLibuvEvents;
15
16
17static void redisLibuvPoll(uv_poll_t* handle, int status, int events) {
18 redisLibuvEvents* p = (redisLibuvEvents*)handle->data;
19 int ev = (status ? p->events : events);
20
21 if (p->context != NULL && (ev & UV_READABLE)) {
22 redisAsyncHandleRead(p->context);
23 }
24 if (p->context != NULL && (ev & UV_WRITABLE)) {
25 redisAsyncHandleWrite(p->context);
26 }
27}
28
29
30static void redisLibuvAddRead(void *privdata) {
31 redisLibuvEvents* p = (redisLibuvEvents*)privdata;
32
33 if (p->events & UV_READABLE) {
34 return;
35 }
36
37 p->events |= UV_READABLE;
38
39 uv_poll_start(&p->handle, p->events, redisLibuvPoll);
40}
41
42
43static void redisLibuvDelRead(void *privdata) {
44 redisLibuvEvents* p = (redisLibuvEvents*)privdata;
45
46 p->events &= ~UV_READABLE;
47
48 if (p->events) {
49 uv_poll_start(&p->handle, p->events, redisLibuvPoll);
50 } else {
51 uv_poll_stop(&p->handle);
52 }
53}
54
55
56static void redisLibuvAddWrite(void *privdata) {
57 redisLibuvEvents* p = (redisLibuvEvents*)privdata;
58
59 if (p->events & UV_WRITABLE) {
60 return;
61 }
62
63 p->events |= UV_WRITABLE;
64
65 uv_poll_start(&p->handle, p->events, redisLibuvPoll);
66}
67
68
69static void redisLibuvDelWrite(void *privdata) {
70 redisLibuvEvents* p = (redisLibuvEvents*)privdata;
71
72 p->events &= ~UV_WRITABLE;
73
74 if (p->events) {
75 uv_poll_start(&p->handle, p->events, redisLibuvPoll);
76 } else {
77 uv_poll_stop(&p->handle);
78 }
79}
80
81static void on_timer_close(uv_handle_t *handle) {
82 redisLibuvEvents* p = (redisLibuvEvents*)handle->data;
83 p->timer.data = NULL;
84 if (!p->handle.data) {
85 // both timer and handle are closed
86 hi_free(p);
87 }
88 // else, wait for `on_handle_close`
89}
90
91static void on_handle_close(uv_handle_t *handle) {
92 redisLibuvEvents* p = (redisLibuvEvents*)handle->data;
93 p->handle.data = NULL;
94 if (!p->timer.data) {
95 // timer never started, or timer already destroyed
96 hi_free(p);
97 }
98 // else, wait for `on_timer_close`
99}
100
101// libuv removed `status` parameter since v0.11.23
102// see: https://github.com/libuv/libuv/blob/v0.11.23/include/uv.h
103#if (UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR < 11) || \
104 (UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR == 11 && UV_VERSION_PATCH < 23)
105static void redisLibuvTimeout(uv_timer_t *timer, int status) {
106 (void)status; // unused
107#else
108static void redisLibuvTimeout(uv_timer_t *timer) {
109#endif
110 redisLibuvEvents *e = (redisLibuvEvents*)timer->data;
111 redisAsyncHandleTimeout(e->context);
112}
113
114static void redisLibuvSetTimeout(void *privdata, struct timeval tv) {
115 redisLibuvEvents* p = (redisLibuvEvents*)privdata;
116
117 uint64_t millsec = tv.tv_sec * 1000 + tv.tv_usec / 1000.0;
118 if (!p->timer.data) {
119 // timer is uninitialized
120 if (uv_timer_init(p->handle.loop, &p->timer) != 0) {
121 return;
122 }
123 p->timer.data = p;
124 }
125 // updates the timeout if the timer has already started
126 // or start the timer
127 uv_timer_start(&p->timer, redisLibuvTimeout, millsec, 0);
128}
129
130static void redisLibuvCleanup(void *privdata) {
131 redisLibuvEvents* p = (redisLibuvEvents*)privdata;
132
133 p->context = NULL; // indicate that context might no longer exist
134 if (p->timer.data) {
135 uv_close((uv_handle_t*)&p->timer, on_timer_close);
136 }
137 uv_close((uv_handle_t*)&p->handle, on_handle_close);
138}
139
140
141static int redisLibuvAttach(redisAsyncContext* ac, uv_loop_t* loop) {
142 redisContext *c = &(ac->c);
143
144 if (ac->ev.data != NULL) {
145 return REDIS_ERR;
146 }
147
148 ac->ev.addRead = redisLibuvAddRead;
149 ac->ev.delRead = redisLibuvDelRead;
150 ac->ev.addWrite = redisLibuvAddWrite;
151 ac->ev.delWrite = redisLibuvDelWrite;
152 ac->ev.cleanup = redisLibuvCleanup;
153 ac->ev.scheduleTimer = redisLibuvSetTimeout;
154
155 redisLibuvEvents* p = (redisLibuvEvents*)hi_malloc(sizeof(*p));
156 if (p == NULL)
157 return REDIS_ERR;
158
159 memset(p, 0, sizeof(*p));
160
161 if (uv_poll_init_socket(loop, &p->handle, c->fd) != 0) {
162 return REDIS_ERR;
163 }
164
165 ac->ev.data = p;
166 p->handle.data = p;
167 p->context = ac;
168
169 return REDIS_OK;
170}
171#endif