-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy_coroutine.cc
More file actions
186 lines (148 loc) · 4.75 KB
/
study_coroutine.cc
File metadata and controls
186 lines (148 loc) · 4.75 KB
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
#include "coroutine.h"
#include "study_coroutine.h"
using study::Coroutine;
using study::PhpCoroutine;
php_coro_task PhpCoroutine::main_task = {0};
long PhpCoroutine::create(zend_fcall_info_cache *fci_cache, uint32_t argc, zval *argv)
{
php_coro_args php_coro_args;
php_coro_args.fci_cache = fci_cache;
php_coro_args.argc = argc;
php_coro_args.argv = argv;
save_task(get_task());
return Coroutine::create(create_func, (void *) &php_coro_args);
}
void PhpCoroutine::defer(php_study_fci_fcc *defer_fci_fcc)
{
php_coro_task *task = (php_coro_task *) get_task();
if (task->defer_tasks == nullptr) {
task->defer_tasks = new std::stack<php_study_fci_fcc *>;
}
task->defer_tasks->push(defer_fci_fcc);
}
int PhpCoroutine::sleep(double seconds)
{
return Coroutine::sleep(seconds);
}
void PhpCoroutine::save_task(php_coro_task *task)
{
save_vm_stack(task);
}
void PhpCoroutine::save_vm_stack(php_coro_task *task)
{
task->vm_stack_top = EG(vm_stack_top);
task->vm_stack_end = EG(vm_stack_end);
task->vm_stack = EG(vm_stack);
task->vm_stack_page_size = EG(vm_stack_page_size);
task->execute_data = EG(current_execute_data);
}
php_coro_task *PhpCoroutine::get_task()
{
php_coro_task *task = (php_coro_task *) Coroutine::get_current_task();
return task ? task : &main_task;
}
void PhpCoroutine::vm_stack_init(void)
{
uint32_t size = DEFAULT_PHP_STACK_PAGE_SIZE;
zend_vm_stack page = (zend_vm_stack) emalloc(size);
page->top = ZEND_VM_STACK_ELEMENTS(page);
page->end = (zval *) ((char *) page + size);
page->prev = NULL;
EG(vm_stack) = page;
EG(vm_stack)->top++;
EG(vm_stack_top) = EG(vm_stack)->top;
EG(vm_stack_end) = EG(vm_stack)->end;
EG(vm_stack_page_size) = size;
}
void PhpCoroutine::create_func(void *arg)
{
int i;
php_coro_args *php_arg = (php_coro_args *) arg;
zend_fcall_info_cache fci_cache = *php_arg->fci_cache;
zend_function *func = fci_cache.function_handler;
zval *argv = php_arg->argv;
int argc = php_arg->argc;
php_coro_task *task;
zend_execute_data *call;
zval _retval;
zval *retval = &_retval;
vm_stack_init(); // get a new php stack
call = (zend_execute_data *) (EG(vm_stack_top));
task = (php_coro_task *) EG(vm_stack_top);
EG(vm_stack_top) = (zval *) ((char *) call + PHP_CORO_TASK_SLOT * sizeof(zval));
call = zend_vm_stack_push_call_frame(
ZEND_CALL_TOP_FUNCTION | ZEND_CALL_ALLOCATED,
func, argc, fci_cache.called_scope, fci_cache.object
);
for (i = 0; i < argc; ++i) {
zval *param;
zval *arg = &argv[i];
param = ZEND_CALL_ARG(call, i + 1);
ZVAL_COPY(param, arg);
}
call->symbol_table = NULL;
EG(current_execute_data) = call;
save_vm_stack(task);
task->co = Coroutine::get_current();
task->co->set_task((void *) task);
task->defer_tasks = nullptr;
if (func->type == ZEND_USER_FUNCTION) {
ZVAL_UNDEF(retval);
EG(current_execute_data) = NULL;
zend_init_func_execute_data(call, &func->op_array, retval);
zend_execute_ex(EG(current_execute_data));
}
task = get_task();
std::stack<php_study_fci_fcc *> *defer_tasks = task->defer_tasks;
if (defer_tasks) {
php_study_fci_fcc *defer_fci_fcc;
zval result;
while (!defer_tasks->empty()) {
defer_fci_fcc = defer_tasks->top();
defer_tasks->pop();
defer_fci_fcc->fci.retval = &result;
if (zend_call_function(&defer_fci_fcc->fci, &defer_fci_fcc->fcc) != SUCCESS) {
php_error_docref(NULL, E_WARNING, "defer execute error");
return;
}
efree(defer_fci_fcc);
}
delete defer_tasks;
task->defer_tasks = nullptr;
}
// free php stack
zend_vm_stack stack = EG(vm_stack);
efree(stack);
zval_ptr_dtor(retval);
}
void PhpCoroutine::on_yield(void *arg)
{
php_coro_task *task = (php_coro_task *) arg;
php_coro_task *origin_task = get_origin_task(task);
save_task(task);
restore_task(origin_task);
}
void PhpCoroutine::on_resume(void *arg)
{
php_coro_task *task = (php_coro_task *) task;
php_coro_task *current_task = get_task();
save_task(current_task);
restore_task(task);
}
void PhpCoroutine::restore_task(php_coro_task *task)
{
restore_vm_stack(task);
}
inline void PhpCoroutine::restore_vm_stack(php_coro_task *task)
{
EG(vm_stack_top) = task->vm_stack_top;
EG(vm_stack_end) = task->vm_stack_end;
EG(vm_stack) = task->vm_stack;
EG(vm_stack_page_size) = task->vm_stack_page_size;
EG(current_execute_data) = task->execute_data;
}
void PhpCoroutine::init()
{
Coroutine::set_on_yield(on_yield);
Coroutine::set_on_resume(on_resume);
}