21 #ifdef LIBCOTASK_MACRO_ENABLED
23 # if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
25 # define CALC_CLOCK_T std::chrono::system_clock::time_point
26 # define CALC_CLOCK_NOW() std::chrono::system_clock::now()
27 # define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count())
28 # define CALC_NS_AVG_CLOCK(x, y) \
29 static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
31 # define CALC_CLOCK_T clock_t
32 # define CALC_CLOCK_NOW() clock()
33 # define CALC_MS_CLOCK(x) static_cast<int>((x) / (CLOCKS_PER_SEC / 1000))
34 # define CALC_NS_AVG_CLOCK(x, y) (1000000LL * static_cast<long long>((x) / (CLOCKS_PER_SEC / 1000)) / (y ? y : 1))
40 int max_task_number = 100000;
41 std::vector<my_task_t::ptr_t> task_arr;
44 static int my_task_action(
void *) {
55 int main(
int argc,
char *argv[]) {
56 # ifdef LIBCOPP_MACRO_SYS_POSIX
57 puts(
"###################### context coroutine (stack using default allocator[mmap]) ###################");
58 # elif defined(LIBCOPP_MACRO_SYS_WIN)
59 puts(
"###################### context coroutine (stack using default allocator[VirtualAlloc]) ###################");
61 puts(
"###################### context coroutine (stack using default allocator ###################");
63 printf(
"########## Cmd:");
64 for (
int i = 0; i < argc; ++i) {
65 printf(
" %s", argv[i]);
70 max_task_number = atoi(argv[1]);
77 size_t stack_size = 16 * 1024;
79 stack_size = atoi(argv[3]) * 1024;
82 time_t begin_time = time(
nullptr);
86 task_arr.reserve(
static_cast<size_t>(max_task_number));
87 while (task_arr.size() <
static_cast<size_t>(max_task_number)) {
88 my_task_t::ptr_t new_task = my_task_t::create(my_task_action, stack_size);
90 fprintf(stderr,
"create coroutine task failed, real size is %d.\n",
static_cast<int>(task_arr.size()));
91 fprintf(stderr,
"maybe sysconf [vm.max_map_count] extended.\n");
92 max_task_number =
static_cast<int>(task_arr.size());
95 task_arr.push_back(new_task);
98 time_t end_time = time(
nullptr);
100 printf(
"create %d task, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
101 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
104 begin_time = end_time;
105 begin_clock = end_clock;
108 for (
int i = 0; i < max_task_number; ++i) {
109 task_arr[i]->start();
113 bool continue_flag =
true;
114 long long real_switch_times =
static_cast<long long>(0);
116 while (continue_flag) {
117 continue_flag =
false;
118 for (
int i = 0; i < max_task_number; ++i) {
119 if (
false == task_arr[i]->is_completed()) {
120 continue_flag =
true;
122 task_arr[i]->resume();
127 end_time = time(
nullptr);
129 printf(
"switch %d tasks %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
130 real_switch_times,
static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
133 begin_time = end_time;
134 begin_clock = end_clock;
138 end_time = time(
nullptr);
140 printf(
"remove %d tasks, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
141 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
148 puts(
"cotask disabled");
LIBCOPP_COTASK_API impl::task_impl * get_task() LIBCOPP_MACRO_NOEXCEPT
get current running task
#define CALC_NS_AVG_CLOCK(x, y)