mirror of
https://github.com/tearshark/librf.git
synced 2024-10-01 15:57:07 +08:00
减少event的一个自旋锁
This commit is contained in:
parent
1b344cc651
commit
042e85f077
@ -22,24 +22,20 @@ RESUMEF_NS
|
||||
|
||||
void state_event_t::on_cancel() noexcept
|
||||
{
|
||||
scoped_lock<lock_type> lock_(_mtx);
|
||||
|
||||
if (_value != nullptr)
|
||||
bool* oldValue = _value.load(std::memory_order_acquire);
|
||||
if (oldValue != nullptr && _value.compare_exchange_weak(oldValue, nullptr, std::memory_order_acq_rel))
|
||||
{
|
||||
*_value = false;
|
||||
_value = nullptr;
|
||||
}
|
||||
*oldValue = false;
|
||||
this->_coro = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool state_event_t::on_notify()
|
||||
{
|
||||
scoped_lock<lock_type> lock_(_mtx);
|
||||
|
||||
if (_value != nullptr)
|
||||
bool* oldValue = _value.load(std::memory_order_acquire);
|
||||
if (oldValue != nullptr && _value.compare_exchange_weak(oldValue, nullptr, std::memory_order_acq_rel))
|
||||
{
|
||||
*_value = true;
|
||||
_value = nullptr;
|
||||
*oldValue = true;
|
||||
|
||||
assert(this->_scheduler != nullptr);
|
||||
if (this->_coro)
|
||||
@ -52,12 +48,10 @@ RESUMEF_NS
|
||||
|
||||
bool state_event_t::on_timeout()
|
||||
{
|
||||
scoped_lock<lock_type> lock_(_mtx);
|
||||
|
||||
if (_value != nullptr)
|
||||
bool* oldValue = _value.load(std::memory_order_acquire);
|
||||
if (oldValue != nullptr && _value.compare_exchange_weak(oldValue, nullptr, std::memory_order_acq_rel))
|
||||
{
|
||||
*_value = false;
|
||||
_value = nullptr;
|
||||
*oldValue = false;
|
||||
|
||||
assert(this->_scheduler != nullptr);
|
||||
if (this->_coro)
|
||||
|
@ -92,9 +92,11 @@ RESUMEF_NS
|
||||
//为浸入式单向链表提供的next指针
|
||||
counted_ptr<state_event_t> _next = nullptr;
|
||||
private:
|
||||
//std::atomic<bool*> _value;
|
||||
bool* _value;
|
||||
mutable lock_type _mtx;
|
||||
//_value引用awaitor保存的值,这样可以尽可能减少创建state的可能。而不必进入没有state就没有value实体被用于返回。
|
||||
//在调用on_notify()或on_timeout()任意之一后,置为nullptr。
|
||||
//这样来保证要么超时了,要么响应了signal的通知了。
|
||||
//这个指针在on_notify()和on_timeout()里,当作一个互斥的锁来防止同时进入两个函数
|
||||
std::atomic<bool*> _value;
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user