c++ sleep函数_《PHP扩展开发》-hook-(hook原来的sleep)
現(xiàn)在,我們進(jìn)入一個(gè)全新的主題,講解如何替換掉PHP原來那些阻塞的函數(shù)。已達(dá)到不修改歷史代碼,就可以直接協(xié)程化我們的代碼。
我們這篇文章需要實(shí)現(xiàn)的方法如下:
StudyRuntimeenableCoroutine()首先,我們創(chuàng)建兩個(gè)文件study_runtime.h、study_runtime.cc。
其中,study_runtime.h文件的內(nèi)容如下:
#ifndef STUDY_RUNTIME_H #define STUDY_RUNTIME_H#include "php_study.h"#endif /* STUDY_RUNTIME_H */其中,study_runtime.cc文件的內(nèi)容如下:
#include "study_runtime.h"然后修改我們的config.m4文件,增加study_runtime.cc為需要編譯的源文件:
study_source_file="study.cc study_coroutine.cc study_coroutine_util.cc src/coroutine/coroutine.cc src/coroutine/context.cc ${STUDY_ASM_DIR}make_${STUDY_CONTEXT_ASM_FILE} ${STUDY_ASM_DIR}jump_${STUDY_CONTEXT_ASM_FILE} src/socket.cc src/log.cc src/error.cc src/core/base.cc src/coroutine/socket.cc src/timer.cc study_coroutine_channel.cc src/coroutine/channel.cc study_coroutine_socket.cc study_coroutine_server.cc study_runtime.cc "然后和往常一樣,我們需要先實(shí)現(xiàn)PHP類StudyRuntime。
在文件study_runtime.cc里面,我們實(shí)現(xiàn)我們的enableCoroutine方法。首先,定義一下這個(gè)方法的參數(shù)。目前我們不需要傳遞參數(shù):
ZEND_BEGIN_ARG_INFO_EX(arginfo_study_runtime_void, 0, 0, 0) ZEND_END_ARG_INFO()然后是enableCoroutine方法的大體框架:
extern PHP_METHOD(study_coroutine_util, sleep); static void hook_func(const char *name, size_t name_len, zif_handler handler);static PHP_METHOD(study_runtime, enableCoroutine) {hook_func(ZEND_STRL("sleep"), zim_study_coroutine_util_sleep); }(后面我們會(huì)去實(shí)現(xiàn)hook_func這個(gè)函數(shù))
這里,我們的意圖是把PHP原來的sleep函數(shù)替換成StudyCoroutine::sleep這個(gè)方法。zim_study_coroutine_util_sleep實(shí)際上就是我們?cè)谖募tudy_coroutine_util.cc中定義的PHP_METHOD(study_coroutine_util, sleep)展開后的結(jié)果。
然后,收集enableCoroutine到結(jié)構(gòu)體zend_function_entry里面:
static const zend_function_entry study_runtime_methods[] = {PHP_ME(study_runtime, enableCoroutine, arginfo_study_runtime_void, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)PHP_FE_END };然后創(chuàng)建一個(gè)模塊初始化函數(shù)來注冊(cè)PHP類StudyRuntime:
/*** Define zend class entry*/ zend_class_entry study_runtime_ce; zend_class_entry *study_runtime_ce_ptr;void study_runtime_init() {INIT_NS_CLASS_ENTRY(study_runtime_ce, "Study", "Runtime", study_runtime_methods);study_runtime_ce_ptr = zend_register_internal_class(&study_runtime_ce TSRMLS_CC); // Registered in the Zend Engine }然后,我們?cè)趕tudy.cc文件的PHP_MINIT_FUNCTION(study)函數(shù)里面調(diào)用study_runtime_init:
PHP_MINIT_FUNCTION(study) {study_coroutine_util_init();study_coro_server_init(module_number);study_coro_channel_init();study_coro_socket_init(module_number);study_runtime_init(); // 新增的代碼return SUCCESS; }我們需要在php_study.h里面對(duì)study_runtime_init函數(shù)進(jìn)行聲明:
void study_coroutine_util_init(); void study_coro_server_init(int module_number); void study_coro_channel_init(); void study_coro_socket_init(int module_number); void study_runtime_init(); // 新增的代碼OK,我們完成了StudyRuntime類的注冊(cè)。
最后,我們需要實(shí)現(xiàn)hook_func:
static void hook_func(const char *name, size_t name_len, zif_handler new_handler) {zend_function *ori_f = (zend_function *) zend_hash_str_find_ptr(EG(function_table), name, name_len);ori_f->internal_function.handler = new_handler; }代碼其實(shí)比較簡(jiǎn)單,就是先去全局的EG(function_table)里面查找sleep名字對(duì)應(yīng)的zend_function,然后把它的handler換成我們新的new_handler即可。也就是說,PHP內(nèi)核實(shí)現(xiàn)的C函數(shù)實(shí)際上是會(huì)以函數(shù)指針的形式保存在zend_function.internal_function.handler里面。
然后,我們重新編譯、安裝擴(kuò)展:
phpize --clean && phpize && ./configure && 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/OK,編譯、安裝擴(kuò)展成功。
我們來編寫測(cè)試腳本:
<?phpstudy_event_init();Sgo(function () {var_dump(StudyCoroutine::getCid());sleep(1);var_dump(StudyCoroutine::getCid()); });Sgo(function () {var_dump(StudyCoroutine::getCid()); });study_event_wait();這個(gè)腳本沒有開啟hook,執(zhí)行結(jié)果如下:
~/codeDir/cppCode/study # php test.php int(1) int(1) int(2)符合預(yù)期。進(jìn)程因?yàn)榈谝粋€(gè)協(xié)程調(diào)用了阻塞的sleep函數(shù),所以導(dǎo)致整個(gè)進(jìn)程阻塞了起來,所以打印是順序的。
我們現(xiàn)在來開啟一下hook功能:
<?phpstudy_event_init();StudyRuntime::enableCoroutine();Sgo(function () {var_dump(StudyCoroutine::getCid());sleep(1);var_dump(StudyCoroutine::getCid()); });Sgo(function () {var_dump(StudyCoroutine::getCid()); });study_event_wait();結(jié)果如下:
~/codeDir/cppCode/study # php test.php int(1) int(2) int(1)因?yàn)槲覀円呀?jīng)把PHP原先的sleep函數(shù)替換成了StudyCoroutine::sleep方法,所以,進(jìn)程不會(huì)阻塞起來,會(huì)在調(diào)用sleep之后立馬切換到第二個(gè)協(xié)程。
總結(jié)
以上是生活随笔為你收集整理的c++ sleep函数_《PHP扩展开发》-hook-(hook原来的sleep)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如果简历上真写了“会多线程”,那面试一般
- 下一篇: windows录屏_电脑自带录屏软件怎么