php-extension-research / study Goto Github PK
View Code? Open in Web Editor NEW手把手教你写PHP协程扩展(teach you to write php coroutine extension by hand)
Home Page: https://php-extension-research.github.io/study/#/
手把手教你写PHP协程扩展(teach you to write php coroutine extension by hand)
Home Page: https://php-extension-research.github.io/study/#/
如题
要执行 php ext_skel.php --ext
命令找不到源码
测试gittalk
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.argv = argv;
php_coro_args.argc = argc;
save_task(get_task());
return Coroutine::create(create_func, (void*) &php_coro_args);
}
好像是这里报错,提示zend_fcall_info_cache 未定义么? 这不是php自定义的宏么,我已经引入了php.h 了的
问题描述
协程创建传入的闭包op_array分配在closures object成员属性,当yield后会被以参数释减少引用计数,
resume后closures object已被释放,将高概率性触发core dumped。
协程execute_data分配阶段应该手动对closures增加一次引用计数
以下是错误bt
Starting program: /usr/local/php7.3/bin/php -dextension=/usr/local/php7.3/lib/php/extensions/debug-non-zts-20180731/study.so -f /vagrant/www/tests/coroutine/bug.php [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". int(0) int(1) int(0) int(1) string(9) "cid_a_end" Program received signal SIGSEGV, Segmentation fault. 0x00000000009cb10c in zend_gc_delref (p=0x3) at /vagrant/www/php-src-win/Zend/zend_types.h:996 996 return --(p->refcount); (gdb) bt #0 0x00000000009cb10c in zend_gc_delref (p=0x3) at /vagrant/www/php-src-win/Zend/zend_types.h:996 #1 0x00000000009d310b in i_free_compiled_variables (execute_data=0x7ffff3e7b070) at /vagrant/www/php-src-win/Zend/zend_execute.c:2361 #2 0x00000000009d6260 in zend_leave_helper_SPEC () at /vagrant/www/php-src-win/Zend/zend_vm_execute.h:641 #3 0x00000000009dc2bb in ZEND_RETURN_SPEC_CONST_HANDLER () at /vagrant/www/php-src-win/Zend/zend_vm_execute.h:2808 #4 0x00000000009d5a8d in execute_ex (ex=0x7ffff3e7b070) at /vagrant/www/php-src-win/Zend/zend_vm_execute.h:43
以下是复现代码
`<?php
study_event_init();
$cid_a = Sgo(function () {
$b = function () {
$arr = debug_backtrace();
};
for($i=0;$i<=2;$i++){
Study\Coroutine::yield();
$arr =range(1,100002);
$b();
}
var_dump("cid_a_end");
});
$cid_b = Sgo(function () {
$b = function (){
$arr = debug_backtrace();
};
for($i=0;$i<=2;$i++){
Study\Coroutine::yield();
$arr =range(1,100001);
$b();
}
var_dump("cid_b_end");
});
$count = 0 ;
$i= 0;
while ($count<=4){
if($i>1){
$i=0;
}
Study\Coroutine::resume($i==0?$cid_a:$cid_b);
var_dump($i);
$i++;
$count++;
}
study_event_wait();`
执行 php test.php
后输出如下
bash-4.4# php test.php
main coroutine
coroutine[1] create successfully
a
b
main coroutine
问下, 有什么方法调试看, 为什么挂起了吗?
发现在swoole源码中出现了c++,本教程开发的协程也使用了c++,请问是因为c++在开发协程方面有固有的优势,还是开发者个人意愿?
一年多没更新了,是不维护了吗?
疑问:结构体成员params是一个zval的指针,但是怎么看出来它是一个数组呢?
我在【协程创建(四)】看到,当给task方法传递$a,$b两个参数时,在gdb里调试,p *fci.params[0].value.str.val@1,p *fci.params[1].value.str.val@1。
报错提示:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20180731/lib.so'
(tried: /usr/lib/php/20180731/lib.so (/usr/lib/php/20180731/lib.so:
undefined symbol: _ZN3Lib9Coroutine10coroutinesE),
/usr/lib/php/20180731//usr/lib/php/20180731/lib.so.so
(/usr/lib/php/20180731//usr/lib/php/20180731/lib.so.so: cannot open shared object file:
No such file or directory)) in Unknown on line 0
关键字:
undefined symbol: _ZN3Lib9Coroutine10coroutinesE)
分析原因
提示有看到`Lib` `Coroutine` `coroutines` 这三个关键字,说明这里有问题,经排查:未初始化这个静态变量
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.