1
0
mirror of https://github.com/tearshark/librf.git synced 2024-10-01 15:57:07 +08:00

减少event的一个自旋锁

This commit is contained in:
tearshark 2020-03-10 22:51:44 +08:00
parent 1b344cc651
commit 042e85f077
2 changed files with 15 additions and 19 deletions

View File

@ -22,24 +22,20 @@ RESUMEF_NS
void state_event_t::on_cancel() noexcept void state_event_t::on_cancel() noexcept
{ {
scoped_lock<lock_type> lock_(_mtx); bool* oldValue = _value.load(std::memory_order_acquire);
if (oldValue != nullptr && _value.compare_exchange_weak(oldValue, nullptr, std::memory_order_acq_rel))
if (_value != nullptr)
{ {
*_value = false; *oldValue = false;
_value = nullptr;
}
this->_coro = nullptr; this->_coro = nullptr;
} }
}
bool state_event_t::on_notify() bool state_event_t::on_notify()
{ {
scoped_lock<lock_type> lock_(_mtx); bool* oldValue = _value.load(std::memory_order_acquire);
if (oldValue != nullptr && _value.compare_exchange_weak(oldValue, nullptr, std::memory_order_acq_rel))
if (_value != nullptr)
{ {
*_value = true; *oldValue = true;
_value = nullptr;
assert(this->_scheduler != nullptr); assert(this->_scheduler != nullptr);
if (this->_coro) if (this->_coro)
@ -52,12 +48,10 @@ RESUMEF_NS
bool state_event_t::on_timeout() bool state_event_t::on_timeout()
{ {
scoped_lock<lock_type> lock_(_mtx); bool* oldValue = _value.load(std::memory_order_acquire);
if (oldValue != nullptr && _value.compare_exchange_weak(oldValue, nullptr, std::memory_order_acq_rel))
if (_value != nullptr)
{ {
*_value = false; *oldValue = false;
_value = nullptr;
assert(this->_scheduler != nullptr); assert(this->_scheduler != nullptr);
if (this->_coro) if (this->_coro)

View File

@ -92,9 +92,11 @@ RESUMEF_NS
//为浸入式单向链表提供的next指针 //为浸入式单向链表提供的next指针
counted_ptr<state_event_t> _next = nullptr; counted_ptr<state_event_t> _next = nullptr;
private: private:
//std::atomic<bool*> _value; //_value引用awaitor保存的值这样可以尽可能减少创建state的可能。而不必进入没有state就没有value实体被用于返回。
bool* _value; //在调用on_notify()或on_timeout()任意之一后置为nullptr。
mutable lock_type _mtx; //这样来保证要么超时了要么响应了signal的通知了。
//这个指针在on_notify()和on_timeout()里,当作一个互斥的锁来防止同时进入两个函数
std::atomic<bool*> _value;
}; };
} }