21 #if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO 23 #define CALC_CLOCK_T std::chrono::system_clock::time_point 24 #define CALC_CLOCK_NOW() std::chrono::system_clock::now() 25 #define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count()) 26 #define CALC_NS_AVG_CLOCK(x, y) static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1)) 28 #define CALC_CLOCK_T clock_t 29 #define CALC_CLOCK_NOW() clock() 30 #define CALC_MS_CLOCK(x) static_cast<int>((x) / (CLOCKS_PER_SEC / 1000)) 31 #define CALC_NS_AVG_CLOCK(x, y) (1000000LL * static_cast<long long>((x) / (CLOCKS_PER_SEC / 1000)) / (y ? y : 1)) 42 my_cotoutine_t *addr = copp::this_coroutine::get<my_cotoutine_t>();
54 int main(
int argc,
char *argv[]) {
55 puts(
"###################### context coroutine (stack using malloc/free) ###################");
56 printf(
"########## Cmd:");
57 for (
int i = 0; i < argc; ++i) {
58 printf(
" %s", argv[i]);
70 size_t stack_size = 16 * 1024;
72 stack_size = atoi(argv[3]) * 1024;
75 time_t begin_time = time(NULL);
81 time_t end_time = time(NULL);
83 printf(
"allocate %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
MAX_COROUTINE_NUMBER,
84 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
93 end_time = time(NULL);
95 printf(
"create %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n", MAX_COROUTINE_NUMBER,
96 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
99 begin_time = end_time;
100 begin_clock = end_clock;
108 bool continue_flag =
true;
109 long long real_switch_times =
static_cast<long long>(0);
111 while (continue_flag) {
112 continue_flag =
false;
114 if (
false == co_arr[i]->is_finished()) {
115 continue_flag =
true;
122 end_time = time(NULL);
124 printf(
"switch %d coroutine contest %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n", MAX_COROUTINE_NUMBER,
125 real_switch_times, static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
128 begin_time = end_time;
129 begin_clock = end_clock;
133 end_time = time(NULL);
135 printf(
"remove %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n", MAX_COROUTINE_NUMBER,
136 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
static int my_runner(void *)
static ptr_t create(const callback_t &runner, allocator_type &alloc, size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
create and init coroutine with specify runner and specify stack size
#define CALC_NS_AVG_CLOCK(x, y)
copp::coroutine_context_container< copp::allocator::stack_allocator_malloc > my_cotoutine_t
my_cotoutine_t::ptr_t * co_arr
int main(int argc, char *argv[])
int yield(void **priv_data=UTIL_CONFIG_NULLPTR)
yield coroutine
coroutine container contain stack context, stack allocator and runtime fcontext