Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
study
提交
007f28e0
S
study
项目概览
OpenDocCN
/
study
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
study
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
007f28e0
编写于
7月 03, 2019
作者:
C
codinghuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新增《PHP扩展开发》-协程-修复一些bug(三)
上级
038f7674
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
190 addition
and
69 deletion
+190
-69
README.md
README.md
+1
-0
docs/《PHP扩展开发》-协程-修复一些bug(三).md
docs/《PHP扩展开发》-协程-修复一些bug(三).md
+166
-0
docs/《PHP扩展开发》-协程-协程isExist.md
docs/《PHP扩展开发》-协程-协程isExist.md
+1
-1
study_coroutine_util.cc
study_coroutine_util.cc
+4
-0
test.php
test.php
+18
-68
未找到文件。
README.md
浏览文件 @
007f28e0
...
...
@@ -44,3 +44,4 @@ PHP版本:7.3.5
[
19、协程isExist
](
./docs/《PHP扩展开发》-协程-协程isExist.md
)
[
20、修复一些bug(三)
](
./docs/《PHP扩展开发》-协程-修复一些bug(三).md
)
\ No newline at end of file
docs/《PHP扩展开发》-协程-修复一些bug(三).md
0 → 100644
浏览文件 @
007f28e0
# 修复一些bug(三)
这篇文章,我们来修复一个
`Study\Coroutine::getCid()`
的bug。
测试脚本如下:
```
php
<?php
$cid
=
Study\Coroutine
::
getCid
();
echo
"coroutine [
{
$cid
}
] create"
.
PHP_EOL
;
```
也就是说,我们在非协程环境下调用了
`Study\Coroutine::getCid()`
。
我们来执行脚本:
```
shell
~/codeDir/cppCode/study
# php test.php
Segmentation fault
~/codeDir/cppCode/study
#
```
报错了。
我们来调试一下:
```
shell
~/codeDir/cppCode/study
# cgdb php
GNU gdb
(
GDB
)
8.2
Copyright
(
C
)
2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type
"show copying"
and
"show warranty"
for
details.
This GDB was configured as
"x86_64-alpine-linux-musl"
.
Type
"show configuration"
for
configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For
help
,
type
"help"
.
Type
"apropos word"
to search
for
commands related to
"word"
...
Reading symbols from php...
(
no debugging symbols found
)
...done.
(
gdb
)
```
然后执行脚本:
```
shell
13│ public:
14│ static std::unordered_map<long, Coroutine
*
>
coroutines
;
15│
16│ static void
*
get_current_task
()
;
17│ static long create
(
coroutine_func_t fn, void
*
args
=
nullptr
)
;
18│ void
*
get_task
()
;
19│ static Coroutine
*
get_current
()
;
20│ void set_task
(
void
*
_task
)
;
21│ void yield
()
;
22│ void resume
()
;
23│
24│ inline long get_cid
()
25│
{
26├─666666>
return
cid
;
27│
}
```
是在调用
`Study::Coroutine::get_cid()`
的时候报错的。
我们在
`zim_study_coroutine_util_getCid`
处打一个断点:
```
shell
(
gdb
)
b zim_study_coroutine_util_getCid
Breakpoint 1 at 0x7ffff78d1f73: file /root/codeDir/cppCode/study/study_coroutine_util.cc, line 71.
(
gdb
)
```
然后重新运行脚本:
```
shell
(
gdb
)
r test.php
The program being debugged has been started already.
Start it from the beginning?
(
y or n
)
y
The program being debugged has been started already.
Starting program: /usr/local/bin/php test.php
Breakpoint 1, zim_study_coroutine_util_getCid
(
execute_data
=
0x7ffff761d0f0,
return_value
=
0x7ffff761d090
)
at /root/codeDir/cppCode/study/study_coroutine_util.cc:71
(
gdb
)
```
```
cpp
69
│
PHP_METHOD
(
study_coroutine_util
,
getCid
)
70
│
{
71
├───
>
Coroutine
*
co
=
Coroutine
::
get_current
();
72
│
RETURN_LONG
(
co
->
get_cid
());
73
│
}
```
继续:
```
shell
(
gdb
)
n
(
gdb
)
```
```
cpp
69
│
PHP_METHOD
(
study_coroutine_util
,
getCid
)
70
│
{
71
│
Coroutine
*
co
=
Coroutine
::
get_current
();
72
├───
>
RETURN_LONG
(
co
->
get_cid
());
73
│
}
```
打印一下这个
`co`
:
```
shell
(
gdb
)
p co
$1
=
(
Study::Coroutine
*
)
0x0
(
gdb
)
```
我们发现,因为我们的
`Study::Coroutine::current`
在初始化的时候,给了初始值:
```
cpp
Coroutine
*
Coroutine
::
current
=
nullptr
;
```
所以,当然是
`0x0`
了。因此我们无法调用
`get_cid()`
。
所以,我们只需要判断一下
`Coroutine* co = Coroutine::get_current();`
取出来的
`co`
是否为
`nullptr`
即可修复这个bug:
```
cpp
Coroutine
*
co
=
Coroutine
::
get_current
();
if
(
co
==
nullptr
)
{
RETURN_LONG
(
-
1
);
}
RETURN_LONG
(
co
->
get_cid
());
```
OK,我们编译、安装:
```
shell
~/codeDir/cppCode/study
# make clean && make && make install
----------------------------------------------------------------------
Build complete.
Don
't forget to run '
make
test
'.
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20180731/
Installing header files: /usr/local/include/php/
```
执行脚本:
```
shell
~/codeDir/cppCode/study
# php test.php
coroutine
[
-1
]
create
~/codeDir/cppCode/study
#
```
符合预期。
docs/《PHP扩展开发》-协程-协程isExist.md
浏览文件 @
007f28e0
...
...
@@ -152,7 +152,7 @@ coroutine [2] is non-existent
符合预期。
[
下一篇:修复一些bug(三)
](
./《PHP扩展开发》-协程-修复一些bug(三).md
)
...
...
study_coroutine_util.cc
浏览文件 @
007f28e0
...
...
@@ -69,6 +69,10 @@ PHP_METHOD(study_coroutine_util, resume)
PHP_METHOD
(
study_coroutine_util
,
getCid
)
{
Coroutine
*
co
=
Coroutine
::
get_current
();
if
(
co
==
nullptr
)
{
RETURN_LONG
(
-
1
);
}
RETURN_LONG
(
co
->
get_cid
());
}
...
...
test.php
浏览文件 @
007f28e0
<?php
function
task
(
$arg
)
{
$cid
=
Study\Coroutine
::
getCid
();
echo
"coroutine [
{
$cid
}
] create"
.
PHP_EOL
;
Study\Coroutine
::
yield
();
echo
"coroutine [
{
$cid
}
] is resumed"
.
PHP_EOL
;
}
$cid
=
Study\Coroutine
::
getCid
();
echo
"coroutine [
{
$cid
}
] create"
.
PHP_EOL
;
echo
"main coroutine"
.
PHP_EOL
;
$cid1
=
Study\Coroutine
::
create
(
'task'
,
'a'
);
echo
"main coroutine"
.
PHP_EOL
;
$cid2
=
Study\Coroutine
::
create
(
'task'
,
'b'
);
echo
"main coroutine"
.
PHP_EOL
;
// function task($arg)
// {
// $cid = Study\Coroutine::getCid();
// echo "coroutine [{$cid}] create" . PHP_EOL;
// Study\Coroutine::yield();
// echo "coroutine [{$cid}] is resumed" . PHP_EOL;
// }
if
(
Study\Coroutine
::
isExist
(
$cid1
))
{
echo
"coroutine [
{
$cid1
}
] is existent
\n
"
;
}
else
{
echo
"coroutine [
{
$cid1
}
] is non-existent
\n
"
;
}
if
(
Study\Coroutine
::
isExist
(
$cid2
))
{
echo
"coroutine [
{
$cid2
}
] is existent
\n
"
;
}
else
{
echo
"coroutine [
{
$cid2
}
] is non-existent
\n
"
;
}
Study\Coroutine
::
resume
(
$cid1
);
echo
"main coroutine"
.
PHP_EOL
;
if
(
Study\Coroutine
::
isExist
(
$cid1
))
{
echo
"coroutine [
{
$cid1
}
] is existent
\n
"
;
}
else
{
echo
"coroutine [
{
$cid1
}
] is non-existent
\n
"
;
}
if
(
Study\Coroutine
::
isExist
(
$cid2
))
{
echo
"coroutine [
{
$cid2
}
] is existent
\n
"
;
}
else
{
echo
"coroutine [
{
$cid2
}
] is non-existent
\n
"
;
}
Study\Coroutine
::
resume
(
$cid2
);
echo
"main coroutine"
.
PHP_EOL
;
if
(
Study\Coroutine
::
isExist
(
$cid1
))
{
echo
"coroutine [
{
$cid1
}
] is existent
\n
"
;
}
else
{
echo
"coroutine [
{
$cid1
}
] is non-existent
\n
"
;
}
if
(
Study\Coroutine
::
isExist
(
$cid2
))
{
echo
"coroutine [
{
$cid2
}
] is existent
\n
"
;
}
else
{
echo
"coroutine [
{
$cid2
}
] is non-existent
\n
"
;
}
// echo "main coroutine" . PHP_EOL;
// $cid1 = Study\Coroutine::create('task', 'a');
// echo "main coroutine" . PHP_EOL;
// $cid2 = Study\Coroutine::create('task', 'b');
// echo "main coroutine" . PHP_EOL;
// Study\Coroutine::resume($cid1);
// echo "main coroutine" . PHP_EOL;
// Study\Coroutine::resume($cid2);
// echo "main coroutine" . PHP_EOL;
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录