acl/lib_fiber/samples/wait_group/main.cpp

151 lines
3.2 KiB
C++
Raw Normal View History

2022-07-03 00:26:12 +08:00
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
static int __delay = 1;
//////////////////////////////////////////////////////////////////////////////
class fiber_notifier : public acl::fiber
{
public:
2022-08-01 21:57:07 +08:00
fiber_notifier(acl::wait_group& sync) : sync_(sync) {}
2022-07-03 00:26:12 +08:00
private:
~fiber_notifier(void) {}
private:
2022-08-01 21:57:07 +08:00
acl::wait_group& sync_;
2022-07-03 00:26:12 +08:00
// @override
void run(void) {
2022-08-01 22:13:42 +08:00
printf("Fiber-%u started, delay=%d seconds\r\n",
acl::fiber::self(), __delay);
2022-07-03 00:26:12 +08:00
sleep(__delay);
printf("Fiber-%u done\r\n", acl::fiber::self());
sync_.done();
delete this;
}
};
//////////////////////////////////////////////////////////////////////////////
class thread_notifier : public acl::thread
{
public:
2022-08-01 21:57:07 +08:00
thread_notifier(acl::wait_group& sync) : sync_(sync) {}
2022-07-03 00:26:12 +08:00
private:
~thread_notifier(void) {}
private:
2022-08-01 21:57:07 +08:00
acl::wait_group& sync_;
2022-07-03 00:26:12 +08:00
// @override
void* run(void) {
2022-08-01 22:13:42 +08:00
printf("Thread-%lu started, delay=%d seconds\r\n",
acl::thread::self(), __delay);
2022-07-03 00:26:12 +08:00
sleep(__delay);
printf("Thread-%lu done\r\n", acl::thread::self());
sync_.done();
delete this;
return NULL;
}
};
//////////////////////////////////////////////////////////////////////////////
class fiber_waiter : public acl::fiber
{
public:
2022-08-01 21:57:07 +08:00
fiber_waiter(acl::wait_group& sync) : sync_(sync) {}
2022-07-03 00:26:12 +08:00
~fiber_waiter(void) {}
private:
2022-08-01 21:57:07 +08:00
acl::wait_group& sync_;
2022-07-03 00:26:12 +08:00
// @override
void run(void) {
size_t ret = sync_.wait();
printf("All threads and fibers were done, ret=%zd\r\n", ret);
}
};
//////////////////////////////////////////////////////////////////////////////
static void usage(const char* procname)
{
printf("usage: %s -h [help]\r\n"
" -e event_type[kernel|poll|select|io_uring]\r\n"
2022-07-03 00:26:12 +08:00
" -t threads_count[default: 1]\r\n"
" -c fibers_count[default: 1]\r\n"
2022-08-01 22:13:42 +08:00
" -d delay[default: 1 second]\r\n"
2022-07-03 00:26:12 +08:00
, procname);
}
int main(int argc, char *argv[])
{
int ch, threads_count = 1, fibers_count = 1;
acl::fiber_event_t event_type = acl::FIBER_EVENT_T_KERNEL;
2022-07-03 00:26:12 +08:00
acl::acl_cpp_init();
acl::log::stdout_open(true);
while ((ch = getopt(argc, argv, "he:t:f:c:d:")) > 0) {
2022-07-03 00:26:12 +08:00
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 'e':
if (strcasecmp(optarg, "kernel") == 0) {
event_type = acl::FIBER_EVENT_T_KERNEL;
} else if (strcasecmp(optarg, "select") == 0) {
event_type = acl::FIBER_EVENT_T_SELECT;
} else if (strcasecmp(optarg, "poll") == 0) {
event_type = acl::FIBER_EVENT_T_POLL;
} else if (strcasecmp(optarg, "io_uring") == 0) {
event_type = acl::FIBER_EVENT_T_IO_URING;
}
break;
2022-07-03 00:26:12 +08:00
case 't':
threads_count = atoi(optarg);
break;
case 'c':
fibers_count = atoi(optarg);
break;
case 'd':
__delay = atoi(optarg);
break;
default:
break;
}
}
acl::log::stdout_open(true);
acl::fiber::stdout_open(true);
2022-08-01 21:57:07 +08:00
acl::wait_group sync;
2022-07-03 00:26:12 +08:00
sync.add(threads_count + fibers_count);
for (int i = 0; i < threads_count; i++) {
acl::thread* thr = new thread_notifier(sync);
thr->set_detachable(true);
thr->start();
}
for (int i = 0; i < fibers_count; i++) {
acl::fiber* fb = new fiber_notifier(sync);
fb->start();
}
fiber_waiter waiter(sync);
waiter.start();
acl::fiber::schedule_with(event_type);
2022-07-03 00:26:12 +08:00
return 0;
}