Browse Source

完善when_any/when_al功能,并通过相关范例

tags/v2.9.7
tearshark 4 years ago
parent
commit
8521691d0d
5 changed files with 24 additions and 31 deletions
  1. 5
    2
      librf/src/promise.h
  2. 4
    4
      librf/src/promise.inl
  3. 8
    5
      librf/src/when.h
  4. 4
    2
      vs_proj/librf.cpp
  5. 3
    18
      vs_proj/librf.vcxproj

+ 5
- 2
librf/src/promise.h View File



namespace resumef namespace resumef
{ {
struct suspend_on_initial;
struct suspend_on_final;

template <typename _Ty> template <typename _Ty>
struct promise_impl_t struct promise_impl_t
{ {
promise_impl_t(const promise_impl_t&) = delete; promise_impl_t(const promise_impl_t&) = delete;
promise_impl_t & operator = (const promise_impl_t&) = delete; promise_impl_t & operator = (const promise_impl_t&) = delete;


auto initial_suspend() noexcept;
auto final_suspend() noexcept;
suspend_on_initial initial_suspend() noexcept;
suspend_on_final final_suspend() noexcept;
void set_exception(std::exception_ptr e); void set_exception(std::exception_ptr e);
future_type get_return_object(); future_type get_return_object();
void cancellation_requested(); void cancellation_requested();

+ 4
- 4
librf/src/promise.inl View File

}; };


template <typename _Ty> template <typename _Ty>
inline auto promise_impl_t<_Ty>::initial_suspend() noexcept
inline suspend_on_initial promise_impl_t<_Ty>::initial_suspend() noexcept
{ {
return suspend_on_initial{ _state.get() };
return { _state.get() };
} }


template <typename _Ty> template <typename _Ty>
inline auto promise_impl_t<_Ty>::final_suspend() noexcept
inline suspend_on_final promise_impl_t<_Ty>::final_suspend() noexcept
{ {
return suspend_on_final{ _state.get() };
return { _state.get() };
} }


template <typename _Ty> template <typename _Ty>

+ 8
- 5
librf/src/when.h View File

#pragma once #pragma once
#include "_awaker.h"
#if RESUMEF_USE_BOOST_ANY #if RESUMEF_USE_BOOST_ANY
#include <boost/any.hpp> #include <boost/any.hpp>
namespace resumef namespace resumef
} }
#endif #endif
#include "_awaker.h"
#include "promise.h"
#include "promise.inl"
//纠结过when_any的返回值,是选用index + std::any,还是选用std::variant<>。最终选择了std::any。 //纠结过when_any的返回值,是选用index + std::any,还是选用std::variant<>。最终选择了std::any。
//std::variant<>存在第一个元素不能默认构造的问题,需要使用std::monostate来占位,导致下标不是从0开始。 //std::variant<>存在第一个元素不能默认构造的问题,需要使用std::monostate来占位,导致下标不是从0开始。
//而且,std::variant<>里面存在类型重复的问题,好几个操作都是病态的 //而且,std::variant<>里面存在类型重复的问题,好几个操作都是病态的
template<class _Tup, class _Iter> template<class _Tup, class _Iter>
future_t<_Tup> when_all_range(size_t count, const std::shared_ptr<_Tup> & vals, scheduler_t& s, _Iter begin, _Iter end) future_t<_Tup> when_all_range(size_t count, const std::shared_ptr<_Tup> & vals, scheduler_t& s, _Iter begin, _Iter end)
{ {
promise_t<_Tup> awaitable;
awaitable_t<_Tup> awaitable;
when_impl_ptr _event = std::make_shared<when_impl>(count); when_impl_ptr _event = std::make_shared<when_impl>(count);
auto awaker = std::make_shared<when_awaker>( auto awaker = std::make_shared<when_awaker>(
template<class... _Fty> template<class... _Fty>
future_t<when_any_pair> when_any_count(size_t count, const when_any_result_ptr & val_ptr, scheduler_t & s, _Fty&&... f) future_t<when_any_pair> when_any_count(size_t count, const when_any_result_ptr & val_ptr, scheduler_t & s, _Fty&&... f)
{ {
promise_t<when_any_pair> awaitable;
awaitable_t<when_any_pair> awaitable;
when_impl_ptr _event = std::make_shared<when_impl>(count); when_impl_ptr _event = std::make_shared<when_impl>(count);
auto awaker = std::make_shared<when_awaker>( auto awaker = std::make_shared<when_awaker>(
template<class _Iter> template<class _Iter>
future_t<when_any_pair> when_any_range(size_t count, const when_any_result_ptr & val_ptr, scheduler_t & s, _Iter begin, _Iter end) future_t<when_any_pair> when_any_range(size_t count, const when_any_result_ptr & val_ptr, scheduler_t & s, _Iter begin, _Iter end)
{ {
promise_t<when_any_pair> awaitable;
awaitable_t<when_any_pair> awaitable;
when_impl_ptr _event = std::make_shared<when_impl>(count); when_impl_ptr _event = std::make_shared<when_impl>(count);
auto awaker = std::make_shared<when_awaker>( auto awaker = std::make_shared<when_awaker>(

+ 4
- 2
vs_proj/librf.cpp View File

extern void resumable_main_when_all(); extern void resumable_main_when_all();
extern void resumable_main_benchmark_mem(); extern void resumable_main_benchmark_mem();
extern void benchmark_main_channel_passing_next();
extern void resumable_main_benchmark_asio_server(); extern void resumable_main_benchmark_asio_server();
extern void resumable_main_benchmark_asio_client(intptr_t nNum); extern void resumable_main_benchmark_asio_client(intptr_t nNum);
//resumable_main_event_timeout(); //resumable_main_event_timeout();
//resumable_main_channel(); //resumable_main_channel();
//resumable_main_channel_mult_thread(); //测试失败! //resumable_main_channel_mult_thread(); //测试失败!
resumable_main_sleep();
//resumable_main_sleep();
//resumable_main_when_all();
benchmark_main_channel_passing_next();
/* /*
if (argc > 1) if (argc > 1)
else else
resumable_main_benchmark_asio_server(); resumable_main_benchmark_asio_server();
resumable_main_when_all();
*/ */
return 0; return 0;

+ 3
- 18
vs_proj/librf.vcxproj View File

<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\benchmark\benchmark_async_mem.cpp" /> <ClCompile Include="..\benchmark\benchmark_async_mem.cpp" />
<ClCompile Include="..\benchmark\benchmark_channel_passing_next.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\benchmark\benchmark_channel_passing_next.cpp" />
<ClCompile Include="..\librf\src\event.cpp" /> <ClCompile Include="..\librf\src\event.cpp" />
<ClCompile Include="..\librf\src\mutex.cpp" /> <ClCompile Include="..\librf\src\mutex.cpp" />
<ClCompile Include="..\librf\src\rf_task.cpp" /> <ClCompile Include="..\librf\src\rf_task.cpp" />
<ClCompile Include="..\librf\src\sleep.cpp" /> <ClCompile Include="..\librf\src\sleep.cpp" />
<ClCompile Include="..\librf\src\state.cpp" /> <ClCompile Include="..\librf\src\state.cpp" />
<ClCompile Include="..\librf\src\timer.cpp" /> <ClCompile Include="..\librf\src\timer.cpp" />
<ClCompile Include="..\librf\src\when.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\librf\src\when.cpp" />
<ClCompile Include="..\tutorial\test_async_cb.cpp" /> <ClCompile Include="..\tutorial\test_async_cb.cpp" />
<ClCompile Include="..\tutorial\test_async_channel.cpp" /> <ClCompile Include="..\tutorial\test_async_channel.cpp" />
<ClCompile Include="..\tutorial\test_async_channel_mult_thread.cpp" /> <ClCompile Include="..\tutorial\test_async_channel_mult_thread.cpp" />
<ClCompile Include="..\tutorial\test_async_sleep.cpp" /> <ClCompile Include="..\tutorial\test_async_sleep.cpp" />
<ClCompile Include="..\tutorial\test_async_suspend_always.cpp" /> <ClCompile Include="..\tutorial\test_async_suspend_always.cpp" />
<ClCompile Include="..\tutorial\test_async_timer.cpp" /> <ClCompile Include="..\tutorial\test_async_timer.cpp" />
<ClCompile Include="..\tutorial\test_async_when_all.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\tutorial\test_async_when_all.cpp" />
<ClCompile Include="..\tutorial\test_async_yield_return.cpp" /> <ClCompile Include="..\tutorial\test_async_yield_return.cpp" />
<ClCompile Include="librf.cpp"> <ClCompile Include="librf.cpp">
<BasicRuntimeChecks Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Default</BasicRuntimeChecks> <BasicRuntimeChecks Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Default</BasicRuntimeChecks>

Loading…
Cancel
Save