From 8f9cbcfbe6f5529334647a597df6ca2602314314 Mon Sep 17 00:00:00 2001 From: codinghuang <2812240764@qq.com> Date: Wed, 3 Jul 2019 19:06:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E3=80=8APHP=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E5=BC=80=E5=8F=91=E3=80=8B-=E5=8D=8F=E7=A8=8B-?= =?UTF-8?q?=E5=8D=8F=E7=A8=8BisExist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +- ...ug\357\274\210\344\272\214\357\274\211.md" | 2 +- ...50\213-\345\215\217\347\250\213isExist.md" | 166 ++++++++++++++++++ study_coroutine_util.cc | 19 ++ test.php | 54 +++++- 5 files changed, 243 insertions(+), 3 deletions(-) create mode 100644 "docs/\343\200\212PHP\346\211\251\345\261\225\345\274\200\345\217\221\343\200\213-\345\215\217\347\250\213-\345\215\217\347\250\213isExist.md" diff --git a/README.md b/README.md index 843d361..a3afbc2 100644 --- a/README.md +++ b/README.md @@ -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) + diff --git "a/docs/\343\200\212PHP\346\211\251\345\261\225\345\274\200\345\217\221\343\200\213-\345\215\217\347\250\213-\344\277\256\345\244\215\344\270\200\344\272\233bug\357\274\210\344\272\214\357\274\211.md" "b/docs/\343\200\212PHP\346\211\251\345\261\225\345\274\200\345\217\221\343\200\213-\345\215\217\347\250\213-\344\277\256\345\244\215\344\270\200\344\272\233bug\357\274\210\344\272\214\357\274\211.md" index 028c10b..fc9b4f1 100644 --- "a/docs/\343\200\212PHP\346\211\251\345\261\225\345\274\200\345\217\221\343\200\213-\345\215\217\347\250\213-\344\277\256\345\244\215\344\270\200\344\272\233bug\357\274\210\344\272\214\357\274\211.md" +++ "b/docs/\343\200\212PHP\346\211\251\345\261\225\345\274\200\345\217\221\343\200\213-\345\215\217\347\250\213-\344\277\256\345\244\215\344\270\200\344\272\233bug\357\274\210\344\272\214\357\274\211.md" @@ -169,7 +169,7 @@ main coroutine OK,符合预期。 - +[下一篇:协程isExist](./《PHP扩展开发》-协程-协程isExist.md) diff --git "a/docs/\343\200\212PHP\346\211\251\345\261\225\345\274\200\345\217\221\343\200\213-\345\215\217\347\250\213-\345\215\217\347\250\213isExist.md" "b/docs/\343\200\212PHP\346\211\251\345\261\225\345\274\200\345\217\221\343\200\213-\345\215\217\347\250\213-\345\215\217\347\250\213isExist.md" new file mode 100644 index 0000000..372929f --- /dev/null +++ "b/docs/\343\200\212PHP\346\211\251\345\261\225\345\274\200\345\217\221\343\200\213-\345\215\217\347\250\213-\345\215\217\347\250\213isExist.md" @@ -0,0 +1,166 @@ +# 协程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 +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 }; diff --git a/test.php b/test.php index 72b66fa..ab11106 100644 --- a/test.php +++ b/test.php @@ -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"; +} -- GitLab