Skip to content

Commit 204fcbe

Browse files
committed
MFH: Added pcntl_signal_dispatch()
[DOC] pcntl_signal_dispatch() allows to dispatch pending signals to registered signal handler functions on-demand. This allows to use pcntl_signal() without ticks.
1 parent b6cca30 commit 204fcbe

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

ext/pcntl/pcntl.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ const zend_function_entry pcntl_functions[] = {
134134
PHP_FE(pcntl_waitpid, arginfo_pcntl_waitpid)
135135
PHP_FE(pcntl_wait, arginfo_pcntl_wait)
136136
PHP_FE(pcntl_signal, arginfo_pcntl_signal)
137+
PHP_FE(pcntl_signal_dispatch, arginfo_pcntl_void)
137138
PHP_FE(pcntl_wifexited, arginfo_pcntl_wifexited)
138139
PHP_FE(pcntl_wifstopped, arginfo_pcntl_wifstopped)
139140
PHP_FE(pcntl_wifsignaled, arginfo_pcntl_wifsignaled)
@@ -173,7 +174,7 @@ ZEND_GET_MODULE(pcntl)
173174
#endif
174175

175176
static void pcntl_signal_handler(int);
176-
static void pcntl_tick_handler();
177+
static void pcntl_signal_dispatch();
177178

178179
void php_register_signal_constants(INIT_FUNC_ARGS)
179180
{
@@ -262,7 +263,7 @@ PHP_RINIT_FUNCTION(pcntl)
262263
PHP_MINIT_FUNCTION(pcntl)
263264
{
264265
php_register_signal_constants(INIT_FUNC_ARGS_PASSTHRU);
265-
php_add_tick_function(pcntl_tick_handler);
266+
php_add_tick_function(pcntl_signal_dispatch);
266267

267268
return SUCCESS;
268269
}
@@ -644,6 +645,15 @@ PHP_FUNCTION(pcntl_signal)
644645
}
645646
/* }}} */
646647

648+
/* {{{ proto bool pcntl_signal_dispatch()
649+
Dispatch signals to signal handlers */
650+
PHP_FUNCTION(pcntl_signal_dispatch)
651+
{
652+
pcntl_signal_dispatch();
653+
RETURN_TRUE;
654+
}
655+
/* }}} */
656+
647657
#ifdef HAVE_GETPRIORITY
648658
/* {{{ proto int pcntl_getpriority([int pid [, int process_identifier]])
649659
Get the priority of any process */
@@ -747,7 +757,7 @@ static void pcntl_signal_handler(int signo)
747757
PCNTL_G(tail) = psig;
748758
}
749759

750-
void pcntl_tick_handler()
760+
void pcntl_signal_dispatch()
751761
{
752762
zval *param, **handle, *retval;
753763
struct php_pcntl_pending_signal *queue, *next;

ext/pcntl/php_pcntl.h

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ PHP_FUNCTION(pcntl_wexitstatus);
4444
PHP_FUNCTION(pcntl_wtermsig);
4545
PHP_FUNCTION(pcntl_wstopsig);
4646
PHP_FUNCTION(pcntl_signal);
47+
PHP_FUNCTION(pcntl_signal_dispatch);
4748
PHP_FUNCTION(pcntl_exec);
4849
#ifdef HAVE_GETPRIORITY
4950
PHP_FUNCTION(pcntl_getpriority);
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
pcnt_signal_dispatch()
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("pcntl")) print "skip";
6+
if (!function_exists("pcntl_signal")) print "skip pcntl_signal() not available";
7+
if (!function_exists("pcntl_signal_dispatch")) print "skip pcntl_signal_dispatch() not available";
8+
if (!function_exists("posix_kill")) print "skip posix_kill() not available";
9+
if (!function_exists("posix_getpid")) print "skip posix_getpid() not available";
10+
?>
11+
--FILE--
12+
<?php
13+
14+
pcntl_signal(SIGTERM, function ($signo) { echo "Signal handler called!\n"; });
15+
16+
echo "Start!\n";
17+
posix_kill(posix_getpid(), SIGTERM);
18+
$i = 0; // dummy
19+
pcntl_signal_dispatch();
20+
echo "Done!\n";
21+
22+
?>
23+
--EXPECTF--
24+
Start!
25+
Signal handler called!
26+
Done!

0 commit comments

Comments
 (0)