From 0e5ac7099185934e068e77343d730ccffc45015a Mon Sep 17 00:00:00 2001 From: tearshark Date: Sun, 1 Mar 2020 15:36:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8state=E4=B8=8A=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E8=87=AA=E6=97=8B=E9=94=81=E6=9B=BF=E6=8D=A2=E9=80=92=E5=BD=92?= =?UTF-8?q?=E9=94=81=EF=BC=8C=E4=BB=A5=E5=87=8F=E5=B0=91=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=8D=A0=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- benchmark/benchmark_async_mem.cpp | 2 +- librf/src/state.cpp | 6 +++--- librf/src/state.h | 8 ++++---- librf/src/state.inl | 12 ++++++------ vs_proj/librf.cpp | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/benchmark/benchmark_async_mem.cpp b/benchmark/benchmark_async_mem.cpp index 436e902..1194e28 100644 --- a/benchmark/benchmark_async_mem.cpp +++ b/benchmark/benchmark_async_mem.cpp @@ -7,7 +7,7 @@ #include "librf.h" -const size_t N = 1000000; +const size_t N = 10000000; const size_t LOOP_COUNT = 100; volatile size_t globalValue = 0; diff --git a/librf/src/state.cpp b/librf/src/state.cpp index 0ac6aa6..43b2a2c 100644 --- a/librf/src/state.cpp +++ b/librf/src/state.cpp @@ -134,7 +134,7 @@ RESUMEF_NS bool state_future_t::is_ready() const { scoped_lock __guard(this->_mtx); - return _exception != nullptr || _has_value || !_is_awaitor; + return _exception != nullptr || _has_value.load(std::memory_order_acquire) || !_is_awaitor; } void state_future_t::set_exception(std::exception_ptr e) @@ -185,7 +185,7 @@ RESUMEF_NS if (this->_exception) std::rethrow_exception(std::move(this->_exception)); - if (!this->_has_value) + if (!this->_has_value.load(std::memory_order_acquire)) std::rethrow_exception(std::make_exception_ptr(future_exception{error_code::not_ready})); } @@ -193,7 +193,7 @@ RESUMEF_NS { { scoped_lock __guard(this->_mtx); - this->_has_value = true; + this->_has_value.store(true, std::memory_order_release); } scheduler_t* sch = this->get_scheduler(); diff --git a/librf/src/state.h b/librf/src/state.h index 6d46394..2887211 100644 --- a/librf/src/state.h +++ b/librf/src/state.h @@ -92,7 +92,7 @@ RESUMEF_NS #endif std::exception_ptr _exception; uint32_t _alloc_size; - bool _has_value = false; + std::atomic _has_value{ false }; bool _is_awaitor; initor_type _is_initor = initor_type::None; public: @@ -143,8 +143,8 @@ RESUMEF_NS void future_await_suspend(coroutine_handle<_PromiseT> handler); bool future_await_ready() { - scoped_lock __guard(this->_mtx); - return _has_value; + //scoped_lock __guard(this->_mtx); + return _has_value.load(std::memory_order_acquire); } template>> @@ -184,7 +184,7 @@ RESUMEF_NS public: ~state_t() { - if (_has_value) + if (_has_value.load(std::memory_order_acquire)) cast_value_ptr()->~value_type(); } diff --git a/librf/src/state.inl b/librf/src/state.inl index 5eb9970..2a9ed27 100644 --- a/librf/src/state.inl +++ b/librf/src/state.inl @@ -64,7 +64,7 @@ RESUMEF_NS this->_coro = handler; } - this->_has_value = true; + this->_has_value.store(true, std::memory_order_release); } if (!handler.done()) @@ -90,14 +90,14 @@ RESUMEF_NS this->_coro = handler; } - if (this->_has_value) + if (this->_has_value.load(std::memory_order_acquire)) { *this->cast_value_ptr() = std::forward(val); } else { new (this->cast_value_ptr()) value_type(std::forward(val)); - this->_has_value = true; + this->_has_value.store(true, std::memory_order_release); } } @@ -115,7 +115,7 @@ RESUMEF_NS scoped_lock __guard(this->_mtx); if (this->_exception) std::rethrow_exception(std::move(this->_exception)); - if (!this->_has_value) + if (!this->_has_value.load(std::memory_order_acquire)) std::rethrow_exception(std::make_exception_ptr(future_exception{error_code::not_ready})); return std::move(*this->cast_value_ptr()); @@ -128,14 +128,14 @@ RESUMEF_NS { scoped_lock __guard(this->_mtx); - if (this->_has_value) + if (this->_has_value.load(std::memory_order_acquire)) { *this->cast_value_ptr() = std::forward(val); } else { new (this->cast_value_ptr()) value_type(std::forward(val)); - this->_has_value = true; + this->_has_value.store(true, std::memory_order_release); } } diff --git a/vs_proj/librf.cpp b/vs_proj/librf.cpp index 7fd5245..95671f9 100644 --- a/vs_proj/librf.cpp +++ b/vs_proj/librf.cpp @@ -31,8 +31,8 @@ int main(int argc, const char* argv[]) { (void)argc; (void)argv; - //resumable_main_layout(); - //return 0; + resumable_main_layout(); + return 0; //if (argc > 1) // resumable_main_benchmark_asio_client(atoi(argv[1]));