Skip to content

Commit

Permalink
Merge pull request Tencent#33 from xiaozhuai/master
Browse files Browse the repository at this point in the history
add cmake build support and something in example_echosvr
  • Loading branch information
leiffyli authored Jun 14, 2017
2 parents eb75d36 + df6f682 commit 425878b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
55 changes: 55 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
cmake_minimum_required(VERSION 2.8)
project(libco)

# This for mac osx only
set(CMAKE_MACOSX_RPATH 0)

# Set lib version
set(LIBCO_VERSION 0.5)

# Set cflags
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} -g -fno-strict-aliasing -O2 -Wall -export-dynamic -Wall -pipe -D_GNU_SOURCE -D_REENTRANT -fPIC -Wno-deprecated -m64)

# Use c and asm
enable_language(C ASM)

# Add source files
set(SOURCE_FILES
co_epoll.cpp
co_hook_sys_call.cpp
co_routine.cpp
coctx.cpp
coctx_swap.S)

# Add static and shared library target
add_library(colib_static STATIC ${SOURCE_FILES})
add_library(colib_shared SHARED ${SOURCE_FILES})

# Set library output name
set_target_properties(colib_static PROPERTIES OUTPUT_NAME colib)
set_target_properties(colib_shared PROPERTIES OUTPUT_NAME colib)

set_target_properties(colib_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(colib_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1)

# Set shared library version, will generate libcolib.${LIBCO_VERSION}.so and a symbol link named libcolib.so
# For mac osx, the extension name will be .dylib
set_target_properties(colib_shared PROPERTIES VERSION ${LIBCO_VERSION} SOVERSION ${LIBCO_VERSION})



# Macro for add example target
macro(add_example_target EXAMPLE_TARGET)
add_executable("example_${EXAMPLE_TARGET}" "example_${EXAMPLE_TARGET}.cpp")
target_link_libraries("example_${EXAMPLE_TARGET}" colib_static)
endmacro(add_example_target)

add_example_target(closure)
add_example_target(cond)
add_example_target(copystack)
add_example_target(echocli)
add_example_target(echosvr)
add_example_target(poll)
add_example_target(setenv)
add_example_target(specific)
add_example_target(thread)
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ libco通过仅有的几个函数接口 co_create/co_resume/co_yield 再配合 co
* __thread的协程私有变量、协程间通信的协程信号量co_signal (New);
* 语言级别的lambda实现,结合协程原地编写并执行后台异步任务 (New);
* 基于epoll/kqueue实现的小而轻的网络框架,基于时间轮盘实现的高性能定时器;

### Build

```bash
$ cd /path/to/libco
$ make
```

or use cmake

```bash
$ cd /path/to/libco
$ mkdir build
$ cd build
$ cmake ..
$ make
```
12 changes: 12 additions & 0 deletions example_echosvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,24 @@ static int CreateTcpSocket(const unsigned short shPort /* = 0 */,const char *psz

int main(int argc,char *argv[])
{
if(argc<5){
printf("Usage:\n"
"example_echosvr [IP] [PORT] [TASK_COUNT] [PROCESS_COUNT]\n"
"example_echosvr [IP] [PORT] [TASK_COUNT] [PROCESS_COUNT] -d # daemonize mode\n");
return -1;
}
const char *ip = argv[1];
int port = atoi( argv[2] );
int cnt = atoi( argv[3] );
int proccnt = atoi( argv[4] );
bool deamonize = argc >= 6 && strcmp(argv[5], "-d") == 0;

g_listen_fd = CreateTcpSocket( port,ip,true );
listen( g_listen_fd,1024 );
if(g_listen_fd==-1){
printf("Port %d is in use\n", port);
return -1;
}
printf("listen %d %s:%d\n",g_listen_fd,ip,port);

SetNonBlock( g_listen_fd );
Expand Down Expand Up @@ -233,6 +244,7 @@ int main(int argc,char *argv[])

exit(0);
}
if(!deamonize) wait(NULL);
return 0;
}

0 comments on commit 425878b

Please sign in to comment.