Skip to content

Commit

Permalink
Improve swoole event wait and add a test (#2144).
Browse files Browse the repository at this point in the history
  • Loading branch information
twose committed Nov 26, 2018
1 parent 707ab3b commit ba371ee
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 18 deletions.
3 changes: 2 additions & 1 deletion php_swoole.h
Expand Up @@ -444,7 +444,8 @@ static sw_inline void php_swoole_check_reactor()
}

void php_swoole_check_aio();
void php_swoole_at_shutdown(char *function);
void php_swoole_register_shutdown_function(char *function);
void php_swoole_register_shutdown_function_prepend(char *function);
void php_swoole_event_init();
void php_swoole_event_wait();
void php_swoole_event_exit();
Expand Down
35 changes: 34 additions & 1 deletion swoole.c
Expand Up @@ -801,6 +801,39 @@ int swoole_register_rshutdown_function(swCallback func, int push_back)
}
}

void php_swoole_register_shutdown_function(char *function)
{
php_shutdown_function_entry shutdown_function_entry;
shutdown_function_entry.arg_count = 1;
shutdown_function_entry.arguments = (zval *) safe_emalloc(sizeof(zval), 1, 0);
ZVAL_STRING(&shutdown_function_entry.arguments[0], function);
register_user_shutdown_function(function, ZSTR_LEN(Z_STR(shutdown_function_entry.arguments[0])), &shutdown_function_entry);
}

static void php_swoole_old_shutdown_function_move(zval *zv)
{
php_shutdown_function_entry *old_shutdown_function_entry = Z_PTR_P(zv);
zend_hash_next_index_insert_mem(BG(user_shutdown_function_names), old_shutdown_function_entry, sizeof(php_shutdown_function_entry));
efree(old_shutdown_function_entry);
}

void php_swoole_register_shutdown_function_prepend(char *function)
{
HashTable *old_user_shutdown_function_names = BG(user_shutdown_function_names);
if (!old_user_shutdown_function_names)
{
php_swoole_register_shutdown_function(function);
return;
}
BG(user_shutdown_function_names) = NULL;
php_swoole_register_shutdown_function(function);
zend_try {
old_user_shutdown_function_names->pDestructor = php_swoole_old_shutdown_function_move;
zend_hash_destroy(old_user_shutdown_function_names);
FREE_HASHTABLE(old_user_shutdown_function_names);
} zend_end_try();
}

void swoole_call_rshutdown_function(void *arg)
{
if (SWOOLE_G(rshutdown_functions))
Expand Down Expand Up @@ -1288,7 +1321,7 @@ PHP_MINFO_FUNCTION(swoole)
PHP_RINIT_FUNCTION(swoole)
{
SWOOLE_G(req_status) = PHP_SWOOLE_RINIT_BEGIN;
php_swoole_at_shutdown("swoole_call_user_shutdown_begin");
php_swoole_register_shutdown_function("swoole_call_user_shutdown_begin");
//running
SwooleG.running = 1;

Expand Down
15 changes: 0 additions & 15 deletions swoole_client.c
Expand Up @@ -753,21 +753,6 @@ void php_swoole_client_check_setting(swClient *cli, zval *zset)
#endif
}

void php_swoole_at_shutdown(char *function)
{
php_shutdown_function_entry shutdown_function_entry;
shutdown_function_entry.arg_count = 1;
shutdown_function_entry.arguments = (zval *) safe_emalloc(sizeof(zval), 1, 0);
ZVAL_STRING(&shutdown_function_entry.arguments[0], function);

if (!register_user_shutdown_function(function, ZSTR_LEN(Z_STR(shutdown_function_entry.arguments[0])), &shutdown_function_entry))
{
zval_ptr_dtor(&shutdown_function_entry.arguments[0]);
efree(shutdown_function_entry.arguments);
swoole_php_fatal_error(E_WARNING, "Unable to register shutdown function [%s]",function);
}
}

void php_swoole_client_free(zval *zobject, swClient *cli)
{
if (cli->timer)
Expand Down
2 changes: 1 addition & 1 deletion swoole_event.c
Expand Up @@ -225,7 +225,7 @@ void php_swoole_reactor_init()
SwooleWG.reactor_wait_onexit = 1;
SwooleWG.reactor_ready = 0;
//only client side
php_swoole_at_shutdown("swoole_event_wait");
php_swoole_register_shutdown_function_prepend("swoole_event_wait");
}

SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_USER | SW_EVENT_READ, php_swoole_event_onRead);
Expand Down
48 changes: 48 additions & 0 deletions tests/swoole_event/swoole_event_wait.phpt
@@ -0,0 +1,48 @@
--TEST--
swoole_event: swoole_event_wait (auto)
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';
register_shutdown_function(function () {
echo "register 1\n";
register_shutdown_function(function () {
echo "register 5\n";
register_shutdown_function(function () {
echo "register 7\n";
});
});
});

go(function () {
co::sleep(0.1);
register_shutdown_function(function () {
echo "register 3\n";
});
echo "swoole event wait is always the first one\n";
register_shutdown_function(function () {
echo "register 4\n";
});
});

register_shutdown_function(function () {
echo "register 2\n";
register_shutdown_function(function () {
echo "register 6\n";
register_shutdown_function(function () {
echo "register 8\n";
});
});
});
?>
--EXPECT--
swoole event wait is always the first one
register 1
register 2
register 3
register 4
register 5
register 6
register 7
register 8

0 comments on commit ba371ee

Please sign in to comment.