Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
study
提交
8f9cbcfb
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 搜索 >>
提交
8f9cbcfb
编写于
7月 03, 2019
作者:
C
codinghuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新增《PHP扩展开发》-协程-协程isExist
上级
42bdf2c5
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
243 addition
and
3 deletion
+243
-3
README.md
README.md
+4
-1
docs/《PHP扩展开发》-协程-修复一些bug(二).md
docs/《PHP扩展开发》-协程-修复一些bug(二).md
+1
-1
docs/《PHP扩展开发》-协程-协程isExist.md
docs/《PHP扩展开发》-协程-协程isExist.md
+166
-0
study_coroutine_util.cc
study_coroutine_util.cc
+19
-0
test.php
test.php
+53
-1
未找到文件。
README.md
浏览文件 @
8f9cbcfb
...
...
@@ -40,4 +40,7 @@ PHP版本:7.3.5
[
17、修复一些bug(一)
](
./docs/《PHP扩展开发》-协程-修复一些bug(一).md
)
[
18、修复一些bug(二)
](
./docs/《PHP扩展开发》-协程-修复一些bug(二).md
)
\ No newline at end of file
[
18、修复一些bug(二)
](
./docs/《PHP扩展开发》-协程-修复一些bug(二).md
)
[
19、协程isExist
](
./docs/《PHP扩展开发》-协程-协程isExist.md
)
docs/《PHP扩展开发》-协程-修复一些bug(二).md
浏览文件 @
8f9cbcfb
...
...
@@ -169,7 +169,7 @@ main coroutine
OK,符合预期。
[
下一篇:协程isExist
](
./《PHP扩展开发》-协程-协程isExist.md
)
...
...
docs/《PHP扩展开发》-协程-协程isExist.md
0 → 100644
浏览文件 @
8f9cbcfb
# 协程isExist
这篇文章,我们实现一个判断某个协程是否存在的接口:
```
php
Study\Coroutine
::
isExist
(
long
$cid
)
:
bool
```
我们先定义方法参数,在
`study_coroutine_util.cc`
文件中进行定义:
```
cpp
ZEND_BEGIN_ARG_INFO_EX
(
arginfo_study_coroutine_isExist
,
0
,
0
,
1
)
ZEND_ARG_INFO
(
0
,
cid
)
ZEND_END_ARG_INFO
()
```
方法接收一个协程id。
然后实现这个接口:
```
cpp
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
);
}
```
我们直接在
`Coroutine::coroutines`
中查找这个协程是否存在,如果找到,则返回
`true`
;否则返回
`false`
。
因为上一章我们修复了一个协程死亡后,没有从
`Coroutine::coroutines`
中删除的bug,所以,我们的
`Study\Coroutine::isExist`
可以正常使用了。
我们编写测试脚本:
```
php
<?php
function
task
(
$arg
)
{
$cid
=
Study\Coroutine
::
getCid
();
echo
"coroutine [
{
$cid
}
] create"
.
PHP_EOL
;
Study\Coroutine
::
yield
();
echo
"coroutine [
{
$cid
}
] is resumed"
.
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
;
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
"
;
}
```
然后编译、安装扩展:
```
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
main coroutine
coroutine
[
1] create
main coroutine
coroutine
[
2] create
main coroutine
coroutine
[
1] is existent
coroutine
[
2] is existent
coroutine
[
1] is resumed
main coroutine
coroutine
[
1] is non-existent
coroutine
[
2] is existent
coroutine
[
2] is resumed
main coroutine
coroutine
[
1] is non-existent
coroutine
[
2] is non-existent
~/codeDir/cppCode/study
#
```
符合预期。
study_coroutine_util.cc
浏览文件 @
8f9cbcfb
...
...
@@ -13,6 +13,10 @@ 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
()
...
...
@@ -68,12 +72,27 @@ PHP_METHOD(study_coroutine_util, getCid)
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
);
}
static
const
zend_function_entry
study_coroutine_util_methods
[]
=
{
PHP_ME
(
study_coroutine_util
,
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_FE_END
};
...
...
test.php
浏览文件 @
8f9cbcfb
...
...
@@ -5,7 +5,7 @@ function task($arg)
$cid
=
Study\Coroutine
::
getCid
();
echo
"coroutine [
{
$cid
}
] create"
.
PHP_EOL
;
Study\Coroutine
::
yield
();
echo
"coroutine [
{
$cid
}
]
create
"
.
PHP_EOL
;
echo
"coroutine [
{
$cid
}
]
is resumed
"
.
PHP_EOL
;
}
echo
"main coroutine"
.
PHP_EOL
;
...
...
@@ -14,7 +14,59 @@ echo "main coroutine" . PHP_EOL;
$cid2
=
Study\Coroutine
::
create
(
'task'
,
'b'
);
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
(
$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
"
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录