Skip to content

Commit

Permalink
add more expect, add unit test of "start a coroutine in another corou…
Browse files Browse the repository at this point in the history
…tine"
  • Loading branch information
owent committed Jun 16, 2016
1 parent f5f560d commit 58eac6c
Show file tree
Hide file tree
Showing 35 changed files with 511 additions and 622 deletions.
61 changes: 41 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,43 @@ language: cpp
env:
global:
- LANG="zh_CN.UTF-8"
- CMAKE_PREFIX=$HOME/prebuilt/cmake ;
- CMAKE_VERSION=3.4.3 ;
- CMAKE_PREFIX=/home/travis/prebuilt/cmake ;
- CMAKE_VERSION=3.5.2 ;
matrix:
- TRAVIS_OS_NAME=osx CC=clang CXX=clang++
- TRAVIS_OS_NAME=osx CC=gcc CXX=g++
- TRAVIS_OS_NAME=linux CC=gcc CXX=g++
- TRAVIS_OS_NAME=linux CC=gcc-4.9 CXX=g++-4.9
- TRAVIS_OS_NAME=linux CC=clang-3.7 CXX=clang++-3.7
# - TRAVIS_OS_NAME=osx USE_CC=clang USE_CXX=clang++
- TRAVIS_OS_NAME=osx USE_CC=gcc USE_CXX=g++
- TRAVIS_OS_NAME=linux USE_CC=gcc USE_CXX=g++
- TRAVIS_OS_NAME=linux USE_CC=gcc-4.4 USE_CXX=g++-4.4
- TRAVIS_OS_NAME=linux USE_CC=gcc-4.9 USE_CXX=g++-4.9
- TRAVIS_OS_NAME=linux USE_CC=gcc-6 USE_CXX=g++-6
# - TRAVIS_OS_NAME=linux USE_CC=clang-3.8 USE_CXX=clang++-3.8

before_install:
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y "deb http://llvm.org/apt/precise/ llvm-toolchain-precise-3.7 main"; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key | sudo apt-key add - ; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update -qq; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update; fi

install:
- if [ "$TRAVIS_OS_NAME" == "linux" ] && [ "$CC" == "gcc-4.9" ] ; then sudo apt-get install -qq gcc-4.9-multilib g++-4.9-multilib; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ] && [ "$CC" == "clang-3.7" ] ; then sudo apt-get install --allow-unauthenticated -qq clang-3.7; fi
addons:
apt:
sources:
- ubuntu-toolchain-r-test
# - llvm-toolchain-precise-3.8
packages:
- g++-4.4
- g++-4.4-multilib
- gcc-4.4
- gcc-4.4-multilib
- gcc-4.9
- gcc-4.9-multilib
- g++-4.9
- g++-4.9-multilib
- gcc-6
- gcc-6-multilib
- g++-6
- g++-6-multilib
- clang
## - libc++1
## - libc++-dev
## - libc++abi1
## - libc++abi-dev
#- clang-3.8
#- libclang-common-3.8-dev
#- libclang-3.8-dev

before_script:
- REPO_DIR=$PWD;
Expand All @@ -37,11 +55,14 @@ before_script:
rm -rf CMake-$CMAKE_VERSION CMake-$CMAKE_VERSION.tar.gz;
fi
- cd "$REPO_DIR";

script:

script:
- export CC=$USE_CC
- export CXX=$USE_CXX
- REPO_DIR=$PWD;
- mkdir -p $REPO_DIR/build && cd $REPO_DIR/build ;
- $CMAKE_PREFIX/bin/cmake .. -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX ;
- echo "$CMAKE_PREFIX/bin/cmake .. -DPROJECT_ENABLE_UNITTEST=ON -DPROJECT_ENABLE_SAMPLE=ON -DCMAKE_C_COMPILER=$USE_CC -DCMAKE_CXX_COMPILER=$USE_CXX" ;
- $CMAKE_PREFIX/bin/cmake .. -DPROJECT_ENABLE_UNITTEST=ON -DPROJECT_ENABLE_SAMPLE=ON -DCMAKE_C_COMPILER=$USE_CC -DCMAKE_CXX_COMPILER=$USE_CXX ;
- make -j4

after_success:
Expand Down
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ EchoWithColor(COLOR GREEN "-- Build Type: ${CMAKE_BUILD_TYPE}")
########################################################################
# 导入项目配置
## 导入所有 macro 定义
include_macro_recurse("${PROJECT_SOURCE_DIR}")
include_macro_recurse(${CMAKE_CURRENT_LIST_DIR})

## 导入所有工程项目
add_project_recurse("${PROJECT_SOURCE_DIR}")
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/src")

if(PROJECT_ENABLE_SAMPLE)
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/sample")
endif()

if(PROJECT_ENABLE_UNITTEST)
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/test")
endif()
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ INSTALL
Prerequisites
-------------

- **[required]** GCC or Clang or VC support ISO C++ 98 and upper
- **[required]** [cmake](www.cmake.org) 2.8.9 and upper
- **[required]** GCC or Clang or VC support ISO C++ 03 and upper
- **[required]** [cmake](www.cmake.org) 3.1.0 and upper
- **[optional]** [gtest](https://code.google.com/p/googletest/) 1.6.0 and upper (better test supported)
- **[optional]** [Boost.Test](http://www.boost.org/doc/libs/release/libs/test/) (Boost.Test supported)

Expand Down Expand Up @@ -66,7 +66,7 @@ Build
> > -DLIBCOPP\_ENABLE\_SEGMENTED\_STACKS=YES|NO [default=NO] enable split stack supported context.(it's only availabe in linux and gcc 4.7.0 or upper)
> > -DLIBCOPP\_ENABLE\_VALGRIND=YES|NO [default=NO] enable valgrind supported context.
> > -DLIBCOPP\_ENABLE\_VALGRIND=YES|NO [default=YES] enable valgrind supported context.
> > -DGTEST\_ROOT=[path] set gtest library install prefix path
Expand Down Expand Up @@ -198,13 +198,15 @@ HISTORY
========
2016-06-16
------
1. merge boost.context 1.61.0 and use the new jump progress(see https://owent.net/90QQw for detail)
2. enable valgrind support if valgrind/valgrind.h exists
3. use cmake to detect the function of compiler
4. using pthread key when c++11 TLS not available
5. remove coroutine_context_safe_base.coroutine_context_base is also thread safe now
6. remove all global variables of cotask
7. remove std/thread.h
1. [BOOST] merge boost.context 1.61.0 and use the new jump progress(see https://owent.net/90QQw for detail)
2. [BOOST] enable valgrind support if valgrind/valgrind.h exists
3. [CXX] use cmake to detect the function of compiler
4. [OPTIMIZE] using pthread key when c++11 TLS not available
5. [OPTIMIZE] remove coroutine_context_safe_base.coroutine_context_base is also thread safe now
6. [OPTIMIZE] remove all global variables of cotask
7. [OPTIMIZE] remove std/thread.h, add noexpect if available
8. [CI] CI use build matrix to test more compiler
9. [BUILD] use RelWithDebInfo for default

2016-02-27
------
Expand Down
2 changes: 1 addition & 1 deletion cmake_dev.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mkdir build
cd build

cmake .. -G "Visual Studio 14 2015 Win64" -DPROJECT_ENABLE_UNITTEST=ON -DPROJECT_ENABLE_SAMPLE=ON
cmake .. -G "Visual Studio 14 2015 Win64" -DPROJECT_ENABLE_UNITTEST=ON -DPROJECT_ENABLE_SAMPLE=ON -DCMAKE_BUILD_TYPE=Debug
2 changes: 1 addition & 1 deletion cmake_dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ cd "$(dirname $0)";
mkdir -p "$BUILD_DIR";
cd "$BUILD_DIR";

cmake .. -DPROJECT_ENABLE_UNITTEST=ON -DPROJECT_ENABLE_SAMPLE=ON "$@";
cmake .. -DPROJECT_ENABLE_UNITTEST=ON -DPROJECT_ENABLE_SAMPLE=ON -DLIBCOPP_ENABLE_SEGMENTED_STACKS=ON -DCMAKE_BUILD_TYPE=Debug "$@";
2 changes: 1 addition & 1 deletion include/include.macro.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ write_compiler_detection_header(
FILE "${PROJECT_ROOT_INC_DIR}/libcopp/utils/config/compiler_features.h"
PREFIX UTIL_CONFIG
COMPILERS GNU Clang AppleClang MSVC
FEATURES cxx_auto_type cxx_constexpr cxx_decltype cxx_decltype_auto cxx_defaulted_functions cxx_deleted_functions cxx_final cxx_override cxx_range_for cxx_noexcept cxx_nullptr cxx_static_assert cxx_thread_local cxx_variadic_templates cxx_lambdas
FEATURES cxx_auto_type cxx_constexpr cxx_decltype cxx_decltype_auto cxx_defaulted_functions cxx_deleted_functions cxx_final cxx_override cxx_range_for cxx_noexcept cxx_nullptr cxx_rvalue_references cxx_static_assert cxx_thread_local cxx_variadic_templates cxx_lambdas
)
22 changes: 11 additions & 11 deletions include/libcopp/coroutine/coroutine_context_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace copp {
#endif

public:
coroutine_context_base();
coroutine_context_base() UTIL_CONFIG_NOEXCEPT;
virtual ~coroutine_context_base();

public:
Expand All @@ -82,7 +82,7 @@ namespace copp {
* @param func fcontext callback
* @return COPP_EC_SUCCESS or error code
*/
virtual int create(coroutine_runnable_base* runner, void(*func)(::copp::fcontext::transfer_t) = &coroutine_context_base::coroutine_context_callback);
virtual int create(coroutine_runnable_base* runner, void(*func)(::copp::fcontext::transfer_t) = &coroutine_context_base::coroutine_context_callback) UTIL_CONFIG_NOEXCEPT;

/**
* @brief start coroutine
Expand Down Expand Up @@ -118,45 +118,45 @@ namespace copp {
* @param runner
* @return COPP_EC_SUCCESS or error code
*/
virtual int set_runner(coroutine_runnable_base* runner);
virtual int set_runner(coroutine_runnable_base* runner) UTIL_CONFIG_NOEXCEPT;

public:

/**
* get runner of this coroutine context
* @return NULL of pointer of runner
*/
inline coroutine_runnable_base* get_runner() { return runner_; }
inline coroutine_runnable_base* get_runner() UTIL_CONFIG_NOEXCEPT { return runner_; }

/**
* get runner of this coroutine context (const)
* @return NULL of pointer of runner
*/
inline const coroutine_runnable_base* get_runner() const { return runner_; }
inline const coroutine_runnable_base* get_runner() const UTIL_CONFIG_NOEXCEPT { return runner_; }

/**
* @brief get runner return code
* @return
*/
inline int get_ret_code() const { return runner_ret_code_; }
inline int get_ret_code() const UTIL_CONFIG_NOEXCEPT { return runner_ret_code_; }

/**
* @brief get runner return code
* @return true if coroutine has run and finished
*/
bool is_finished() const;
bool is_finished() const UTIL_CONFIG_NOEXCEPT;

/**
* @brief set private data(raw pointer)
* @note cotask set this private data to pointer of cotask, if you use cotask, do not use this function
*/
inline void set_private_data(void* ptr) { priv_data_ = ptr; }
inline void set_private_data(void* ptr) UTIL_CONFIG_NOEXCEPT { priv_data_ = ptr; }

/**
* @brief get private data(raw pointer)
* @note cotask set this private data to pointer of cotask, if you use cotask, do not use this function
*/
inline void* get_private_data() const { return priv_data_; }
inline void* get_private_data() const UTIL_CONFIG_NOEXCEPT { return priv_data_; }
protected:
/**
* @brief call platform jump to asm instruction
Expand All @@ -167,7 +167,7 @@ namespace copp {
*/
static void jump_to(fcontext::fcontext_t& to_fctx,
stack_context& from_sctx, stack_context& to_sctx,
jump_src_data_t& jump_transfer);
jump_src_data_t& jump_transfer) UTIL_CONFIG_NOEXCEPT;

/**
* @brief fcontext entrance function
Expand All @@ -183,7 +183,7 @@ namespace copp {
* @see detail::coroutine_context_base
* @return pointer of current coroutine, if not in coroutine, return NULL
*/
detail::coroutine_context_base* get_coroutine();
detail::coroutine_context_base* get_coroutine() UTIL_CONFIG_NOEXCEPT;

/**
* @brief get current coroutine and try to convert type
Expand Down
32 changes: 19 additions & 13 deletions include/libcopp/coroutine/coroutine_context_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace copp {

COROUTINE_CONTEXT_BASE_USING_BASE(base_type)
public:
coroutine_context_container() : base_type(), alloc_(){}
coroutine_context_container(const allocator_type& alloc) : alloc_(alloc){}
coroutine_context_container() UTIL_CONFIG_NOEXCEPT : base_type(), alloc_(){}
coroutine_context_container(const allocator_type& alloc) UTIL_CONFIG_NOEXCEPT : alloc_(alloc){}

~coroutine_context_container() {
_reset_stack();
Expand All @@ -43,15 +43,15 @@ namespace copp {
* @brief get stack allocator
* @return stack allocator
*/
inline const allocator_type& get_allocator() const {
inline const allocator_type& get_allocator() const UTIL_CONFIG_NOEXCEPT {
return alloc_;
}

/**
* @brief get stack allocator
* @return stack allocator
*/
inline allocator_type& get_allocator() {
inline allocator_type& get_allocator() UTIL_CONFIG_NOEXCEPT {
return alloc_;
}

Expand All @@ -60,9 +60,11 @@ namespace copp {
/**
* @brief deallocate stack context
*/
void _reset_stack() {
if (NULL == callee_stack_.sp || 0 == callee_stack_.size)
void _reset_stack() UTIL_CONFIG_NOEXCEPT {
if (NULL == callee_stack_.sp || 0 == callee_stack_.size) {
return;
}

alloc_.deallocate(callee_stack_);
callee_stack_.reset();
}
Expand All @@ -75,14 +77,16 @@ namespace copp {
* @param func fcontext callback(set NULL to use default callback)
* @return COPP_EC_SUCCESS or error code
*/
int create(coroutine_runnable_base* runner, std::size_t stack_size_, void(*func)(::copp::fcontext::transfer_t) = NULL){
if (NULL != callee_stack_.sp)
int create(coroutine_runnable_base* runner, std::size_t stack_size_, void(*func)(::copp::fcontext::transfer_t) = NULL) UTIL_CONFIG_NOEXCEPT {
if (NULL != callee_stack_.sp) {
return COPP_EC_ALREADY_INITED;
}

alloc_.allocate(callee_stack_, stack_size_);

if (NULL == callee_stack_.sp)
if (NULL == callee_stack_.sp) {
return COPP_EC_ALLOC_STACK_FAILED;
}

return base_type::create(runner, func);
}
Expand All @@ -93,20 +97,22 @@ namespace copp {
* @param func fcontext callback(set NULL to use default callback)
* @return COPP_EC_SUCCESS or error code
*/
int create(coroutine_runnable_base* runner, void(*func)(::copp::fcontext::transfer_t) = NULL){
if (NULL != callee_stack_.sp)
int create(coroutine_runnable_base* runner, void(*func)(::copp::fcontext::transfer_t) = NULL) UTIL_CONFIG_NOEXCEPT {
if (NULL != callee_stack_.sp) {
return COPP_EC_ALREADY_INITED;
}

alloc_.allocate(callee_stack_, stack_traits::default_size());

if (NULL == callee_stack_.sp)
if (NULL == callee_stack_.sp) {
return COPP_EC_ALLOC_STACK_FAILED;
}

return base_type::create(runner, func);
}

private:
coroutine_context_container(const coroutine_context_container&);
coroutine_context_container(const coroutine_context_container&) UTIL_CONFIG_DELETED_FUNCTION;

private:
allocator_type alloc_; /** stack allocator **/
Expand Down
18 changes: 8 additions & 10 deletions include/libcopp/stack/allocator/stack_allocator_malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@


#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#pragma once
#endif

#include <cstddef>

#include <libcopp/utils/features.h>

#ifdef COPP_HAS_ABI_HEADERS
# include COPP_ABI_PREFIX
#include COPP_ABI_PREFIX
#endif

namespace copp {
Expand All @@ -23,10 +23,9 @@ namespace copp {
* @brief memory allocator
* this allocator will maintain buffer using malloc/free function
*/
class stack_allocator_malloc
{
class stack_allocator_malloc {
public:
stack_allocator_malloc();
stack_allocator_malloc() UTIL_CONFIG_NOEXCEPT;
~stack_allocator_malloc();

/**
Expand All @@ -35,20 +34,19 @@ namespace copp {
* @param size stack size
* @note size must less or equal than attached
*/
void allocate(stack_context &ctx, std::size_t size);
void allocate(stack_context &ctx, std::size_t size) UTIL_CONFIG_NOEXCEPT;

/**
* deallocate memory from stack context [standard function]
* @param ctx stack context
*/
void deallocate(stack_context &ctx);
void deallocate(stack_context &ctx) UTIL_CONFIG_NOEXCEPT;
};

}
}
}

#ifdef COPP_HAS_ABI_HEADERS
# include COPP_ABI_SUFFIX
#include COPP_ABI_SUFFIX
#endif

#endif
Loading

0 comments on commit 58eac6c

Please sign in to comment.