From 8bd13c710e788ed70f038e66048acf78ac8b042d Mon Sep 17 00:00:00 2001 From: Justin Boswell Date: Thu, 4 Mar 2021 05:46:13 -0800 Subject: [PATCH] Added EventLoopGroup API to native extension (#15) --- Makefile.frag | 16 ++++++++-------- composer.lock | 4 ++-- ext/awscrt.stub.php | 4 ++++ ext/awscrt_arginfo.h | 14 +++++++++++++- ext/crt.c | 22 ++++++++++++++++++++++ src/AWS/CRT/CRT.php | 15 +++++++++++---- tests/aws_crt_event_loop_group.phpt | 16 ++++++++++++++++ 7 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 tests/aws_crt_event_loop_group.phpt diff --git a/Makefile.frag b/Makefile.frag index acee1c6..b1d8389 100644 --- a/Makefile.frag +++ b/Makefile.frag @@ -37,15 +37,15 @@ extension: ext/crt.lo # Force the crt object target to depend on the CRT static library ext/crt.lo: $(BUILD_DIR)/aws-crt-ffi-static/libaws-crt-ffi.a ext/api.h ext/awscrt_arginfo.h -ifeq ($(AT_LEAST_PHP7),1) - GEN_STUB=build/gen_stub.php - # generate awscrt_arginfo.h - ext/awscrt_arginfo.h: ext/awscrt.stub.php $(GEN_STUB) - php $(GEN_STUB) ext/awscrt.stub.php +# borrow the gen_stub script from PHP's build process +GEN_STUB=build/gen_stub.php +$(GEN_STUB): + curl -o $(GEN_STUB) -sSL https://raw.githubusercontent.com/php/php-src/bbb86ba7e2fe8ae365294d1834c6a392570a9dcd/build/gen_stub.php - # borrow the gen_stub script from PHP's build process - $(GEN_STUB): - curl -o $(GEN_STUB) -sSL https://raw.githubusercontent.com/php/php-src/bbb86ba7e2fe8ae365294d1834c6a392570a9dcd/build/gen_stub.php +ext/awscrt_arginfo.h: ext/awscrt.stub.php $(GEN_STUB) +ifeq ($(AT_LEAST_PHP7),1) + # generate awscrt_arginfo.h + php $(GEN_STUB) ext/awscrt.stub.php endif # transform/install api.h from FFI lib diff --git a/composer.lock b/composer.lock index 00d6ea5..accfcb2 100644 --- a/composer.lock +++ b/composer.lock @@ -1343,7 +1343,7 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.22.0", + "version": "v1.22.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", @@ -1402,7 +1402,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.22.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.22.1" }, "funding": [ { diff --git a/ext/awscrt.stub.php b/ext/awscrt.stub.php index 72184db..d8dc24a 100644 --- a/ext/awscrt.stub.php +++ b/ext/awscrt.stub.php @@ -9,3 +9,7 @@ function aws_crt_error_name(int $error_code): string {} function aws_crt_error_str(int $error_code): string {} function aws_crt_error_debug_str(int $error_code): string {} + +function aws_crt_event_loop_group_new(int $max_threads): int {} + +function aws_crt_event_loop_group_release(int $event_loop_group): void {} diff --git a/ext/awscrt_arginfo.h b/ext/awscrt_arginfo.h index 7cc8f99..53d0dc0 100644 --- a/ext/awscrt_arginfo.h +++ b/ext/awscrt_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: a629a07df494f8ae6563949b2819c833e9d73f9a */ + * Stub hash: 04ff04fec5e4293918658ac98394d98387fa977b */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_last_error, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -12,11 +12,21 @@ ZEND_END_ARG_INFO() #define arginfo_aws_crt_error_debug_str arginfo_aws_crt_error_name +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_new, 0, 1, IS_LONG, 0) + ZEND_ARG_TYPE_INFO(0, max_threads, IS_LONG, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_aws_crt_event_loop_group_release, 0, 1, IS_VOID, 0) + ZEND_ARG_TYPE_INFO(0, event_loop_group, IS_LONG, 0) +ZEND_END_ARG_INFO() + ZEND_FUNCTION(aws_crt_last_error); ZEND_FUNCTION(aws_crt_error_name); ZEND_FUNCTION(aws_crt_error_str); ZEND_FUNCTION(aws_crt_error_debug_str); +ZEND_FUNCTION(aws_crt_event_loop_group_new); +ZEND_FUNCTION(aws_crt_event_loop_group_release); static const zend_function_entry ext_functions[] = { @@ -24,5 +34,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(aws_crt_error_name, arginfo_aws_crt_error_name) ZEND_FE(aws_crt_error_str, arginfo_aws_crt_error_str) ZEND_FE(aws_crt_error_debug_str, arginfo_aws_crt_error_debug_str) + ZEND_FE(aws_crt_event_loop_group_new, arginfo_aws_crt_event_loop_group_new) + ZEND_FE(aws_crt_event_loop_group_release, arginfo_aws_crt_event_loop_group_release) ZEND_FE_END }; diff --git a/ext/crt.c b/ext/crt.c index d915b05..cd945d8 100644 --- a/ext/crt.c +++ b/ext/crt.c @@ -81,6 +81,28 @@ PHP_FUNCTION(aws_crt_error_debug_str) { AWS_RETURN_STRING(aws_crt_error_debug_str(error_code)); } +PHP_FUNCTION(aws_crt_event_loop_group_new) { + zend_ulong num_threads = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &num_threads) == FAILURE) { + RETURN_NULL(); + } + + struct aws_event_loop_group *elg = aws_crt_event_loop_group_new(num_threads); + RETURN_LONG((zend_ulong)elg); +} + +PHP_FUNCTION(aws_crt_event_loop_group_release) { + zend_ulong php_elg = 0; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &php_elg) == FAILURE) { + return; + } + + struct aws_event_loop_group *elg = (void *)php_elg; + aws_crt_event_loop_group_release(elg); +} + zend_module_entry awscrt_module_entry = { STANDARD_MODULE_HEADER, "awscrt", diff --git a/src/AWS/CRT/CRT.php b/src/AWS/CRT/CRT.php index b4bde63..510c406 100644 --- a/src/AWS/CRT/CRT.php +++ b/src/AWS/CRT/CRT.php @@ -42,7 +42,7 @@ final class CRT { /** * @return integer last error code reported within the CRT */ - public static function last_error() { + public static function last_error(): int { return self::$impl->aws_crt_last_error(); } @@ -50,7 +50,7 @@ final class CRT { * @param integer $error Error code from the CRT, usually delivered via callback or {@see last_error} * @return string Human-readable description of the provided error code */ - public static function error_str($error) { + public static function error_str(int $error) : string { return self::$impl->aws_crt_error_str((int) $error); } @@ -58,14 +58,21 @@ final class CRT { * @param integer $error Error code from the CRT, usually delivered via callback or {@see last_error} * @return string Name/enum identifier for the provided error code */ - public static function error_name($error) { + public static function error_name(int $error) : string { return self::$impl->aws_crt_error_name((int) $error); } - function event_loop_group_new($num_threads) { + /** + * @param integer $num_threads Maximum threads to use in the event loop group + * @return object Pointer to the new event loop group + */ + function event_loop_group_new(int $num_threads) { return self::$impl->aws_crt_event_loop_group_new($num_threads); } + /** + * @param object $elg Pointer to the event loop group to release + */ function event_loop_group_release($elg) { return self::$impl->aws_crt_event_loop_group_release($elg); } diff --git a/tests/aws_crt_event_loop_group.phpt b/tests/aws_crt_event_loop_group.phpt new file mode 100644 index 0000000..a549a23 --- /dev/null +++ b/tests/aws_crt_event_loop_group.phpt @@ -0,0 +1,16 @@ +--TEST-- +aws_crt_event_loop_group +--SKIPIF-- + +--FILE-- + +--EXPECTREGEX-- +int\(\d+\)