2017-09-24 14:01:30 +08:00
|
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <conio.h>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
#include "librf.h"
|
|
|
|
|
|
2020-02-18 11:32:20 +08:00
|
|
|
|
using namespace resumef;
|
|
|
|
|
|
2020-02-15 13:32:14 +08:00
|
|
|
|
template<class _Ctype>
|
|
|
|
|
void callback_get_long(int64_t val, _Ctype&& cb)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
using namespace std::chrono;
|
2020-02-15 13:32:14 +08:00
|
|
|
|
std::thread([val, cb = std::forward<_Ctype>(cb)]
|
|
|
|
|
{
|
2020-02-16 01:18:25 +08:00
|
|
|
|
std::this_thread::sleep_for(500ms);
|
2020-02-15 13:32:14 +08:00
|
|
|
|
cb(val * val);
|
|
|
|
|
}).detach();
|
|
|
|
|
}
|
2017-09-24 14:01:30 +08:00
|
|
|
|
|
2020-02-15 13:32:14 +08:00
|
|
|
|
//这种情况下,没有生成 frame-context,因此,并没有promise_type被内嵌在frame-context里
|
2020-02-18 11:32:20 +08:00
|
|
|
|
future_t<int64_t> async_get_long(int64_t val)
|
2020-02-15 13:32:14 +08:00
|
|
|
|
{
|
2020-02-18 11:32:20 +08:00
|
|
|
|
/*
|
|
|
|
|
void* frame_ptr = _coro_frame_ptr();
|
|
|
|
|
size_t frame_size = _coro_frame_size();
|
|
|
|
|
std::cout << "test_routine_use_timer" << std::endl;
|
2020-02-18 11:42:42 +08:00
|
|
|
|
std::cout << "frame point=" << frame_ptr << ", size=" << frame_size << ", promise_size=" << _Align_size<promise_t<>>() << std::endl;
|
2020-02-18 11:32:20 +08:00
|
|
|
|
|
|
|
|
|
auto handler = coroutine_handle<promise_t<>>::from_address(frame_ptr);
|
|
|
|
|
auto st = handler.promise()._state;
|
|
|
|
|
scheduler_t* sch = st->get_scheduler();
|
|
|
|
|
auto parent = st->get_parent();
|
|
|
|
|
std::cout << "st=" << st.get() << ", scheduler=" << sch << ", parent=" << parent << std::endl;
|
|
|
|
|
*/
|
|
|
|
|
resumef::awaitable_t<int64_t> awaitable;
|
|
|
|
|
callback_get_long(val, [awaitable](int64_t val)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
2020-02-18 11:32:20 +08:00
|
|
|
|
awaitable.set_value(val);
|
2020-02-15 13:32:14 +08:00
|
|
|
|
});
|
2020-02-18 11:32:20 +08:00
|
|
|
|
return awaitable.get_future();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 11:32:20 +08:00
|
|
|
|
future_t<> wait_get_long(int64_t val)
|
2020-02-15 17:53:11 +08:00
|
|
|
|
{
|
|
|
|
|
co_await async_get_long(val);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 13:32:14 +08:00
|
|
|
|
//这种情况下,会生成对应的 frame-context,一个promise_type被内嵌在frame-context里
|
2020-02-18 11:32:20 +08:00
|
|
|
|
future_t<> resumable_get_long(int64_t val)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << val << std::endl;
|
|
|
|
|
val = co_await async_get_long(val);
|
|
|
|
|
std::cout << val << std::endl;
|
|
|
|
|
val = co_await async_get_long(val);
|
|
|
|
|
std::cout << val << std::endl;
|
|
|
|
|
val = co_await async_get_long(val);
|
|
|
|
|
std::cout << val << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 11:32:20 +08:00
|
|
|
|
future_t<int64_t> loop_get_long(int64_t val)
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
std::cout << val << std::endl;
|
|
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
|
{
|
|
|
|
|
val = co_await async_get_long(val);
|
|
|
|
|
std::cout << val << std::endl;
|
|
|
|
|
}
|
2020-02-15 17:53:11 +08:00
|
|
|
|
co_return val;
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resumable_main_cb()
|
|
|
|
|
{
|
|
|
|
|
std::cout << std::this_thread::get_id() << std::endl;
|
|
|
|
|
|
2020-02-15 17:53:11 +08:00
|
|
|
|
GO
|
2017-09-24 14:01:30 +08:00
|
|
|
|
{
|
|
|
|
|
auto val = co_await loop_get_long(2);
|
2020-02-16 00:47:45 +08:00
|
|
|
|
std::cout << "GO:" << val << std::endl;
|
2017-09-24 14:01:30 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-02-16 00:47:45 +08:00
|
|
|
|
go resumable_get_long(3);
|
2017-10-01 10:33:08 +08:00
|
|
|
|
resumef::this_scheduler()->run_until_notask();
|
2017-09-24 14:01:30 +08:00
|
|
|
|
}
|