mirror of
https://github.com/tearshark/librf.git
synced 2024-10-04 08:50:31 +08:00
在state上使用自旋锁替换递归锁,以减少内存占用
This commit is contained in:
parent
8de633c8b6
commit
0e5ac70991
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#include "librf.h"
|
#include "librf.h"
|
||||||
|
|
||||||
const size_t N = 1000000;
|
const size_t N = 10000000;
|
||||||
const size_t LOOP_COUNT = 100;
|
const size_t LOOP_COUNT = 100;
|
||||||
|
|
||||||
volatile size_t globalValue = 0;
|
volatile size_t globalValue = 0;
|
||||||
|
@ -134,7 +134,7 @@ RESUMEF_NS
|
|||||||
bool state_future_t::is_ready() const
|
bool state_future_t::is_ready() const
|
||||||
{
|
{
|
||||||
scoped_lock<lock_type> __guard(this->_mtx);
|
scoped_lock<lock_type> __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)
|
void state_future_t::set_exception(std::exception_ptr e)
|
||||||
@ -185,7 +185,7 @@ RESUMEF_NS
|
|||||||
|
|
||||||
if (this->_exception)
|
if (this->_exception)
|
||||||
std::rethrow_exception(std::move(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}));
|
std::rethrow_exception(std::make_exception_ptr(future_exception{error_code::not_ready}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,7 +193,7 @@ RESUMEF_NS
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
scoped_lock<lock_type> __guard(this->_mtx);
|
scoped_lock<lock_type> __guard(this->_mtx);
|
||||||
this->_has_value = true;
|
this->_has_value.store(true, std::memory_order_release);
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduler_t* sch = this->get_scheduler();
|
scheduler_t* sch = this->get_scheduler();
|
||||||
|
@ -92,7 +92,7 @@ RESUMEF_NS
|
|||||||
#endif
|
#endif
|
||||||
std::exception_ptr _exception;
|
std::exception_ptr _exception;
|
||||||
uint32_t _alloc_size;
|
uint32_t _alloc_size;
|
||||||
bool _has_value = false;
|
std::atomic<bool> _has_value{ false };
|
||||||
bool _is_awaitor;
|
bool _is_awaitor;
|
||||||
initor_type _is_initor = initor_type::None;
|
initor_type _is_initor = initor_type::None;
|
||||||
public:
|
public:
|
||||||
@ -143,8 +143,8 @@ RESUMEF_NS
|
|||||||
void future_await_suspend(coroutine_handle<_PromiseT> handler);
|
void future_await_suspend(coroutine_handle<_PromiseT> handler);
|
||||||
bool future_await_ready()
|
bool future_await_ready()
|
||||||
{
|
{
|
||||||
scoped_lock<lock_type> __guard(this->_mtx);
|
//scoped_lock<lock_type> __guard(this->_mtx);
|
||||||
return _has_value;
|
return _has_value.load(std::memory_order_acquire);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _PromiseT, typename = std::enable_if_t<is_promise_v<_PromiseT>>>
|
template<class _PromiseT, typename = std::enable_if_t<is_promise_v<_PromiseT>>>
|
||||||
@ -184,7 +184,7 @@ RESUMEF_NS
|
|||||||
public:
|
public:
|
||||||
~state_t()
|
~state_t()
|
||||||
{
|
{
|
||||||
if (_has_value)
|
if (_has_value.load(std::memory_order_acquire))
|
||||||
cast_value_ptr()->~value_type();
|
cast_value_ptr()->~value_type();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ RESUMEF_NS
|
|||||||
this->_coro = handler;
|
this->_coro = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->_has_value = true;
|
this->_has_value.store(true, std::memory_order_release);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!handler.done())
|
if (!handler.done())
|
||||||
@ -90,14 +90,14 @@ RESUMEF_NS
|
|||||||
this->_coro = handler;
|
this->_coro = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->_has_value)
|
if (this->_has_value.load(std::memory_order_acquire))
|
||||||
{
|
{
|
||||||
*this->cast_value_ptr() = std::forward<U>(val);
|
*this->cast_value_ptr() = std::forward<U>(val);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
new (this->cast_value_ptr()) value_type(std::forward<U>(val));
|
new (this->cast_value_ptr()) value_type(std::forward<U>(val));
|
||||||
this->_has_value = true;
|
this->_has_value.store(true, std::memory_order_release);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ RESUMEF_NS
|
|||||||
scoped_lock<lock_type> __guard(this->_mtx);
|
scoped_lock<lock_type> __guard(this->_mtx);
|
||||||
if (this->_exception)
|
if (this->_exception)
|
||||||
std::rethrow_exception(std::move(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}));
|
std::rethrow_exception(std::make_exception_ptr(future_exception{error_code::not_ready}));
|
||||||
|
|
||||||
return std::move(*this->cast_value_ptr());
|
return std::move(*this->cast_value_ptr());
|
||||||
@ -128,14 +128,14 @@ RESUMEF_NS
|
|||||||
{
|
{
|
||||||
scoped_lock<lock_type> __guard(this->_mtx);
|
scoped_lock<lock_type> __guard(this->_mtx);
|
||||||
|
|
||||||
if (this->_has_value)
|
if (this->_has_value.load(std::memory_order_acquire))
|
||||||
{
|
{
|
||||||
*this->cast_value_ptr() = std::forward<U>(val);
|
*this->cast_value_ptr() = std::forward<U>(val);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
new (this->cast_value_ptr()) value_type(std::forward<U>(val));
|
new (this->cast_value_ptr()) value_type(std::forward<U>(val));
|
||||||
this->_has_value = true;
|
this->_has_value.store(true, std::memory_order_release);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,8 +31,8 @@ int main(int argc, const char* argv[])
|
|||||||
{
|
{
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
//resumable_main_layout();
|
resumable_main_layout();
|
||||||
//return 0;
|
return 0;
|
||||||
|
|
||||||
//if (argc > 1)
|
//if (argc > 1)
|
||||||
// resumable_main_benchmark_asio_client(atoi(argv[1]));
|
// resumable_main_benchmark_asio_client(atoi(argv[1]));
|
||||||
|
Loading…
Reference in New Issue
Block a user