1
0
Mirror von https://github.com/tearshark/librf.git synchronisiert 2024-10-01 15:57:07 +08:00

减少event的一个自旋锁

Dieser Commit ist enthalten in:
tearshark 2020-03-10 22:51:44 +08:00
Ursprung 1b344cc651
Commit 042e85f077
2 geänderte Dateien mit 15 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -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;
}
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)

Datei anzeigen

@ -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;
};
}