Skip to content

Commit

Permalink
Merge branch 'v2' of https://github.com/owt5008137/libcopp into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
owent committed May 14, 2019
2 parents eb3c54f + 6451579 commit e545651
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ EchoWithColor(COLOR GREEN "-- Build Type: ${CMAKE_BUILD_TYPE}")

########################################################################
# 导入项目配置
unset (PROJECT_LIBCOPP_SYSLIB_LINK_NAMES)
## 导入所有 macro 定义
include("${CMAKE_CURRENT_LIST_DIR}/include/include.macro.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/src/libcopp.macro.cmake")
Expand Down Expand Up @@ -59,6 +60,15 @@ if (LIBCOTASK_ENABLE)
set(LIBCOTASK_MACRO_ENABLED 1)
endif ()

find_package(Threads)
if (CMAKE_USE_PTHREADS_INIT)
list(APPEND PROJECT_LIBCOPP_SYSLIB_LINK_NAMES pthread)
set(THREAD_TLS_USE_PTHREAD 1)
if (THREADS_PREFER_PTHREAD_FLAG)
add_definitions(${THREADS_PREFER_PTHREAD_FLAG})
endif ()
endif ()

configure_file(
"${PROJECT_LIBCOPP_ROOT_INC_DIR}/libcopp/utils/config/build_feature.h.in"
"${PROJECT_LIBCOPP_ROOT_INC_DIR}/libcopp/utils/config/build_feature.h"
Expand Down
4 changes: 4 additions & 0 deletions include/libcopp/utils/config/build_feature.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@

#cmakedefine LIBCOTASK_MACRO_ENABLED @LIBCOTASK_MACRO_ENABLED@

#ifndef THREAD_TLS_USE_PTHREAD
#cmakedefine THREAD_TLS_USE_PTHREAD @THREAD_TLS_USE_PTHREAD@
#endif

#endif
9 changes: 1 addition & 8 deletions include/libcopp/utils/config/compiler_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
# define UTIL_CONFIG_COMPILER_IS_Cray 0
# define UTIL_CONFIG_COMPILER_IS_TI 0
# define UTIL_CONFIG_COMPILER_IS_Fujitsu 0
# define UTIL_CONFIG_COMPILER_IS_GHS 0
# define UTIL_CONFIG_COMPILER_IS_SCO 0
# define UTIL_CONFIG_COMPILER_IS_ARMCC 0
# define UTIL_CONFIG_COMPILER_IS_AppleClang 0
Expand Down Expand Up @@ -77,7 +76,7 @@
# undef UTIL_CONFIG_COMPILER_IS_zOS
# define UTIL_CONFIG_COMPILER_IS_zOS 1

#elif defined(__ibmxl__) || (defined(__IBMCPP__) && !defined(__COMPILER_VER__) && __IBMCPP__ >= 800)
#elif defined(__IBMCPP__) && !defined(__COMPILER_VER__) && __IBMCPP__ >= 800
# undef UTIL_CONFIG_COMPILER_IS_XL
# define UTIL_CONFIG_COMPILER_IS_XL 1

Expand All @@ -101,10 +100,6 @@
# undef UTIL_CONFIG_COMPILER_IS_Fujitsu
# define UTIL_CONFIG_COMPILER_IS_Fujitsu 1

#elif defined(__ghs__)
# undef UTIL_CONFIG_COMPILER_IS_GHS
# define UTIL_CONFIG_COMPILER_IS_GHS 1

#elif defined(__SCO_VERSION__)
# undef UTIL_CONFIG_COMPILER_IS_SCO
# define UTIL_CONFIG_COMPILER_IS_SCO 1
Expand Down Expand Up @@ -642,8 +637,6 @@

# if defined(UTIL_CONFIG_COMPILER_CXX_NULLPTR) && UTIL_CONFIG_COMPILER_CXX_NULLPTR
# define UTIL_CONFIG_NULLPTR nullptr
# elif UTIL_CONFIG_COMPILER_IS_GNU
# define UTIL_CONFIG_NULLPTR __null
# else
# define UTIL_CONFIG_NULLPTR 0
# endif
Expand Down
3 changes: 2 additions & 1 deletion sample/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ foreach(SAMPLE_SRC_FILE IN LISTS SAMPLE_SRC_LIST)
add_executable(${SAMPLE_SRC_BIN_NAME} ${SAMPLE_SRC_FILE})
target_link_libraries(${SAMPLE_SRC_BIN_NAME}
${PROJECT_LIBCOTASK_LIB_LINK}
${PROJECT_LIBCOPP_LIB_LINK}
${PROJECT_LIBCOPP_LIB_LINK}
${PROJECT_LIBCOPP_SYSLIB_LINK_NAMES}
${COMPILER_OPTION_EXTERN_CXX_LIBS}
)

Expand Down
30 changes: 13 additions & 17 deletions src/libcopp/coroutine/coroutine_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
#include <cstdlib>
#include <cstring>

#include <libcopp/utils/config/build_feature.h>
#include <libcopp/utils/errno.h>
#include <libcopp/utils/std/explicit_declare.h>

#include <libcopp/coroutine/coroutine_context.h>

#ifndef UTIL_CONFIG_THREAD_LOCAL

#if !(defined(THREAD_TLS_USE_PTHREAD) && THREAD_TLS_USE_PTHREAD) && defined(UTIL_CONFIG_THREAD_LOCAL)
// using thread_local
#else
#include <pthread.h>

#endif

#ifdef LIBCOPP_MACRO_USE_SEGMENTED_STACKS
Expand All @@ -29,34 +30,29 @@ void __splitstack_block_signals_context(void * [COPP_MACRO_SEGMENTED_STACK_NUMBE
namespace copp {
namespace detail {

#ifndef UTIL_CONFIG_THREAD_LOCAL

#if !(defined(THREAD_TLS_USE_PTHREAD) && THREAD_TLS_USE_PTHREAD) && defined(UTIL_CONFIG_THREAD_LOCAL)
static UTIL_CONFIG_THREAD_LOCAL coroutine_context *gt_current_coroutine = UTIL_CONFIG_NULLPTR;
#else
static pthread_once_t gt_coroutine_init_once = PTHREAD_ONCE_INIT;
static pthread_key_t gt_coroutine_tls_key;
static void init_pthread_this_coroutine_context() { (void)pthread_key_create(&gt_coroutine_tls_key, UTIL_CONFIG_NULLPTR); }

#else

static UTIL_CONFIG_THREAD_LOCAL coroutine_context *gt_current_coroutine = UTIL_CONFIG_NULLPTR;

#endif

static void set_this_coroutine_context(coroutine_context *p) {
#ifndef UTIL_CONFIG_THREAD_LOCAL
#if !(defined(THREAD_TLS_USE_PTHREAD) && THREAD_TLS_USE_PTHREAD) && defined(UTIL_CONFIG_THREAD_LOCAL)
gt_current_coroutine = p;
#else
(void)pthread_once(&gt_coroutine_init_once, init_pthread_this_coroutine_context);
pthread_setspecific(gt_coroutine_tls_key, p);
#else
gt_current_coroutine = p;
#endif
}

static coroutine_context *get_this_coroutine_context() {
#ifndef UTIL_CONFIG_THREAD_LOCAL
#if !(defined(THREAD_TLS_USE_PTHREAD) && THREAD_TLS_USE_PTHREAD) && defined(UTIL_CONFIG_THREAD_LOCAL)
return gt_current_coroutine;
#else
(void)pthread_once(&gt_coroutine_init_once, init_pthread_this_coroutine_context);
return reinterpret_cast<coroutine_context *>(pthread_getspecific(gt_coroutine_tls_key));
#else

return gt_current_coroutine;
#endif
}
} // namespace detail
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ target_link_libraries(libcopp_unit_test
${PROJECT_LIBCOTASK_LIB_LINK}
${PROJECT_LIBCOPP_LIB_LINK}
${PROJECT_LIBCOPP_TEST_LIB_LINK}
${PROJECT_LIBCOPP_SYSLIB_LINK_NAMES}
${COMPILER_OPTION_EXTERN_CXX_LIBS}
)

Expand Down

0 comments on commit e545651

Please sign in to comment.