Skip to content

Commit

Permalink
Merge pull request #1 from idealvin/master
Browse files Browse the repository at this point in the history
sync from upstream
  • Loading branch information
izhengfan committed Dec 27, 2019
2 parents e636785 + e2de25e commit 88288bd
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 158 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.xmake
build
lib
18 changes: 9 additions & 9 deletions base/co/context/context_x64.asm
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ extern _exit:proc
; ---------------------------------------
; | r12 | r13 | r14 | r15 |
; ---------------------------------------
; 32 40 48 56 ---------------------------------------
; | |
; func __end | __entry arguments retval(from)
; --------------------------------------------------------------------------------------------------------------------------
; | rdi | rsi | rbx | rbp | retval(saved) | rip | unused | context(unused) | priv(unused) | padding |
; --------------------------------------------------------------------------------------------------------------------------
; 64 72 80 88 96 104 112 120 128 136
; 32 40 48 56 -------------------------------------------------------
; | |
; func __end | __entry arguments retval(from)
; ----------------------------------------------------------------------------------------------------------------------------------
; | rdi | rsi | rbx | rbp | retval(saved) | rip | from(ctx/priv) | context(unused) | priv(unused) | padding |
; ----------------------------------------------------------------------------------------------------------------------------------
; 64 72 80 88 96 104 112 128 136 144
; |
; | 16-align
; |
Expand All @@ -87,8 +87,8 @@ tb_context_make proc frame
add rax, rdx

; reserve space for first argument(from) and retval(from) item of context-function
; 3 * 8 = 24
sub rax, 24
; 4 * 8 = 32
sub rax, 32

; 16-align of the stack top address
and rax, -16
Expand Down
8 changes: 2 additions & 6 deletions base/co/impl/hook_unix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,10 @@ int poll(struct pollfd* fds, nfds_t nfds, int ms) {
if (ms > 0) id = gSched->add_ev_timer(ms);

gSched->yield();
if (ms > 0 && gSched->timeout()) {
gSched->del_event(fd);
return 0;
}
gSched->del_event(fd);
if (ms > 0 && gSched->timeout()) return 0;

if (id != co::null_timer_id) gSched->del_timer(id);
if (fds[0].events == POLLOUT) gSched->del_ev_write(fd);

fds[0].revents = fds[0].events;
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion base/co/impl/scheduler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void Scheduler::check_timeout(std::vector<Coroutine*>& res) {
}

if (it != _timed_wait.begin()) {
if (_it != _timed_wait.end() && _it->first > now_ms) {
if (_it != _timed_wait.end() && _it->first <= now_ms) {
_it = it;
}
_timed_wait.erase(_timed_wait.begin(), it);
Expand Down
12 changes: 1 addition & 11 deletions base/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
target("base")
set_kind("static")
add_cxxflags("-std=c++11")
set_optimize("faster") -- -O2
set_warnings("all") -- -Wall
set_targetdir("../lib")
add_includedirs("..", {interface = true})
add_files("**.cc")

if is_plat("macosx", "linux") then
add_cxflags("-g3")
if is_plat("macosx") then
add_cxflags("-fno-pie")
end
add_files("co/context/context.S")
end

if is_plat("windows") then
add_cxflags("-Ox", "-fp:fast", "-EHsc")
add_files("**.cpp")
if is_arch("x64") then
add_files("co/context/context_x64.asm")
Expand All @@ -24,6 +17,3 @@ target("base")
end
end

after_build(function ()
os.rm("build")
end)
37 changes: 15 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ export XMAKE_ROOT=y
[co/base](https://github.com/idealvin/co/tree/master/base) 是 CO 提供的核心基础库,其他工具都依赖于 base 库。

```sh
# 在 co/lib 目录下生成 libbase.a 或 base.lib
cd co/base
# 在 lib 目录下生成 libbase.a 或 base.lib
xmake
```

Expand All @@ -119,46 +118,40 @@ xmake
[co/unitest](https://github.com/idealvin/co/tree/master/unitest/base) 是单元测试代码,用于检验 base 库功能的正确性。

```sh
# 在 co/build 目录下生成可执行文件 unitest 或 unitest.exe
cd co/unitest/base
xmake
# 在 build 目录下生成可执行文件 unitest 或 unitest.exe
xmake build unittest

cd ../../build
./unitest -a # 执行所有单元测试
./unitest -os # 执行 os 单元测试
./unitest -json # 执行 json 单元测试
xmake run unitest -a # 执行所有单元测试
xmake run unitest -os # 执行 os 单元测试
xmake run unitest -json # 执行 json 单元测试
```

- 编译 co/test

[co/test](https://github.com/idealvin/co/tree/master/test) 包含了一些测试代码。

```sh
# 在 co/build 目录下生成相应的可执行文件
cd co/test
xmake # 编译 test 目录下的全部目标代码
xmake -b log # 编译 log_test.cc
xmake -b flag # 编译 flag_test.cc
# 在 build 目录下生成相应的可执行文件
xmake --all # 编译 test 目录下的全部目标代码
xmake build log # 编译 log_test.cc
xmake build flag # 编译 flag_test.cc

cd ../build
./log.exe -perf # log 库性能测试
./rpc.exe -c=0 # 启动 rpc server
./rpc.exe -c=1 # 启动 rpc client
xmake run log -perf # log 库性能测试
xmake run rpc -c=0 # 启动 rpc server
xmake run rpc -c=1 # 启动 rpc client
```

- 编译 rpcgen

[rpcgen](https://github.com/idealvin/co/tree/master/rpcgen)`json rpc` 的代码生成器,根据指定的 proto 文件,自动生成相应的代码。

```sh
# 在 co/build 目录下生成 rpcgen 或 rpcgen.exe
cd co/rpcgen
xmake
# 在 build 目录下生成 rpcgen 或 rpcgen.exe
xmake rpcgen

# 建议将 rpcgen 放到系统目录下(/usr/local/bin/).
# 有些 linux 系统自带了一个 rpcgen,为避免冲突,可能需要重命名 rpcgen.
rpcgen hello_world.proto
xmake run rpcgen test/rpc/hello_world.proto
```

`proto` 文件格式可以参考 [co/test/rpc/hello_world.proto](https://github.com/idealvin/co/blob/master/test/rpc/hello_world.proto)
Expand Down
24 changes: 2 additions & 22 deletions rpcgen/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
target("rpcgen")
set_kind("binary")
add_cxxflags("-std=c++11")
set_optimize("faster") -- -O2
set_warnings("all") -- -Wall
set_targetdir("../build")
add_includedirs("..")
add_linkdirs("../lib")
add_links("base")
add_deps("base")
add_files("*.cc")
set_rundir("$(projectdir)")

if is_plat("macosx", "linux") then
add_cxflags("-g3")
if is_plat("macosx") then
add_cxflags("-fno-pie")
end
add_syslinks("pthread", "dl")
end

if is_plat("windows") then
add_cxflags("-Ox", "-fp:fast", "-EHsc")
end

after_build(function ()
os.rm("build")
end)
18 changes: 11 additions & 7 deletions test/rpc_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ DEF_string(user, "", "username");
DEF_string(passwd, "", "passwd");
DEF_string(serv_ip, "127.0.0.1", "server ip");
DEF_bool(ping, false, "test rpc ping");
DEF_int32(hb, 10000, "heartbeat");

namespace xx {

Expand Down Expand Up @@ -48,9 +49,10 @@ void client_fun() {

void test_ping() {
rpc::Client* c = rpc::new_client(FLG_serv_ip.c_str(), 7788, FLG_passwd.c_str());
c->ping();
co::sleep(12000);
c->ping();
while (true) {
c->ping();
co::sleep(FLG_hb);
}
delete c;
}

Expand All @@ -63,11 +65,13 @@ int main(int argc, char** argv) {
server->start();

} else {
for (int i = 0; i < FLG_conn; ++i) {
go(&client_fun);
if (FLG_ping) {
go(&test_ping);
} else {
for (int i = 0; i < FLG_conn; ++i) {
go(&client_fun);
}
}

if (FLG_ping) go(&test_ping);
}

while (true) {
Expand Down
66 changes: 8 additions & 58 deletions test/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,61 +1,11 @@
set_kind("binary")
add_cxxflags("-std=c++11")
set_optimize("faster") -- -O2
set_warnings("all") -- -Wall
set_targetdir("../build")
add_includedirs("..")
add_linkdirs("../lib")
--add_links("base", "jemalloc")
add_links("base")

after_build(function ()
os.rm("build")
end)

if is_plat("macosx", "linux") then
add_cxflags("-g3")
if is_plat("macosx") then
add_cxflags("-fno-pie")
end
add_syslinks("pthread", "dl")
end

if is_plat("windows") then
add_cxflags("-Ox", "-fp:fast", "-EHsc")
for _, test in ipairs({"co", "fast", "flag", "hash",
"json", "rapidjson", "log", "rpc", "str", "time", "tw", "xx"}) do
target(test)
set_kind("binary")
set_default(false)
add_deps("base")
-- add_links("jemalloc")
add_files(test .. "_test.cc")
end

target("co")
add_files("co_test.cc")

target("fast")
add_files("fast_test.cc")

target("flag")
add_files("flag_test.cc")

target("hash")
add_files("hash_test.cc")

target("json")
add_files("json_test.cc")

target("rapidjson")
add_files("rapidjson_test.cc")

target("log")
add_files("log_test.cc")

target("rpc")
add_files("rpc_test.cc")

target("str")
add_files("str_test.cc")

target("time")
add_files("time_test.cc")

target("tw")
add_files("tw_test.cc")

target("xx")
add_files("xx_test.cc")
24 changes: 2 additions & 22 deletions unitest/base/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
target("unitest")
set_kind("binary")
add_cxxflags("-std=c++11")
set_optimize("faster") -- -O2
set_warnings("all") -- -Wall
set_targetdir("../../build")
add_includedirs("../..")
add_linkdirs("../../lib")
add_links("base")
set_default(false)
add_deps("base")
add_files("*.cc")

if is_plat("macosx", "linux") then
add_cxflags("-g3")
if is_plat("macosx") then
add_cxflags("-fno-pie")
end
add_syslinks("pthread", "dl")
end

if is_plat("windows") then
add_cxflags("-Ox", "-fp:fast", "-EHsc")
end

after_build(function ()
os.rm("build")
end)
25 changes: 25 additions & 0 deletions xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- project
set_project("co")

-- set xmake minimum version
set_xmakever("2.2.5")

-- set common flags
set_languages("c++11")
set_optimize("faster") -- -O2
set_warnings("all") -- -Wall

if is_plat("macosx", "linux") then
add_cxflags("-g3")
if is_plat("macosx") then
add_cxflags("-fno-pie")
end
add_syslinks("pthread", "dl")
end

if is_plat("windows") then
add_cxflags("-Ox", "-fp:fast", "-EHsc")
end

-- include sub-projects
includes("base", "rpcgen", "test", "unitest/base")

0 comments on commit 88288bd

Please sign in to comment.