Skip to content

ext/pcntl: adding pcntl_getcpu. #13908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions ext/pcntl/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ if test "$PHP_PCNTL" != "no"; then
AC_CHECK_FUNCS([sigaction], [], [AC_MSG_ERROR([pcntl: sigaction() not supported by this platform])])
AC_CHECK_FUNCS([getpriority setpriority wait3 wait4 sigwaitinfo sigtimedwait unshare rfork forkx pidfd_open sched_setaffinity])

dnl if unsupported, -1 means automatically ENOSYS in this context
AC_MSG_CHECKING([if sched_getcpu is supported])
AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <sched.h>
int main(void) {
if (sched_getcpu() == -1) {
return 1;
}
return 0;
}
]])],[
AC_MSG_RESULT(yes)
AC_DEFINE([HAVE_SCHED_GETCPU],1,[Whether sched_getcpu is properly supported])
],[
AC_MSG_RESULT(no)
],[
AC_MSG_RESULT([no, cross-compiling])
])

AC_CHECK_TYPE([siginfo_t],[PCNTL_CFLAGS="-DHAVE_STRUCT_SIGINFO_T"],,[#include <signal.h>])

PHP_NEW_EXTENSION(pcntl, pcntl.c php_signal.c, $ext_shared, cli, $PCNTL_CFLAGS -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
Expand Down
11 changes: 10 additions & 1 deletion ext/pcntl/pcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#endif

#include <errno.h>
#if defined(HAVE_UNSHARE) || defined(HAVE_SCHED_SETAFFINITY)
#if defined(HAVE_UNSHARE) || defined(HAVE_SCHED_SETAFFINITY) || defined(HAVE_SCHED_GETCPU)
#include <sched.h>
#if defined(__FreeBSD__)
#include <sys/types.h>
Expand Down Expand Up @@ -1613,6 +1613,15 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
}
#endif

#if defined(HAVE_SCHED_GETCPU)
PHP_FUNCTION(pcntl_getcpu)
{
ZEND_PARSE_PARAMETERS_NONE();

RETURN_LONG(sched_getcpu());
}
#endif

static void pcntl_interrupt_function(zend_execute_data *execute_data)
{
pcntl_signal_dispatch();
Expand Down
4 changes: 4 additions & 0 deletions ext/pcntl/pcntl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -999,3 +999,7 @@ function pcntl_setns(?int $process_id = null, int $nstype = CLONE_NEWNET): bool
function pcntl_getcpuaffinity(?int $process_id = null): array|false {}
function pcntl_setcpuaffinity(?int $process_id = null, array $cpu_ids = []): bool {}
#endif

#ifdef HAVE_SCHED_GETCPU
function pcntl_getcpu(): int {}
#endif
13 changes: 12 additions & 1 deletion ext/pcntl/pcntl_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions ext/pcntl/tests/pcntl_getcpu.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
pcntl_getcpu()
--EXTENSIONS--
pcntl
--SKIPIF--
<?php
if (!function_exists("pcntl_getcpu")) die("skip pcntl_getcpu() is not available");
if (!function_exists("pcntl_setcpuaffinity")) die("skip pcntl_setcpuaffinity() is not available");
if (getenv('TRAVIS')) die('skip Currently fails on Travis');
?>
--FILE--
<?php
$pid = pcntl_fork();
if ($pid == -1) {
die("fork failed");
} else if ($pid == 0) {
var_dump(pcntl_setcpuaffinity(null, [1]));
var_dump(pcntl_getcpu());
}
?>
--EXPECTF--
bool(true)
int(1)