-
Notifications
You must be signed in to change notification settings - Fork 79
/
study_coroutine_util.cc
159 lines (127 loc) · 4.56 KB
/
study_coroutine_util.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
#include "study_coroutine.h"
#include <unordered_map>
using study::PHPCoroutine;
using study::Coroutine;
static std::unordered_map<long, Coroutine *> user_yield_coros;
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_resume, 0, 0, 1)
ZEND_ARG_INFO(0, cid)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_isExist, 0, 0, 1)
ZEND_ARG_INFO(0, cid)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_create, 0, 0, 1)
ZEND_ARG_CALLABLE_INFO(0, func, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_defer, 0, 0, 1)
ZEND_ARG_CALLABLE_INFO(0, func, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_coroutine_sleep, 0, 0, 1)
ZEND_ARG_INFO(0, seconds)
ZEND_END_ARG_INFO()
PHP_FUNCTION(study_coroutine_create)
{
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_FUNC(fci, fcc)
Z_PARAM_VARIADIC('*', fci.params, fci.param_count)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
long cid = PHPCoroutine::create(&fcc, fci.param_count, fci.params);
RETURN_LONG(cid);
}
PHP_METHOD(study_coroutine_util, yield)
{
Coroutine* co = Coroutine::get_current();
user_yield_coros[co->get_cid()] = co;
co->yield();
RETURN_TRUE;
}
PHP_METHOD(study_coroutine_util, resume)
{
zend_long cid = 0;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(cid)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
auto coroutine_iterator = user_yield_coros.find(cid);
if (coroutine_iterator == user_yield_coros.end())
{
php_error_docref(NULL, E_WARNING, "resume error");
RETURN_FALSE;
}
Coroutine* co = coroutine_iterator->second;
user_yield_coros.erase(cid);
co->resume();
RETURN_TRUE;
}
PHP_METHOD(study_coroutine_util, getCid)
{
Coroutine* co = Coroutine::get_current();
if (co == nullptr)
{
RETURN_LONG(-1);
}
RETURN_LONG(co->get_cid());
}
PHP_METHOD(study_coroutine_util, isExist)
{
zend_long cid = 0;
bool is_exist;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(cid)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
auto coroutine_iterator = Coroutine::coroutines.find(cid);
is_exist = (coroutine_iterator != Coroutine::coroutines.end());
RETURN_BOOL(is_exist);
}
PHP_METHOD(study_coroutine_util, defer)
{
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
php_study_fci_fcc *defer_fci_fcc;
defer_fci_fcc = (php_study_fci_fcc *)emalloc(sizeof(php_study_fci_fcc));
ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_FUNC(fci, fcc)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
defer_fci_fcc->fci = fci;
defer_fci_fcc->fcc = fcc;
PHPCoroutine::defer(defer_fci_fcc);
}
PHP_METHOD(study_coroutine_util, sleep)
{
double seconds;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_DOUBLE(seconds)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
if (UNEXPECTED(seconds < 0.001))
{
php_error_docref(NULL, E_WARNING, "Timer must be greater than or equal to 0.001");
RETURN_FALSE;
}
PHPCoroutine::sleep(seconds);
RETURN_TRUE;
}
static const zend_function_entry study_coroutine_util_methods[] =
{
ZEND_FENTRY(create, ZEND_FN(study_coroutine_create), arginfo_study_coroutine_create, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(study_coroutine_util, yield, arginfo_study_coroutine_void, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(study_coroutine_util, resume, arginfo_study_coroutine_resume, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(study_coroutine_util, getCid, arginfo_study_coroutine_void, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(study_coroutine_util, isExist, arginfo_study_coroutine_isExist, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(study_coroutine_util, defer, arginfo_study_coroutine_defer, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(study_coroutine_util, sleep, arginfo_study_coroutine_sleep, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_FE_END
};
/**
* Define zend class entry
*/
zend_class_entry study_coroutine_ce;
zend_class_entry *study_coroutine_ce_ptr;
void study_coroutine_util_init()
{
PHPCoroutine::init();
INIT_NS_CLASS_ENTRY(study_coroutine_ce, "Study", "Coroutine", study_coroutine_util_methods);
study_coroutine_ce_ptr = zend_register_internal_class(&study_coroutine_ce TSRMLS_CC); // Registered in the Zend Engine
zend_register_class_alias("SCo", study_coroutine_ce_ptr);
}