From 268acd54ed14b1f3be5b989f18a1f4c8da77669b Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Thu, 14 May 2015 15:05:00 -0700 Subject: [PATCH] Made PHP library use only a single completion queue --- src/php/ext/grpc/call.c | 17 +++------- src/php/ext/grpc/call.h | 4 +-- src/php/ext/grpc/completion_queue.c | 50 +++++++++++++++++++++++++++++ src/php/ext/grpc/completion_queue.h | 50 +++++++++++++++++++++++++++++ src/php/ext/grpc/config.m4 | 2 +- src/php/ext/grpc/php_grpc.c | 3 ++ src/php/ext/grpc/server.c | 19 +++-------- src/php/ext/grpc/server.h | 1 - 8 files changed, 114 insertions(+), 32 deletions(-) create mode 100644 src/php/ext/grpc/completion_queue.c create mode 100644 src/php/ext/grpc/completion_queue.h diff --git a/src/php/ext/grpc/call.c b/src/php/ext/grpc/call.c index 9cc4fd7f39f..9f651ff56f6 100644 --- a/src/php/ext/grpc/call.c +++ b/src/php/ext/grpc/call.c @@ -52,6 +52,7 @@ #include #include +#include "completion_queue.h" #include "timeval.h" #include "channel.h" #include "byte_buffer.h" @@ -62,13 +63,6 @@ zend_class_entry *grpc_ce_call; void free_wrapped_grpc_call(void *object TSRMLS_DC) { wrapped_grpc_call *call = (wrapped_grpc_call *)object; if (call->owned && call->wrapped != NULL) { - if (call->queue != NULL) { - grpc_completion_queue_shutdown(call->queue); - while (grpc_completion_queue_next(call->queue, gpr_inf_future).type != - GRPC_QUEUE_SHUTDOWN) - ; - grpc_completion_queue_destroy(call->queue); - } grpc_call_destroy(call->wrapped); } efree(call); @@ -95,15 +89,13 @@ zend_object_value create_wrapped_grpc_call(zend_class_entry *class_type /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the struct should be destroyed at the end of the object's lifecycle */ -zval *grpc_php_wrap_call(grpc_call *wrapped, grpc_completion_queue *queue, - bool owned) { +zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned) { zval *call_object; MAKE_STD_ZVAL(call_object); object_init_ex(call_object, grpc_ce_call); wrapped_grpc_call *call = (wrapped_grpc_call *)zend_object_store_get_object(call_object TSRMLS_CC); call->wrapped = wrapped; - call->queue = queue; return call_object; } @@ -247,9 +239,8 @@ PHP_METHOD(Call, __construct) { wrapped_grpc_timeval *deadline = (wrapped_grpc_timeval *)zend_object_store_get_object( deadline_obj TSRMLS_CC); - call->queue = grpc_completion_queue_create(); call->wrapped = grpc_channel_create_call( - channel->wrapped, call->queue, method, channel->target, + channel->wrapped, completion_queue, method, channel->target, deadline->wrapped); } @@ -415,7 +406,7 @@ PHP_METHOD(Call, startBatch) { (long)error TSRMLS_CC); goto cleanup; } - event = grpc_completion_queue_pluck(call->queue, call->wrapped, + event = grpc_completion_queue_pluck(completion_queue, call->wrapped, gpr_inf_future); if (!event.success) { zend_throw_exception(spl_ce_LogicException, diff --git a/src/php/ext/grpc/call.h b/src/php/ext/grpc/call.h index e7eb9a75e2e..f3ef89dc976 100644 --- a/src/php/ext/grpc/call.h +++ b/src/php/ext/grpc/call.h @@ -54,15 +54,13 @@ typedef struct wrapped_grpc_call { bool owned; grpc_call *wrapped; - grpc_completion_queue *queue; } wrapped_grpc_call; /* Initializes the Call PHP class */ void grpc_init_call(TSRMLS_D); /* Creates a Call object that wraps the given grpc_call struct */ -zval *grpc_php_wrap_call(grpc_call *wrapped, grpc_completion_queue *queue, - bool owned); +zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned); /* Creates and returns a PHP associative array of metadata from a C array of * call metadata */ diff --git a/src/php/ext/grpc/completion_queue.c b/src/php/ext/grpc/completion_queue.c new file mode 100644 index 00000000000..b24c83757ca --- /dev/null +++ b/src/php/ext/grpc/completion_queue.c @@ -0,0 +1,50 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "completion_queue.h" + +#include + +grpc_completion_queue *completion_queue; + +void grpc_php_init_completion_queue(TSRMLS_D) { + completion_queue = grpc_completion_queue_create(); +} + +void grpc_php_shutdown_completion_queue(TSRMLS_D) { + grpc_completion_queue_shutdown(completion_queue); + while (grpc_completion_queue_next(completion_queue, gpr_inf_future).type != + GRPC_QUEUE_SHUTDOWN) + ; + grpc_completion_queue_destroy(completion_queue); +} diff --git a/src/php/ext/grpc/completion_queue.h b/src/php/ext/grpc/completion_queue.h new file mode 100644 index 00000000000..d5dac4f0cf3 --- /dev/null +++ b/src/php/ext/grpc/completion_queue.h @@ -0,0 +1,50 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPC_PHP_GRPC_COMPLETION_QUEUE_H_ +#define GRPC_PHP_GRPC_COMPLETION_QUEUE_H_ + +#include + +#include + +/* The global completion queue for all operations */ +extern grpc_completion_queue *completion_queue; + +/* Initializes the completion queue */ +void grpc_php_init_completion_queue(TSRMLS_D); + +/* Shut down the completion queue */ +void grpc_php_shutdown_completion_queue(TSRMLS_D); + +#endif /* GRPC_PHP_GRPC_COMPLETION_QUEUE_H_ */ diff --git a/src/php/ext/grpc/config.m4 b/src/php/ext/grpc/config.m4 index 11778e3bb64..b485aabf404 100755 --- a/src/php/ext/grpc/config.m4 +++ b/src/php/ext/grpc/config.m4 @@ -66,5 +66,5 @@ if test "$PHP_GRPC" != "no"; then PHP_SUBST(GRPC_SHARED_LIBADD) - PHP_NEW_EXTENSION(grpc, byte_buffer.c call.c channel.c credentials.c timeval.c server.c server_credentials.c php_grpc.c, $ext_shared, , -Wall -Werror -std=c11) + PHP_NEW_EXTENSION(grpc, byte_buffer.c call.c channel.c completion_queue.c credentials.c timeval.c server.c server_credentials.c php_grpc.c, $ext_shared, , -Wall -Werror -std=c11) fi diff --git a/src/php/ext/grpc/php_grpc.c b/src/php/ext/grpc/php_grpc.c index 3e669ecd174..fedcf0f5392 100644 --- a/src/php/ext/grpc/php_grpc.c +++ b/src/php/ext/grpc/php_grpc.c @@ -37,6 +37,7 @@ #include "timeval.h" #include "credentials.h" #include "server_credentials.h" +#include "completion_queue.h" #ifdef HAVE_CONFIG_H #include "config.h" @@ -188,6 +189,7 @@ PHP_MINIT_FUNCTION(grpc) { grpc_init_timeval(TSRMLS_C); grpc_init_credentials(TSRMLS_C); grpc_init_server_credentials(TSRMLS_C); + grpc_php_init_completion_queue(TSRMLS_C); return SUCCESS; } /* }}} */ @@ -199,6 +201,7 @@ PHP_MSHUTDOWN_FUNCTION(grpc) { UNREGISTER_INI_ENTRIES(); */ grpc_shutdown_timeval(TSRMLS_C); + grpc_php_shutdown_completion_queue(TSRMLS_C); grpc_shutdown(); return SUCCESS; } diff --git a/src/php/ext/grpc/server.c b/src/php/ext/grpc/server.c index c2e00b16f44..b7995b6b8bd 100644 --- a/src/php/ext/grpc/server.c +++ b/src/php/ext/grpc/server.c @@ -51,6 +51,7 @@ #include #include +#include "completion_queue.h" #include "server.h" #include "channel.h" #include "server_credentials.h" @@ -61,13 +62,6 @@ zend_class_entry *grpc_ce_server; /* Frees and destroys an instance of wrapped_grpc_server */ void free_wrapped_grpc_server(void *object TSRMLS_DC) { wrapped_grpc_server *server = (wrapped_grpc_server *)object; - if (server->queue != NULL) { - grpc_completion_queue_shutdown(server->queue); - while (grpc_completion_queue_next(server->queue, gpr_inf_future).type != - GRPC_QUEUE_SHUTDOWN) - ; - grpc_completion_queue_destroy(server->queue); - } if (server->wrapped != NULL) { grpc_server_shutdown(server->wrapped); grpc_server_destroy(server->wrapped); @@ -96,7 +90,6 @@ zend_object_value create_wrapped_grpc_server(zend_class_entry *class_type /** * Constructs a new instance of the Server class - * @param CompletionQueue $queue The completion queue to use with the server * @param array $args The arguments to pass to the server (optional) */ PHP_METHOD(Server, __construct) { @@ -112,7 +105,6 @@ PHP_METHOD(Server, __construct) { 1 TSRMLS_CC); return; } - server->queue = grpc_completion_queue_create(); if (args_array == NULL) { server->wrapped = grpc_server_create(NULL); } else { @@ -120,7 +112,7 @@ PHP_METHOD(Server, __construct) { server->wrapped = grpc_server_create(&args); efree(args.args); } - grpc_server_register_completion_queue(server->wrapped, server->queue); + grpc_server_register_completion_queue(server->wrapped, completion_queue); } /** @@ -144,21 +136,20 @@ PHP_METHOD(Server, requestCall) { grpc_metadata_array_init(&metadata); error_code = grpc_server_request_call(server->wrapped, &call, &details, &metadata, - server->queue, server->queue, NULL); + completion_queue, completion_queue, NULL); if (error_code != GRPC_CALL_OK) { zend_throw_exception(spl_ce_LogicException, "request_call failed", (long)error_code TSRMLS_CC); goto cleanup; } - event = grpc_completion_queue_pluck(server->queue, NULL, gpr_inf_future); + event = grpc_completion_queue_pluck(completion_queue, NULL, gpr_inf_future); if (!event.success) { zend_throw_exception(spl_ce_LogicException, "Failed to request a call for some reason", 1 TSRMLS_CC); goto cleanup; } - add_property_zval(result, "call", grpc_php_wrap_call(call, server->queue, - true)); + add_property_zval(result, "call", grpc_php_wrap_call(call, true)); add_property_string(result, "method", details.method, true); add_property_string(result, "host", details.host, true); add_property_zval(result, "absolute_deadline", diff --git a/src/php/ext/grpc/server.h b/src/php/ext/grpc/server.h index a2ee2ff5a92..022257f37c3 100755 --- a/src/php/ext/grpc/server.h +++ b/src/php/ext/grpc/server.h @@ -53,7 +53,6 @@ typedef struct wrapped_grpc_server { zend_object std; grpc_server *wrapped; - grpc_completion_queue *queue; } wrapped_grpc_server; /* Initializes the Server class */