日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

c++ sleep函数_《PHP扩展开发》-hook-(hook原来的sleep)

發(fā)布時間:2025/3/15 php 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ sleep函数_《PHP扩展开发》-hook-(hook原来的sleep) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

現(xiàn)在,我們進(jìn)入一個全新的主題,講解如何替換掉PHP原來那些阻塞的函數(shù)。已達(dá)到不修改歷史代碼,就可以直接協(xié)程化我們的代碼。

我們這篇文章需要實(shí)現(xiàn)的方法如下:

StudyRuntimeenableCoroutine()

首先,我們創(chuàng)建兩個文件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方法。首先,定義一下這個方法的參數(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); }

(后面我們會去實(shí)現(xiàn)hook_func這個函數(shù))

這里,我們的意圖是把PHP原來的sleep函數(shù)替換成StudyCoroutine::sleep這個方法。zim_study_coroutine_util_sleep實(shí)際上就是我們在文件study_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)建一個模塊初始化函數(shù)來注冊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 }

然后,我們在study.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里面對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類的注冊。

最后,我們需要實(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í)比較簡單,就是先去全局的EG(function_table)里面查找sleep名字對應(yīng)的zend_function,然后把它的handler換成我們新的new_handler即可。也就是說,PHP內(nèi)核實(shí)現(xiàn)的C函數(shù)實(shí)際上是會以函數(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ò)展成功。

我們來編寫測試腳本:

<?phpstudy_event_init();Sgo(function () {var_dump(StudyCoroutine::getCid());sleep(1);var_dump(StudyCoroutine::getCid()); });Sgo(function () {var_dump(StudyCoroutine::getCid()); });study_event_wait();

這個腳本沒有開啟hook,執(zhí)行結(jié)果如下:

~/codeDir/cppCode/study # php test.php int(1) int(1) int(2)

符合預(yù)期。進(jìn)程因?yàn)榈谝粋€協(xié)程調(diào)用了阻塞的sleep函數(shù),所以導(dǎo)致整個進(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)程不會阻塞起來,會在調(diào)用sleep之后立馬切換到第二個協(xié)程。

總結(jié)

以上是生活随笔為你收集整理的c++ sleep函数_《PHP扩展开发》-hook-(hook原来的sleep)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。