1
0
mirror of https://github.com/tearshark/librf.git synced 2024-10-01 15:57:07 +08:00
librf/tutorial/test_async_memory_layout.cpp
2020-03-26 17:26:39 +08:00

139 lines
5.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include "librf.h"
using namespace resumef;
template<class _Ctype>
static void callback_get_long(int64_t a, int64_t b, _Ctype&& cb)
{
std::cout << std::endl << __FUNCTION__ << " - begin" << std::endl;
//编译失败。因为这个函数不是"可恢复函数(resumeable function)",甚至都不是"可等待函数(awaitable function)"
//void* frame_ptr = _coro_frame_ptr();
using namespace std::chrono;
std::thread([=, cb = std::forward<_Ctype>(cb)]
{
std::this_thread::sleep_for(500ms);
cb(a + b);
}).detach();
std::cout << __FUNCTION__ << " - end" << std::endl;
}
//这种情况下,没有生成 frame-context因此并没有promise_type被内嵌在frame-context里
future_t<int64_t> awaitable_get_long(int64_t a, int64_t b)
{
std::cout << std::endl << __FUNCTION__ << " - begin" << std::endl;
//编译失败。因为这个函数不是"可恢复函数(resumeable function)",仅仅是"可等待函数(awaitable function)"
//void* frame_ptr = _coro_frame_ptr();
resumef::awaitable_t<int64_t> awaitable;
callback_get_long(a, b, [awaitable](int64_t val)
{
awaitable.set_value(val);
});
std::cout << __FUNCTION__ << " - end" << std::endl;
return awaitable.get_future();
}
future_t<int64_t> resumeable_get_long(int64_t x, int64_t y)
{
std::cout << std::endl << __FUNCTION__ << " - begin" << std::endl;
using future_type = future_t<int64_t>;
using promise_type = typename future_type::promise_type;
using state_type = typename future_type::state_type;
void* frame_ptr = _coro_frame_ptr();
auto handler = coroutine_handle<promise_type>::from_address(frame_ptr);
promise_type* promise = &handler.promise();
state_type* state = handler.promise().get_state();
std::cout << " future size=" << sizeof(future_type) << " / " << _Align_size<future_type>() << std::endl;
std::cout << " promise size=" << sizeof(promise_type) << " / " << _Align_size<promise_type>() << std::endl;
std::cout << " state size=" << sizeof(state_type) << " / "<< _Align_size<state_type>() << std::endl;
std::cout << " frame size=" << _coro_frame_size() << ", alloc size=" << state->get_alloc_size() << std::endl;
std::cout << " frame ptr=" << frame_ptr << "," << (void*)&frame_ptr << std::endl;
std::cout << " frame end=" << (void*)((char*)(frame_ptr)+_coro_frame_size()) << std::endl;
std::cout << " promise ptr=" << promise << "," << (void*)&promise << std::endl;
std::cout << " handle ptr=" << handler.address() << "," << (void*)&handler << std::endl;
std::cout << " state ptr=" << state << "," << (void*)&state << std::endl;
std::cout << " parent ptr=" << state->get_parent() << std::endl;
std::cout << " x=" << x << ", &x=" << std::addressof(x) << std::endl;
std::cout << " y=" << y << ", &y=" << std::addressof(y) << std::endl;
int64_t val = co_await awaitable_get_long(x, y);
std::cout << " val=" << val << ", &val=" << std::addressof(val) << std::endl;
std::cout << __FUNCTION__ << " - end" << std::endl;
co_return val;
}
//这种情况下,会生成对应的 frame-context一个promise_type被内嵌在frame-context里
future_t<> resumable_get_long_2(int64_t a, int64_t b, int64_t c)
{
int64_t v1, v2, v3;
std::cout << std::endl << __FUNCTION__ << " - begin" << std::endl;
using future_type = future_t<>;
using promise_type = typename future_type::promise_type;
using state_type = typename future_type::state_type;
void* frame_ptr = _coro_frame_ptr();
auto handler = coroutine_handle<promise_type>::from_address(frame_ptr);
promise_type * promise = &handler.promise();
state_type * state = handler.promise().get_state();
std::cout << " future size=" << sizeof(future_type) << " / " << _Align_size<future_type>() << std::endl;
std::cout << " promise size=" << sizeof(promise_type) << " / " << _Align_size<promise_type>() << std::endl;
std::cout << " state size=" << sizeof(state_type) << " / "<< _Align_size<state_type>() << std::endl;
std::cout << " frame size=" << _coro_frame_size() << ", alloc size=" << state->get_alloc_size() << std::endl;
std::cout << " frame ptr=" << frame_ptr << ","<< (void*)&frame_ptr << std::endl;
std::cout << " frame end=" << (void *)((char*)(frame_ptr) + _coro_frame_size()) << std::endl;
std::cout << " promise ptr=" << promise << "," << (void *)&promise << std::endl;
std::cout << " handle ptr=" << handler.address() << "," << (void*)&handler << std::endl;
std::cout << " state ptr=" << state << "," << (void*)&state << std::endl;
std::cout << " parent ptr=" << state->get_parent() << std::endl;
std::cout << " a=" << a << ", &a=" << std::addressof(a) << std::endl;
std::cout << " b=" << b << ", &b=" << std::addressof(b) << std::endl;
std::cout << " c=" << c << ", &c=" << std::addressof(c) << std::endl;
v1 = co_await resumeable_get_long(a, b);
std::cout << " v1=" << v1 << ", &v1=" << std::addressof(v1) << std::endl;
v2 = co_await resumeable_get_long(b, c);
std::cout << " v2=" << v2 << ", &v2=" << std::addressof(v2) << std::endl;
v3 = co_await resumeable_get_long(v1, v2);
std::cout << " v3=" << v3 << ", &v3=" << std::addressof(v3) << std::endl;
int64_t v4 = v1 * v2 * v3;
std::cout << " v4=" << v4 << ", &v4=" << std::addressof(v4) << std::endl;
std::cout << __FUNCTION__ << " - end" << std::endl;
}
void resumable_main_layout()
{
std::cout << std::endl << __FUNCTION__ << " - begin" << std::endl;
go resumable_get_long_2(1, 2, 5);
resumef::this_scheduler()->run_until_notask();
std::cout << __FUNCTION__ << " - end" << std::endl;
}