Skip to content

Commit

Permalink
Optimize code [2], add test for #5086
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jul 4, 2023
1 parent b10b534 commit 6f9e01a
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 62 deletions.
58 changes: 29 additions & 29 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ PHP_ARG_ENABLE([swoole-coro-time],
[whether to enable coroutine execution time ],
[AS_HELP_STRING([--enable-swoole-coro-time],
[Calculating coroutine execution time])], [no], [no])

define([PDO_ODBC_HELP_TEXT],[[
The include and lib dirs are looked for under 'dir'. The 'flavour' can be one
of: ibm-db2, iODBC, unixODBC, generic. If ',dir' part is omitted, default for
Expand Down Expand Up @@ -434,11 +434,11 @@ if test "$PHP_SWOOLE" != "no"; then
fi

dnl pgsql begin

if test "$PHP_SWOOLE_PGSQL" != "no"; then
dnl TODO macros below can be reused to find curl things
dnl prepare pkg-config

if test -z "$PKG_CONFIG"; then
AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
fi
Expand Down Expand Up @@ -505,76 +505,76 @@ EOF

AC_DEFINE(SW_USE_PGSQL, 1, [do we enable postgresql coro support])
fi

dnl pgsql end

dnl odbc begin

if test "$PHP_SWOOLE_ODBC" != "no"; then
PHP_CHECK_PDO_INCLUDES

AC_MSG_CHECKING([for selected PDO ODBC flavour])

pdo_odbc_flavour="`echo $PHP_SWOOLE_ODBC | cut -d, -f1`"
pdo_odbc_dir="`echo $PHP_SWOOLE_ODBC | cut -d, -f2`"

if test "$pdo_odbc_dir" = "$PHP_SWOOLE_ODBC" ; then
pdo_odbc_dir=
fi

case $pdo_odbc_flavour in
ibm-db2)
pdo_odbc_def_libdir=/home/db2inst1/sqllib/lib
pdo_odbc_def_incdir=/home/db2inst1/sqllib/include
pdo_odbc_def_lib=db2
;;

iODBC|iodbc)
pdo_odbc_def_libdir=/usr/local/$PHP_LIBDIR
pdo_odbc_def_incdir=/usr/local/include
pdo_odbc_def_lib=iodbc
;;

unixODBC|unixodbc)
pdo_odbc_def_libdir=/usr/local/$PHP_LIBDIR
pdo_odbc_def_incdir=/usr/local/include
pdo_odbc_def_lib=odbc
;;

ODBCRouter|odbcrouter)
pdo_odbc_def_libdir=/usr/$PHP_LIBDIR
pdo_odbc_def_incdir=/usr/include
pdo_odbc_def_lib=odbcsdk
;;

generic)
pdo_odbc_def_lib="`echo $PHP_SWOOLE_ODBC | cut -d, -f3`"
pdo_odbc_def_ldflags="`echo $PHP_SWOOLE_ODBC | cut -d, -f4`"
pdo_odbc_def_cflags="`echo $PHP_SWOOLE_ODBC | cut -d, -f5`"
pdo_odbc_flavour="generic-$pdo_odbc_def_lib"
;;

*)
AC_MSG_ERROR([Unknown ODBC flavour $pdo_odbc_flavour]PDO_ODBC_HELP_TEXT)
;;
esac

if test -n "$pdo_odbc_dir"; then
PDO_ODBC_INCDIR="$pdo_odbc_dir/include"
PDO_ODBC_LIBDIR="$pdo_odbc_dir/$PHP_LIBDIR"
else
PDO_ODBC_INCDIR="$pdo_odbc_def_incdir"
PDO_ODBC_LIBDIR="$pdo_odbc_def_libdir"
fi

AC_MSG_RESULT([$pdo_odbc_flavour
libs $PDO_ODBC_LIBDIR,
headers $PDO_ODBC_INCDIR])

if test ! -d "$PDO_ODBC_LIBDIR" ; then
AC_MSG_WARN([library dir $PDO_ODBC_LIBDIR does not exist])
fi

PDO_ODBC_CHECK_HEADER(odbc.h)
PDO_ODBC_CHECK_HEADER(odbcsdk.h)
PDO_ODBC_CHECK_HEADER(iodbc.h)
Expand All @@ -593,18 +593,18 @@ EOF
PDO_ODBC_CHECK_HEADER(cli0cli.h)
PDO_ODBC_CHECK_HEADER(cli0defs.h)
PDO_ODBC_CHECK_HEADER(cli0env.h)

if test "$php_pdo_have_header" != "yes"; then
AC_MSG_ERROR([Cannot find header file(s) for pdo_odbc])
fi

PDO_ODBC_INCLUDE="$pdo_odbc_def_cflags -I$PDO_ODBC_INCDIR -DPDO_ODBC_TYPE=\\\"$pdo_odbc_flavour\\\""
PDO_ODBC_LDFLAGS="$pdo_odbc_def_ldflags -L$PDO_ODBC_LIBDIR -l$pdo_odbc_def_lib"

PHP_EVAL_LIBLINE([$PDO_ODBC_LDFLAGS], [SWOOLE_SHARED_LIBADD])

EXTRA_CFLAGS="$EXTRA_CFLAGS -I$pdo_cv_inc_path $PDO_ODBC_INCLUDE"

dnl Check first for an ODBC 1.0 function to assert that the libraries work
PHP_CHECK_LIBRARY($pdo_odbc_def_lib, SQLBindCol,
[
Expand All @@ -621,10 +621,10 @@ EOF
],[
AC_MSG_ERROR([Your ODBC library does not exist or there was an error. Check config.log for more information])
], $PDO_ODBC_LDFLAGS)

AC_DEFINE(SW_USE_ODBC, 1, [do we enable swoole-odbc coro support])
fi

dnl odbc end

dnl SWOOLE_ORACLE start
Expand Down Expand Up @@ -1137,7 +1137,7 @@ EOF
thirdparty/nghttp2/nghttp2_hd_huffman.c \
thirdparty/nghttp2/nghttp2_hd_huffman_data.c"
fi

if test "$PHP_SWOOLE_PGSQL" != "no"; then
swoole_source_file="$swoole_source_file \
thirdparty/php80/pdo_pgsql/pgsql_driver.c \
Expand Down Expand Up @@ -1169,7 +1169,7 @@ EOF
thirdparty/php81/pdo_sqlite/sqlite_driver.c \
thirdparty/php81/pdo_sqlite/sqlite_statement.c"
fi

SW_ASM_DIR="thirdparty/boost/asm/"
SW_USE_ASM_CONTEXT="yes"

Expand Down Expand Up @@ -1260,7 +1260,7 @@ EOF
${SW_ASM_DIR}jump_${SW_CONTEXT_ASM_FILE} "
AC_DEFINE(SW_USE_ASM_CONTEXT, 1, [use boost asm context])
fi

EXTRA_CFLAGS="$EXTRA_CFLAGS -DENABLE_PHP_SWOOLE"

PHP_NEW_EXTENSION(swoole, $swoole_source_file, $ext_shared,, "$EXTRA_CFLAGS", cxx)
Expand Down
58 changes: 28 additions & 30 deletions core-tests/include/test_coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,66 @@
#include "swoole_coroutine_socket.h"
#include "swoole_coroutine_c_api.h"

namespace swoole { namespace test {
namespace swoole {
namespace test {

class coroutine
{
public:
coroutine(const CoroutineFunc &_fn, void *_arg, int *_complete_num) :
fn(_fn), arg(_arg), complete_num(_complete_num) { }
class coroutine {
public:
coroutine(const CoroutineFunc &_fn, void *_arg, int *_complete_num)
: fn(_fn), arg(_arg), complete_num(_complete_num) {}

void start()
{
void start() {
fn(arg);
(*complete_num)++;
}

inline static void create(const CoroutineFunc &fn, void *arg, int *complete_num)
{
inline static void create(const CoroutineFunc &fn, void *arg, int *complete_num) {
auto test = new coroutine(fn, arg, complete_num);

long cid = swoole::Coroutine::create([](void *arg)
{
((coroutine *) arg)->start();
delete (coroutine *) arg;
}, test);
long cid = swoole::Coroutine::create(
[](void *arg) {
((coroutine *) arg)->start();
delete (coroutine *) arg;
},
test);
ASSERT_GT(cid, 0);
}

inline static void run(std::initializer_list<std::pair<CoroutineFunc, void*>> args)
{
inline static void run(std::initializer_list<std::pair<CoroutineFunc, void *>> args) {
int complete_num = 0;
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
for (const auto &arg : args)
{
Coroutine::activate();
for (const auto &arg : args) {
create(arg.first, arg.second, &complete_num);
}
swoole_event_wait();
Coroutine::deactivate();
}

inline static void run(std::initializer_list<CoroutineFunc> fns)
{
inline static void run(std::initializer_list<CoroutineFunc> fns) {
int complete_num = 0;
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
for (const auto &fn : fns)
{
Coroutine::activate();
for (const auto &fn : fns) {
create(fn, nullptr, &complete_num);
}
swoole_event_wait();
Coroutine::deactivate();
}

inline static void run(const CoroutineFunc &fn, void *arg = nullptr)
{
inline static void run(const CoroutineFunc &fn, void *arg = nullptr) {
int complete_num = 0;
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
Coroutine::activate();
create(fn, arg, &complete_num);
swoole_event_wait();
Coroutine::deactivate();
}

private:
private:
CoroutineFunc fn;
void *arg;
int *complete_num;
};
}
}


} // namespace test
} // namespace swoole
12 changes: 10 additions & 2 deletions core-tests/src/network/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ TEST(client, udp) {
wait(&status);
}

TEST(client, async_tcp) {
static void test_async_client_tcp(const char *host, int port) {
pid_t pid;

Pipe p(true);
Expand Down Expand Up @@ -136,7 +136,7 @@ TEST(client, async_tcp) {
ac->close();
});

bool retval = ac.connect(TEST_HOST, TEST_PORT);
bool retval = ac.connect(host, port);
EXPECT_TRUE(retval);

swoole_event_wait();
Expand All @@ -146,6 +146,14 @@ TEST(client, async_tcp) {
wait(&status);
}

TEST(client, async_tcp) {
test_async_client_tcp(TEST_HOST, TEST_PORT);
}

TEST(client, async_tcp_dns) {
test_async_client_tcp("localhost", TEST_PORT);
}

TEST(client, connect_refuse) {
int ret;
Client cli(SW_SOCK_TCP, false);
Expand Down
2 changes: 1 addition & 1 deletion src/coroutine/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ std::string gethostbyname_impl_with_async(const std::string &hostname, int domai
swoole_set_last_error(ev.error);
return "";
} else {
std::string addr(dns_request.addr, dns_request.addr_len);
std::string addr(dns_request.addr);
return addr;
}
}
Expand Down
43 changes: 43 additions & 0 deletions tests/swoole_coroutine_wait_group/defer.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
swoole_coroutine_wait_group: run in defer
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

Co\run(function () {
$wg = new Swoole\Coroutine\WaitGroup();
foreach (range(1, 2) as $i) {
var_dump("add $i");
$wg->add();
Co\go(function () use ($wg, $i) {
var_dump("start $i");
defer(function () use ($wg, $i) {
var_dump("defer $i");
$wg->done();
var_dump("done $i");
});
var_dump("end $i");
});
}

var_dump("wait");
$wg->wait();
var_dump("finish");
});

?>
--EXPECT--
string(5) "add 1"
string(7) "start 1"
string(5) "end 1"
string(7) "defer 1"
string(6) "done 1"
string(5) "add 2"
string(7) "start 2"
string(5) "end 2"
string(7) "defer 2"
string(6) "done 2"
string(4) "wait"
string(6) "finish"

0 comments on commit 6f9e01a

Please sign in to comment.