-
Notifications
You must be signed in to change notification settings - Fork 18
/
EIO.c
224 lines (163 loc) · 4.29 KB
/
EIO.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#define EIO_REQ_MEMBERS \
zval *callback; \
zval *zfd; \
int buflen; \
char *buf;
#include "libeio/eio.h"
#include "libeio/eio.c"
/*
* Stores callback, fd and increases their refcount.
* buffer will be assigned to the req->buf, and bufferlen
* to req->buflen. If buflen != 0 then buf will be freed in
* req_done().
*/
#define STORE_REQ(buffer, bufferlen) \
do { /* Save callback and file descriptor, \
freed in eio_func_done() */ \
zval_add_ref(&callback); \
zval_add_ref(fd); \
/* Increase refcount on the loop, to prevent it from exiting while still waiting \
for calls to be finished. Coupled with an ev_unref() in req_done() */ \
ev_ref(eio_loop); \
\
req->buflen = bufferlen; \
req->buf = buffer; \
req->callback = callback; \
req->zfd = *fd; } while(0)
zend_class_entry *eio_ce;
static struct ev_loop *eio_loop = NULL;
static struct ev_idle eio_poll_watcher;
static struct ev_async eio_ready_watcher;
static void eio_repeat_poll(struct ev_loop *loop, ev_idle *w, int revents)
{
int i = eio_poll();
if(i != -1)
{
IF_DEBUG(libev_printf("stopping eio_poll_watcher()\n"));
ev_idle_stop(loop, w);
}
libev_printf("eio_poll(): %d\n", i);
}
static void eio_ready(struct ev_loop *loop, ev_async *w, int revents)
{
IF_DEBUG(libev_printf("eio_ready() "));
int i = eio_poll();
if(i == -1)
{
IF_DEBUG(php_printf("starting eio_poll_watcher"));
ev_idle_start(eio_loop, &eio_poll_watcher);
}
IF_DEBUG(php_printf("\n"));
libev_printf("eio_poll(): %d\n", i);
}
static void eio_want_poll()
{
IF_DEBUG(libev_printf("eio_want_poll()\n"));
ev_async_send(eio_loop, &eio_ready_watcher);
}
PHP_METHOD(EIO, init)
{
if(eio_loop)
{
RETURN_BOOL(0);
}
/* TODO: Allow any other loop? */
eio_loop = EV_DEFAULT;
ev_idle_init(&eio_poll_watcher, eio_repeat_poll);
ev_async_init(&eio_ready_watcher, eio_ready);
ev_async_start(eio_loop, &eio_ready_watcher);
ev_unref(eio_loop);
eio_init(eio_want_poll, 0);
RETURN_BOOL(1);
}
static int req_done(eio_req *req)
{
zval *args[2];
zval retval;
MAKE_STD_ZVAL(args[0]);
MAKE_STD_ZVAL(args[1]);
ZVAL_LONG(args[1], req->errorno);
switch(req->type)
{
case EIO_READ:
if(req->result == -1)
{
ZVAL_BOOL(args[0], 0);
}
else
{
ZVAL_STRINGL(args[0], req->buf, req->result, 1);
}
break;
default:
ZVAL_LONG(args[0], req->result);
}
if(call_user_function(EG(function_table), NULL, req->callback,
&retval, 2, args TSRMLS_CC) == SUCCESS)
{
zval_dtor(&retval);
}
zval_ptr_dtor(&args[0]);
zval_ptr_dtor(&args[1]);
zval_ptr_dtor(&req->callback);
zval_ptr_dtor(&req->zfd);
if(req->buflen)
{
efree(req->buf);
}
ev_unref(eio_loop);
return 0;
}
PHP_METHOD(EIO, write)
{
dFILE_DESC;
dCALLBACK;
char *string;
int string_len;
eio_req *req;
if( ! eio_loop)
{
zend_throw_exception(NULL, "libev\\EIO: EIO not initialized", 1 TSRMLS_CC);
return;
}
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zsz", &fd, &string, &string_len, &callback) != SUCCESS) {
return;
}
EXTRACT_FILE_DESC(EIO, write);
CHECK_CALLBACK;
req = eio_write((int) file_desc, string, string_len,
/* offset */ 0, /* EIO pri */ 0, req_done, NULL);
STORE_REQ(NULL, 0);
/* TODO: Code for handling errors, ie. res == 0 */
assert(req);
}
PHP_METHOD(EIO, read)
{
dFILE_DESC;
dCALLBACK;
int len;
char *string;
eio_req *req;
if( ! eio_loop)
{
zend_throw_exception(NULL, "libev\\EIO: EIO not initialized", 1 TSRMLS_CC);
return;
}
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zlz", &fd, &len, &callback) != SUCCESS) {
return;
}
EXTRACT_FILE_DESC(EIO, read);
CHECK_CALLBACK;
string = emalloc(len);
req = eio_read((int) file_desc, string, len,
/* offset */ 0, /* EIO pri */ 0, req_done, NULL);
STORE_REQ(string, len);
/* TODO: Code for handling errors, ie. res == 0 */
assert(req);
}
static const zend_function_entry eio_methods[] = {
ZEND_ME(EIO, init, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
ZEND_ME(EIO, write, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
ZEND_ME(EIO, read, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
{NULL, NULL, NULL}
};