forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_channel_coro.cc
213 lines (180 loc) · 6.78 KB
/
swoole_channel_coro.cc
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
212
213
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| Copyright (c) 2012-2018 The Swoole Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinyu Zhu <[email protected]> |
| Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#ifdef SW_COROUTINE
#include "swoole_coroutine.h"
#include "channel.h"
using namespace swoole;
static PHP_METHOD(swoole_channel_coro, __construct);
static PHP_METHOD(swoole_channel_coro, __destruct);
static PHP_METHOD(swoole_channel_coro, push);
static PHP_METHOD(swoole_channel_coro, pop);
static PHP_METHOD(swoole_channel_coro, close);
static PHP_METHOD(swoole_channel_coro, stats);
static PHP_METHOD(swoole_channel_coro, length);
static PHP_METHOD(swoole_channel_coro, isEmpty);
static PHP_METHOD(swoole_channel_coro, isFull);
static zend_class_entry swoole_channel_coro_ce;
static zend_class_entry *swoole_channel_coro_class_entry_ptr;
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_channel_coro_construct, 0, 0, 0)
ZEND_ARG_INFO(0, size)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_channel_coro_push, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_channel_coro_pop, 0, 0, 1)
ZEND_ARG_INFO(0, timeout)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
ZEND_END_ARG_INFO()
static const zend_function_entry swoole_channel_coro_methods[] =
{
PHP_ME(swoole_channel_coro, __construct, arginfo_swoole_channel_coro_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_channel_coro, __destruct, arginfo_swoole_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_channel_coro, push, arginfo_swoole_channel_coro_push, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel_coro, pop, arginfo_swoole_channel_coro_pop, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel_coro, isEmpty, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel_coro, isFull, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel_coro, close, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel_coro, stats, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_channel_coro, length, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void swoole_channel_coro_init(int module_number TSRMLS_DC)
{
INIT_CLASS_ENTRY(swoole_channel_coro_ce, "Swoole\\Coroutine\\Channel", swoole_channel_coro_methods);
swoole_channel_coro_class_entry_ptr = zend_register_internal_class(&swoole_channel_coro_ce);
if (SWOOLE_G(use_shortname))
{
sw_zend_register_class_alias("chan", swoole_channel_coro_class_entry_ptr);
}
zend_declare_property_long(swoole_channel_coro_class_entry_ptr, SW_STRL("capacity")-1, 0, ZEND_ACC_PUBLIC);
zend_declare_property_long(swoole_channel_coro_class_entry_ptr, SW_STRL("errCode")-1, 0, ZEND_ACC_PUBLIC);
}
static PHP_METHOD(swoole_channel_coro, __construct)
{
zend_long capacity = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &capacity) == FAILURE)
{
RETURN_FALSE;
}
if (capacity <= 0)
{
capacity = 1;
}
php_swoole_check_reactor();
Channel *chan = new Channel(capacity);
zend_update_property_long(swoole_channel_coro_class_entry_ptr, getThis(), ZEND_STRL("capacity"), capacity);
swoole_set_object(getThis(), chan);
}
static PHP_METHOD(swoole_channel_coro, __destruct)
{
SW_PREVENT_USER_DESTRUCT;
Channel *chan = (Channel *) swoole_get_object(getThis());
while (chan->length() > 0)
{
zval *data = (zval *) chan->pop();
sw_zval_free(data);
}
delete chan;
swoole_set_object(getThis(), NULL);
}
static PHP_METHOD(swoole_channel_coro, push)
{
coro_check(TSRMLS_C);
Channel *chan = (Channel *) swoole_get_object(getThis());
if (chan->closed)
{
zend_update_property_long(swoole_channel_coro_class_entry_ptr, getThis(), SW_STRL("errCode")-1, -2);
RETURN_FALSE;
}
zval *zdata;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zdata) == FAILURE)
{
RETURN_FALSE;
}
Z_TRY_ADDREF_P(zdata);
if (chan->push(sw_zval_dup(zdata)))
{
RETURN_TRUE;
}
else
{
Z_TRY_DELREF_P(zdata);
RETURN_FALSE;
}
}
static PHP_METHOD(swoole_channel_coro, pop)
{
coro_check(TSRMLS_C);
Channel *chan = (Channel *) swoole_get_object(getThis());
if (chan->closed)
{
zend_update_property_long(swoole_channel_coro_class_entry_ptr, getThis(), SW_STRL("errCode")-1, -2);
RETURN_FALSE;
}
double timeout = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|d", &timeout) == FAILURE)
{
RETURN_FALSE;
}
zval *data = (zval *) chan->pop(timeout);
if (data)
{
RETVAL_ZVAL(data, 0, 0);
efree(data);
}
else
{
zend_update_property_long(swoole_channel_coro_class_entry_ptr, getThis(), SW_STRL("errCode")-1, chan->closed ? -2 : -1);
RETURN_FALSE;
}
}
static PHP_METHOD(swoole_channel_coro, close)
{
Channel *chan = (Channel *) swoole_get_object(getThis());
RETURN_BOOL(chan->close());
}
static PHP_METHOD(swoole_channel_coro, length)
{
Channel *chan = (Channel *) swoole_get_object(getThis());
RETURN_LONG(chan->length());
}
static PHP_METHOD(swoole_channel_coro, isEmpty)
{
Channel *chan = (Channel *) swoole_get_object(getThis());
RETURN_BOOL(chan->is_empty());
}
static PHP_METHOD(swoole_channel_coro, isFull)
{
Channel *chan = (Channel *) swoole_get_object(getThis());
RETURN_BOOL(chan->is_full());
}
static PHP_METHOD(swoole_channel_coro, stats)
{
Channel *chan = (Channel *) swoole_get_object(getThis());
array_init(return_value);
sw_add_assoc_long_ex(return_value, ZEND_STRS("consumer_num"), chan->consumer_num());
sw_add_assoc_long_ex(return_value, ZEND_STRS("producer_num"), chan->producer_num());
if (chan)
{
sw_add_assoc_long_ex(return_value, ZEND_STRS("queue_num"), chan->length());
}
}
#endif