diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index f26bef3daa4e..7c05a1948fbf 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -2578,3 +2578,23 @@ PHP_FUNCTION(sys_getloadavg) } /* }}} */ #endif + +PHP_FUNCTION(num_available_processors) +{ + ZEND_PARSE_PARAMETERS_NONE(); + +#if defined(_SC_NPROCESSORS_ONLN) + int nprocs = sysconf(_SC_NPROCESSORS_ONLN); + if (nprocs > 0) { + RETURN_LONG(nprocs); + } +#elif defined _WIN32 && ! defined __CYGWIN__ + SYSTEM_INFO system_info; + GetSystemInfo (&system_info); + if (system_info.dwNumberOfProcessors > 0) { + RETURN_LONG(system_info.dwNumberOfProcessors); + } +#endif + + RETURN_NULL(); +} diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 09f63860f116..923422e0c7ce 100644 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -3834,3 +3834,5 @@ function sapi_windows_set_ctrl_handler(?callable $handler, bool $add = true): bo function sapi_windows_generate_ctrl_event(int $event, int $pid = 0): bool {} #endif + +function num_available_processors(): ?int {} \ No newline at end of file diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index d221a221f413..abb295aab884 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: dfd7d2cfd31312f7f6c5074c10cab54e9d1fbccc */ + * Stub hash: 1eda5d340b462c9d6fbb43d3e27aef20e334e1ab */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0) @@ -2184,6 +2184,9 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_sapi_windows_generate_ctrl_event ZEND_END_ARG_INFO() #endif +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_num_available_processors, 0, 0, IS_LONG, 1) +ZEND_END_ARG_INFO() + ZEND_FRAMELESS_FUNCTION(min, 2); static const zend_frameless_function_info frameless_function_infos_min[] = { @@ -2879,6 +2882,7 @@ ZEND_FUNCTION(sapi_windows_cp_is_utf8); ZEND_FUNCTION(sapi_windows_set_ctrl_handler); ZEND_FUNCTION(sapi_windows_generate_ctrl_event); #endif +ZEND_FUNCTION(num_available_processors); static const zend_function_entry ext_functions[] = { ZEND_FE(set_time_limit, arginfo_set_time_limit) @@ -3491,6 +3495,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(sapi_windows_set_ctrl_handler, arginfo_sapi_windows_set_ctrl_handler) ZEND_FE(sapi_windows_generate_ctrl_event, arginfo_sapi_windows_generate_ctrl_event) #endif + ZEND_FE(num_available_processors, arginfo_num_available_processors) ZEND_FE_END }; diff --git a/ext/standard/tests/general_functions/num_available_processors.phpt b/ext/standard/tests/general_functions/num_available_processors.phpt new file mode 100644 index 000000000000..e3fe40c119d5 --- /dev/null +++ b/ext/standard/tests/general_functions/num_available_processors.phpt @@ -0,0 +1,16 @@ +--TEST-- +num_available_processors() tests +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +int(%d) +Done