forked from php-extension-research/study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudy_coroutine.h
68 lines (58 loc) · 1.85 KB
/
study_coroutine.h
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
#ifndef STUDY_COROUTINE_H
#define STUDY_COROUTINE_H
#include "php_study.h"
#include "coroutine.h"
#include <stack>
#define DEFAULT_PHP_STACK_PAGE_SIZE 8192
#define PHP_CORO_TASK_SLOT ((int)((ZEND_MM_ALIGNED_SIZE(sizeof(php_coro_task)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / ZEND_MM_ALIGNED_SIZE(sizeof(zval))))
struct php_study_fci_fcc
{
zend_fcall_info fci;
zend_fcall_info_cache fcc;
};
struct php_coro_args
{
zend_fcall_info_cache *fci_cache;
zval *argv;
uint32_t argc;
};
// save the coroutine stack info
struct php_coro_task
{
zval *vm_stack_top; // coroutine stack top
zval *vm_stack_end; // coroutine stack end
zend_vm_stack vm_stack; // current coroutine stack pointer
size_t vm_stack_page_size;
zend_execute_data *execute_data; // current coroutine stack frame
study::Coroutine *co;
std::stack<php_study_fci_fcc *> *defer_tasks;
};
namespace study
{
class PHPCoroutine
{
public:
static void init();
static long create(zend_fcall_info_cache *fci_cache, uint32_t argc, zval *argv);
static void defer(php_study_fci_fcc *defer_fci_fcc);
static int sleep(double seconds);
static inline php_coro_task* get_origin_task(php_coro_task *task)
{
Coroutine *co = task->co->get_origin();
return co ? (php_coro_task *) co->get_task() : &main_task;
}
protected:
static php_coro_task main_task;
static void save_task(php_coro_task *task);
static void save_vm_stack(php_coro_task *task);
static php_coro_task* get_task();
static void create_func(void *arg);
static void vm_stack_init(void);
static void on_yield(void *arg);
static void on_resume(void *arg);
static void on_close(void *arg);
static inline void restore_task(php_coro_task *task);
static inline void restore_vm_stack(php_coro_task *task);
};
}
#endif /* STUDY_COROUTINE_H */