@@ -65,6 +65,21 @@ zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, NULL, 0
6565 TSRMLS_SET_CTX(uv->thread_ctx); \
6666 uv->resource_id = PHP_UV_LIST_INSERT(uv, uv_resource_handle); \
6767
68+ #define PHP_UV_INIT_SIGNAL (uv , uv_type ) \
69+ uv = (php_uv_t *)emalloc(sizeof(php_uv_t)); \
70+ if (!uv) { \
71+ php_error_docref(NULL TSRMLS_CC, E_ERROR, "emalloc failed"); \
72+ RETURN_FALSE; \
73+ } \
74+ if (uv_signal_init(loop, &uv->uv.signal)) { \
75+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "uv_signal_init failed");\
76+ RETURN_FALSE;\
77+ } \
78+ uv->type = uv_type; \
79+ PHP_UV_INIT_ZVALS(uv) \
80+ TSRMLS_SET_CTX(uv->thread_ctx); \
81+ uv->resource_id = PHP_UV_LIST_INSERT(uv, uv_resource_handle); \
82+
6883#define PHP_UV_INIT_CONNECT (req , uv ) \
6984 req = (uv_connect_t*)emalloc(sizeof(uv_connect_t)); \
7085 req->data = uv;
@@ -285,6 +300,8 @@ static void php_uv_timer_cb(uv_timer_t *handle, int status);
285300
286301static void php_uv_idle_cb (uv_timer_t * handle , int status );
287302
303+ static void php_uv_signal_cb (uv_signal_t * handle , int sig_num );
304+
288305
289306static char * php_uv_map_resource_name (enum php_uv_resource_type type )
290307{
@@ -2215,6 +2232,31 @@ static void php_uv_timer_cb(uv_timer_t *handle, int status)
22152232 zval_ptr_dtor (& client );
22162233}
22172234
2235+ static void php_uv_signal_cb (uv_signal_t * handle , int sig_num )
2236+ {
2237+ zval * retval_ptr , * zsig , * client = NULL ;
2238+ zval * * params [2 ];
2239+ php_uv_t * uv = (php_uv_t * )handle -> data ;
2240+ TSRMLS_FETCH_FROM_CTX (uv -> thread_ctx );
2241+
2242+ MAKE_STD_ZVAL (zsig );
2243+ ZVAL_LONG (zsig , sig_num );
2244+ MAKE_STD_ZVAL (client );
2245+ ZVAL_RESOURCE (client , uv -> resource_id );
2246+ zend_list_addref (uv -> resource_id );
2247+
2248+ params [0 ] = & client ;
2249+ params [1 ] = & zsig ;
2250+
2251+ php_uv_do_callback2 (& retval_ptr , uv , params , 2 , PHP_UV_SIGNAL_CB TSRMLS_CC );
2252+
2253+ if (retval_ptr != NULL ) {
2254+ zval_ptr_dtor (& retval_ptr );
2255+ }
2256+ zval_ptr_dtor (& zsig );
2257+ zval_ptr_dtor (& client );
2258+ }
2259+
22182260static inline uv_stream_t * php_uv_get_current_stream (php_uv_t * uv )
22192261{
22202262 uv_stream_t * stream ;
@@ -3341,6 +3383,20 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_poll_stop, 0, 0, 1)
33413383 ZEND_ARG_INFO (0 , handle )
33423384ZEND_END_ARG_INFO ()
33433385
3386+ ZEND_BEGIN_ARG_INFO_EX (arginfo_uv_signal_init , 0 , 0 , 1 )
3387+ ZEND_ARG_INFO (0 , loop )
3388+ ZEND_END_ARG_INFO ()
3389+
3390+ ZEND_BEGIN_ARG_INFO_EX (arginfo_uv_signal_start , 0 , 0 , 3 )
3391+ ZEND_ARG_INFO (0 , sig_handle )
3392+ ZEND_ARG_INFO (0 , sig_callback )
3393+ ZEND_ARG_INFO (0 , sig_num )
3394+ ZEND_END_ARG_INFO ()
3395+
3396+ ZEND_BEGIN_ARG_INFO_EX (arginfo_uv_signal_stop , 0 , 0 , 1 )
3397+ ZEND_ARG_INFO (0 , sig_handle )
3398+ ZEND_END_ARG_INFO ()
3399+
33443400/* PHP Functions */
33453401
33463402/* {{{ proto void uv_unref(resource $uv_t)
@@ -3503,6 +3559,88 @@ PHP_FUNCTION(uv_stop)
35033559}
35043560/* }}} */
35053561
3562+ /* {{{ proto resource uv_signal_init([resource $uv_loop])
3563+ */
3564+ PHP_FUNCTION (uv_signal_init )
3565+ {
3566+ zval * zloop = NULL ;
3567+ uv_loop_t * loop ;
3568+ php_uv_t * uv ;
3569+
3570+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
3571+ "|z" ,& zloop ) == FAILURE ) {
3572+ return ;
3573+ }
3574+
3575+ PHP_UV_FETCH_UV_DEFAULT_LOOP (loop , zloop );
3576+ PHP_UV_INIT_SIGNAL (uv , IS_UV_SIGNAL )
3577+
3578+ uv -> uv .signal .data = uv ;
3579+
3580+ ZVAL_RESOURCE (return_value , uv -> resource_id );
3581+ }
3582+ /* }}} */
3583+
3584+ /* {{{ proto void uv_signal_start(resource $sig_handle, callable $sig_callback, int $sig_num)
3585+ */
3586+ PHP_FUNCTION (uv_signal_start )
3587+ {
3588+ zval * sig_handle ;
3589+ long sig_num ;
3590+ php_uv_t * uv ;
3591+ zend_fcall_info fci = empty_fcall_info ;
3592+ zend_fcall_info_cache fcc = empty_fcall_info_cache ;
3593+ php_uv_cb_t * cb ;
3594+
3595+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
3596+ "rfl" , & sig_handle , & fci , & fcc , & sig_num ) == FAILURE ) {
3597+ return ;
3598+ }
3599+
3600+ ZEND_FETCH_RESOURCE (uv , php_uv_t * , & sig_handle , -1 , PHP_UV_RESOURCE_NAME , uv_resource_handle );
3601+
3602+ PHP_UV_TYPE_CHECK (uv , IS_UV_SIGNAL );
3603+
3604+ if (uv_is_active ((uv_handle_t * )& uv -> uv .signal )) {
3605+ php_error_docref (NULL TSRMLS_CC , E_NOTICE , "passed uv signal resource has been started. you don't have to call this method" );
3606+ RETURN_FALSE ;
3607+ }
3608+
3609+ zend_list_addref (uv -> resource_id );
3610+ php_uv_cb_init (& cb , uv , & fci , & fcc , PHP_UV_SIGNAL_CB );
3611+
3612+ uv_signal_start ((uv_signal_t * )& uv -> uv .signal , php_uv_signal_cb , sig_num );
3613+ }
3614+ /* }}} */
3615+
3616+ /* {{{ proto int uv_signal_stop(resource $sig_handle)
3617+ */
3618+ PHP_FUNCTION (uv_signal_stop )
3619+ {
3620+ zval * sig_handle ;
3621+ php_uv_t * uv ;
3622+ int r = 0 ;
3623+
3624+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC ,
3625+ "r" , & sig_handle ) == FAILURE ) {
3626+ return ;
3627+ }
3628+
3629+ ZEND_FETCH_RESOURCE (uv , php_uv_t * , & sig_handle , -1 , PHP_UV_RESOURCE_NAME , uv_resource_handle );
3630+
3631+ PHP_UV_TYPE_CHECK (uv , IS_UV_SIGNAL );
3632+
3633+ if (!uv_is_active ((uv_handle_t * )& uv -> uv .signal )) {
3634+ php_error_docref (NULL TSRMLS_CC , E_NOTICE , "passed uv signal resource has been stopped. you don't have to call this method" );
3635+ RETURN_FALSE ;
3636+ }
3637+
3638+ r = uv_signal_stop ((uv_signal_t * )& uv -> uv .signal );
3639+
3640+ RETURN_LONG (r );
3641+ }
3642+ /* }}} */
3643+
35063644/* {{{ proto long uv_run_once([resource $uv_loop])
35073645*/
35083646PHP_FUNCTION (uv_run_once )
@@ -6622,6 +6760,10 @@ static zend_function_entry uv_functions[] = {
66226760 PHP_FE (uv_cwd , NULL )
66236761 PHP_FE (uv_chdir , arginfo_uv_chdir )
66246762 PHP_FE (uv_resident_set_memory , NULL )
6763+ /* signal handling */
6764+ PHP_FE (uv_signal_init , arginfo_uv_signal_init )
6765+ PHP_FE (uv_signal_start , arginfo_uv_signal_start )
6766+ PHP_FE (uv_signal_stop , arginfo_uv_signal_stop )
66256767 /* http parser */
66266768 PHP_FE (uv_http_parser_init , arginfo_uv_http_parser_init )
66276769 PHP_FE (uv_http_parser_execute , arginfo_uv_http_parser_execute )
0 commit comments