2020-03-26 17:35:12 +08:00
|
|
|
|
#include <chrono>
|
2017-09-24 14:01:30 +08:00
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
|
#include "librf/librf.h"
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2021-11-01 17:59:08 +08:00
|
|
|
|
using namespace librf;
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2019-10-09 09:43:10 +08:00
|
|
|
|
//非协程的逻辑线程,或异步代码,可以通过event_t通知到协程,并且不会阻塞协程所在的线程。
|
2020-03-10 21:31:05 +08:00
|
|
|
|
static std::thread async_set_event(const event_t & e, std::chrono::milliseconds dt)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
return std::thread([=]
|
|
|
|
|
{
|
|
|
|
|
std::this_thread::sleep_for(dt);
|
|
|
|
|
e.signal();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-03-10 21:31:05 +08:00
|
|
|
|
static future_t<> resumable_wait_event(const event_t & e)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
2020-04-18 11:46:29 +08:00
|
|
|
|
auto result = co_await e.wait();
|
|
|
|
|
if (result == false)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
std::cout << "time out!" << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "event signal!" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 21:31:05 +08:00
|
|
|
|
static void test_wait_one()
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
event_t evt;
|
|
|
|
|
go resumable_wait_event(evt);
|
|
|
|
|
auto tt = async_set_event(evt, 1000ms);
|
2017-10-01 10:33:08 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
tt.join();
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
event_t evt2(1);
|
2020-02-15 15:20:16 +08:00
|
|
|
|
go[&]() -> future_t<>
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
(void)co_await evt2.wait();
|
|
|
|
|
std::cout << "event signal on 1!" << std::endl;
|
|
|
|
|
};
|
2020-02-15 15:20:16 +08:00
|
|
|
|
go[&]() -> future_t<>
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
(void)co_await evt2.wait();
|
|
|
|
|
std::cout << "event signal on 2!" << std::endl;
|
|
|
|
|
};
|
|
|
|
|
std::cout << std::this_thread::get_id() << std::endl;
|
|
|
|
|
auto tt = async_set_event(evt2, 1000ms);
|
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
tt.join();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 23:10:38 +08:00
|
|
|
|
static void test_wait_three()
|
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
event_t evt1, evt2, evt3;
|
|
|
|
|
|
|
|
|
|
go[&]() -> future_t<>
|
|
|
|
|
{
|
2020-04-18 11:46:29 +08:00
|
|
|
|
auto result = co_await event_t::wait_all(std::initializer_list<event_t>{ evt1, evt2, evt3 });
|
|
|
|
|
if (result)
|
2020-03-29 23:10:38 +08:00
|
|
|
|
std::cout << "all event signal!" << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "time out!" << std::endl;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<std::thread> vtt;
|
|
|
|
|
|
|
|
|
|
srand((int)time(nullptr));
|
|
|
|
|
vtt.emplace_back(async_set_event(evt1, 1ms * (500 + rand() % 1000)));
|
|
|
|
|
vtt.emplace_back(async_set_event(evt2, 1ms * (500 + rand() % 1000)));
|
|
|
|
|
vtt.emplace_back(async_set_event(evt3, 1ms * (500 + rand() % 1000)));
|
|
|
|
|
|
|
|
|
|
this_scheduler()->run_until_notask();
|
|
|
|
|
|
|
|
|
|
for (auto& tt : vtt)
|
|
|
|
|
tt.join();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 21:31:05 +08:00
|
|
|
|
static void test_wait_any()
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
event_t evts[8];
|
|
|
|
|
|
2020-02-15 15:20:16 +08:00
|
|
|
|
go[&]() -> future_t<>
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
2020-03-31 10:55:43 +08:00
|
|
|
|
for (size_t i = 0; i < std::size(evts); ++i)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
intptr_t idx = co_await event_t::wait_any(evts);
|
|
|
|
|
std::cout << "event " << idx << " signal!" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<std::thread> vtt;
|
|
|
|
|
|
|
|
|
|
srand((int)time(nullptr));
|
|
|
|
|
for (auto & e : evts)
|
|
|
|
|
{
|
|
|
|
|
vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
for (auto & tt : vtt)
|
|
|
|
|
tt.join();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 21:31:05 +08:00
|
|
|
|
static void test_wait_all()
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
event_t evts[8];
|
|
|
|
|
|
2020-02-15 15:20:16 +08:00
|
|
|
|
go[&]() -> future_t<>
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
2020-04-18 11:46:29 +08:00
|
|
|
|
auto result = co_await event_t::wait_all(evts);
|
|
|
|
|
if (result)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
std::cout << "all event signal!" << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "time out!" << std::endl;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<std::thread> vtt;
|
|
|
|
|
|
|
|
|
|
srand((int)time(nullptr));
|
|
|
|
|
for (auto & e : evts)
|
|
|
|
|
{
|
|
|
|
|
vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
for (auto & tt : vtt)
|
|
|
|
|
tt.join();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 21:31:05 +08:00
|
|
|
|
static void test_wait_all_timeout()
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
event_t evts[8];
|
|
|
|
|
|
2020-02-15 15:20:16 +08:00
|
|
|
|
go[&]() -> future_t<>
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
2020-04-18 11:46:29 +08:00
|
|
|
|
auto result = co_await event_t::wait_all_for(1000ms, evts);
|
|
|
|
|
if (result)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
std::cout << "all event signal!" << std::endl;
|
|
|
|
|
else
|
|
|
|
|
std::cout << "time out!" << std::endl;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<std::thread> vtt;
|
|
|
|
|
|
|
|
|
|
srand((int)time(nullptr));
|
|
|
|
|
for (auto & e : evts)
|
|
|
|
|
{
|
|
|
|
|
vtt.emplace_back(async_set_event(e, 1ms * (500 + rand() % 1000)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 10:33:08 +08:00
|
|
|
|
this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
for (auto & tt : vtt)
|
|
|
|
|
tt.join();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resumable_main_event()
|
|
|
|
|
{
|
2020-03-17 18:26:22 +08:00
|
|
|
|
test_wait_one();
|
|
|
|
|
std::cout << std::endl;
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2020-03-29 23:10:38 +08:00
|
|
|
|
test_wait_three();
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
2017-09-24 14:01:30 +08:00
|
|
|
|
test_wait_any();
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
|
|
test_wait_all();
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
|
|
test_wait_all_timeout();
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
}
|
2020-09-23 22:56:51 +08:00
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
resumable_main_event();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|