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

审查mutex_v2代码

This commit is contained in:
tearshark 2020-03-22 14:19:10 +08:00
parent 9d94bfa30c
commit 6fef43340a
10 changed files with 61 additions and 140 deletions

View File

@ -358,6 +358,7 @@ inline namespace channel_v2
read_awaiter(channel_type* ch) noexcept read_awaiter(channel_type* ch) noexcept
: _channel(ch) : _channel(ch)
, _value()
{} {}
~read_awaiter() ~read_awaiter()

View File

@ -219,11 +219,9 @@ RESUMEF_NS
clock_type::time_point _tp; clock_type::time_point _tp;
}; };
struct [[nodiscard]] event_t::timeout_awaiter : timeout_awaitor_impl<event_t::awaiter> struct [[nodiscard]] event_t::timeout_awaiter : timeout_awaitor_impl<awaiter>
{ {
timeout_awaiter(clock_type::time_point tp, detail::event_v2_impl* evt) noexcept using timeout_awaitor_impl<awaiter>::timeout_awaitor_impl;
: timeout_awaitor_impl<event_t::awaiter>(tp, evt)
{}
}; };
template<class _Rep, class _Period> template<class _Rep, class _Period>
@ -331,11 +329,9 @@ RESUMEF_NS
template<class _Iter> template<class _Iter>
struct [[nodiscard]] event_t::timeout_any_awaiter : timeout_awaitor_impl<event_t::any_awaiter<_Iter>> struct [[nodiscard]] event_t::timeout_any_awaiter : timeout_awaitor_impl<any_awaiter<_Iter>>
{ {
timeout_any_awaiter(clock_type::time_point tp, _Iter begin, _Iter end) noexcept using timeout_awaitor_impl<any_awaiter<_Iter>>::timeout_awaitor_impl;
: timeout_awaitor_impl<event_t::any_awaiter<_Iter>>(tp, begin, end)
{}
}; };
template<class _Rep, class _Period, class _Iter COMMA_RESUMEF_ENABLE_IF_TYPENAME()> template<class _Rep, class _Period, class _Iter COMMA_RESUMEF_ENABLE_IF_TYPENAME()>
@ -445,11 +441,9 @@ RESUMEF_NS
template<class _Iter> template<class _Iter>
struct [[nodiscard]] event_t::timeout_all_awaiter : timeout_awaitor_impl<event_t::all_awaiter<_Iter>> struct [[nodiscard]] event_t::timeout_all_awaiter : timeout_awaitor_impl<all_awaiter<_Iter>>
{ {
timeout_all_awaiter(clock_type::time_point tp, _Iter begin, _Iter end) noexcept using timeout_awaitor_impl<all_awaiter<_Iter>>::timeout_awaitor_impl;
: timeout_awaitor_impl<event_t::all_awaiter<_Iter>>(tp, begin, end)
{}
}; };
template<class _Rep, class _Period, class _Iter COMMA_RESUMEF_ENABLE_IF_TYPENAME()> template<class _Rep, class _Period, class _Iter COMMA_RESUMEF_ENABLE_IF_TYPENAME()>

View File

@ -4,7 +4,7 @@ RESUMEF_NS
{ {
namespace detail namespace detail
{ {
void state_mutex_base_t::resume() void state_mutex_t::resume()
{ {
coroutine_handle<> handler = _coro; coroutine_handle<> handler = _coro;
if (handler) if (handler)
@ -15,12 +15,12 @@ RESUMEF_NS
} }
} }
bool state_mutex_base_t::has_handler() const noexcept bool state_mutex_t::has_handler() const noexcept
{ {
return (bool)_coro; return (bool)_coro;
} }
state_base_t* state_mutex_base_t::get_parent() const noexcept state_base_t* state_mutex_t::get_parent() const noexcept
{ {
return _root; return _root;
} }
@ -74,55 +74,14 @@ RESUMEF_NS
return false; return false;
} }
void state_mutex_t::add_timeout_timer(std::chrono::system_clock::time_point tp)
void state_mutex_all_t::on_cancel() noexcept
{ {
intptr_t oldValue = _counter.load(std::memory_order_acquire); this->_thandler = this->_scheduler->timer()->add_handler(tp,
if (oldValue >= 0 && _counter.compare_exchange_strong(oldValue, -1, std::memory_order_acq_rel)) [st = counted_ptr<state_mutex_t>{ this }](bool canceld)
{ {
*_value = false; if (!canceld)
_thandler.stop(); st->on_timeout();
});
this->_coro = nullptr;
}
}
bool state_mutex_all_t::on_notify(event_v2_impl*)
{
intptr_t oldValue = _counter.load(std::memory_order_acquire);
if (oldValue <= 0) return false;
oldValue = _counter.fetch_add(-1, std::memory_order_acq_rel);
if (oldValue == 1)
{
*_value = true;
_thandler.stop();
assert(this->_scheduler != nullptr);
if (this->_coro)
this->_scheduler->add_generator(this);
return true;
}
return oldValue >= 1;
}
bool state_mutex_all_t::on_timeout()
{
intptr_t oldValue = _counter.load(std::memory_order_acquire);
if (oldValue >= 0 && _counter.compare_exchange_strong(oldValue, -1, std::memory_order_acq_rel))
{
*_value = false;
_thandler.reset();
assert(this->_scheduler != nullptr);
if (this->_coro)
this->_scheduler->add_generator(this);
return true;
}
return false;
} }
@ -164,16 +123,16 @@ RESUMEF_NS
{ {
assert(sch != nullptr); assert(sch != nullptr);
void* oldValue = _owner.load(std::memory_order_acquire); void* oldValue = _owner.load(std::memory_order_relaxed);
if (oldValue == nullptr) if (oldValue == nullptr)
{ {
_owner.store(sch, std::memory_order_release); _owner.store(sch, std::memory_order_relaxed);
_counter.fetch_add(1, std::memory_order_acq_rel); _counter.fetch_add(1, std::memory_order_relaxed);
return true; return true;
} }
if (oldValue == sch) if (oldValue == sch)
{ {
_counter.fetch_add(1, std::memory_order_acq_rel); _counter.fetch_add(1, std::memory_order_relaxed);
return true; return true;
} }
return false; return false;
@ -185,12 +144,13 @@ RESUMEF_NS
scoped_lock<lock_type> lock_(_lock); scoped_lock<lock_type> lock_(_lock);
void* oldValue = _owner.load(std::memory_order_acquire); void* oldValue = _owner.load(std::memory_order_relaxed);
if (oldValue == sch) if (oldValue == sch)
{ {
if (_counter.fetch_sub(1, std::memory_order_acquire) == 1) if (_counter.fetch_sub(1, std::memory_order_relaxed) == 1)
{ {
_owner.store(nullptr, std::memory_order_release); _owner.store(nullptr, std::memory_order_relaxed);
while (!_wait_awakes.empty()) while (!_wait_awakes.empty())
{ {
state_mutex_ptr state = _wait_awakes.front(); state_mutex_ptr state = _wait_awakes.front();
@ -204,8 +164,8 @@ RESUMEF_NS
break; break;
//转移状态失败,恢复成空 //转移状态失败,恢复成空
_owner.store(nullptr, std::memory_order_release); _owner.store(nullptr, std::memory_order_relaxed);
_counter.fetch_sub(1, std::memory_order_acq_rel); _counter.fetch_sub(1, std::memory_order_relaxed);
} }
} }

View File

@ -4,15 +4,21 @@ RESUMEF_NS
{ {
namespace detail namespace detail
{ {
struct state_mutex_base_t : public state_base_t struct state_mutex_t : public state_base_t
{ {
state_mutex_t(mutex_v2_impl*& val)
: _value(&val)
{}
virtual void resume() override; virtual void resume() override;
virtual bool has_handler() const noexcept override; virtual bool has_handler() const noexcept override;
virtual state_base_t* get_parent() const noexcept override; virtual state_base_t* get_parent() const noexcept override;
virtual void on_cancel() noexcept = 0; void on_cancel() noexcept;
virtual bool on_notify(mutex_v2_impl* eptr) = 0; bool on_notify(mutex_v2_impl* eptr);
virtual bool on_timeout() = 0; bool on_timeout();
void add_timeout_timer(std::chrono::system_clock::time_point tp);
inline scheduler_t* get_scheduler() const noexcept inline scheduler_t* get_scheduler() const noexcept
{ {
@ -26,62 +32,22 @@ RESUMEF_NS
this->_root = root; this->_root = root;
} }
inline void add_timeout_timer(std::chrono::system_clock::time_point tp)
{
this->_thandler = this->_scheduler->timer()->add_handler(tp,
[st = counted_ptr<state_mutex_base_t>{ this }](bool canceld)
{
if (!canceld)
st->on_timeout();
});
}
timer_handler _thandler; timer_handler _thandler;
protected: protected:
state_base_t* _root; state_base_t* _root;
};
struct state_mutex_t : public state_mutex_base_t
{
state_mutex_t(mutex_v2_impl*& val)
: _value(&val)
{}
virtual void on_cancel() noexcept override;
virtual bool on_notify(mutex_v2_impl* eptr) override;
virtual bool on_timeout() override;
public:
timer_handler _thandler;
protected:
std::atomic<mutex_v2_impl**> _value; std::atomic<mutex_v2_impl**> _value;
}; };
struct state_mutex_all_t : public state_event_base_t
{
state_mutex_all_t(intptr_t count, bool& val)
: _counter(count)
, _value(&val)
{}
virtual void on_cancel() noexcept override;
virtual bool on_notify(event_v2_impl* eptr) override;
virtual bool on_timeout() override;
public:
timer_handler _thandler;
std::atomic<intptr_t> _counter;
protected:
bool* _value;
};
struct mutex_v2_impl : public std::enable_shared_from_this<mutex_v2_impl> struct mutex_v2_impl : public std::enable_shared_from_this<mutex_v2_impl>
{ {
using clock_type = std::chrono::system_clock; using clock_type = std::chrono::system_clock;
mutex_v2_impl() {} mutex_v2_impl() {}
inline void* owner() const noexcept inline void* owner() noexcept
{ {
return _owner.load(std::memory_order_acquire); scoped_lock<lock_type> lock_(_lock);
return _owner.load(std::memory_order_relaxed);
} }
bool try_lock(void* sch); //内部加锁 bool try_lock(void* sch); //内部加锁
@ -171,7 +137,9 @@ RESUMEF_NS
{ {
typedef std::shared_ptr<detail::mutex_v2_impl> mutex_impl_ptr; typedef std::shared_ptr<detail::mutex_v2_impl> mutex_impl_ptr;
scoped_unlock_t() {} scoped_unlock_t()
: _owner(nullptr)
{}
//此函数应该在try_lock()获得锁后使用 //此函数应该在try_lock()获得锁后使用
//或者在协程里由awaiter使用 //或者在协程里由awaiter使用
@ -405,12 +373,9 @@ RESUMEF_NS
struct [[nodiscard]] mutex_t::timeout_awaiter : public event_t::timeout_awaitor_impl<awaiter> struct [[nodiscard]] mutex_t::timeout_awaiter : public event_t::timeout_awaitor_impl<lock_awaiter>
{ {
timeout_awaiter(clock_type::time_point tp, detail::mutex_v2_impl * mtx) noexcept using event_t::timeout_awaitor_impl<lock_awaiter>::timeout_awaitor_impl;
: event_t::timeout_awaitor_impl<mutex_t::awaiter>(tp, mtx)
{}
bool await_resume() noexcept bool await_resume() noexcept
{ {
detail::mutex_v2_impl* mtx = this->_mutex; detail::mutex_v2_impl* mtx = this->_mutex;

View File

@ -2,7 +2,7 @@
RESUMEF_NS RESUMEF_NS
{ {
future_t<> sleep_until_(const std::chrono::system_clock::time_point& tp_, scheduler_t& scheduler_) future_t<> sleep_until_(std::chrono::system_clock::time_point tp_, scheduler_t& scheduler_)
{ {
awaitable_t<> awaitable; awaitable_t<> awaitable;

View File

@ -8,32 +8,32 @@ RESUMEF_NS
{ {
struct scheduler_t; struct scheduler_t;
future_t<> sleep_until_(const std::chrono::system_clock::time_point& tp_, scheduler_t& scheduler_); future_t<> sleep_until_(std::chrono::system_clock::time_point tp_, scheduler_t& scheduler_);
inline future_t<> sleep_for_(const std::chrono::system_clock::duration& dt_, scheduler_t& scheduler_) inline future_t<> sleep_for_(std::chrono::system_clock::duration dt_, scheduler_t& scheduler_)
{ {
return sleep_until_(std::chrono::system_clock::now() + dt_, scheduler_); return sleep_until_(std::chrono::system_clock::now() + dt_, scheduler_);
} }
template<class _Rep, class _Period> template<class _Rep, class _Period>
inline future_t<> sleep_for(const std::chrono::duration<_Rep, _Period>& dt_, scheduler_t& scheduler_) inline future_t<> sleep_for(std::chrono::duration<_Rep, _Period> dt_, scheduler_t& scheduler_)
{ {
return sleep_for_(std::chrono::duration_cast<std::chrono::system_clock::duration>(dt_), scheduler_); return sleep_for_(std::chrono::duration_cast<std::chrono::system_clock::duration>(dt_), scheduler_);
} }
template<class _Clock, class _Duration = typename _Clock::duration> template<class _Clock, class _Duration = typename _Clock::duration>
inline future_t<> sleep_until(const std::chrono::time_point<_Clock, _Duration>& tp_, scheduler_t& scheduler_) inline future_t<> sleep_until(std::chrono::time_point<_Clock, _Duration> tp_, scheduler_t& scheduler_)
{ {
return sleep_until_(std::chrono::time_point_cast<std::chrono::system_clock::duration>(tp_), scheduler_); return sleep_until_(std::chrono::time_point_cast<std::chrono::system_clock::duration>(tp_), scheduler_);
} }
template<class _Rep, class _Period> template<class _Rep, class _Period>
inline future_t<> sleep_for(const std::chrono::duration<_Rep, _Period>& dt_) inline future_t<> sleep_for(std::chrono::duration<_Rep, _Period> dt_)
{ {
co_await sleep_for_(std::chrono::duration_cast<std::chrono::system_clock::duration>(dt_), *current_scheduler()); co_await sleep_for_(std::chrono::duration_cast<std::chrono::system_clock::duration>(dt_), *current_scheduler());
} }
template<class _Clock, class _Duration> template<class _Clock, class _Duration>
inline future_t<> sleep_until(const std::chrono::time_point<_Clock, _Duration>& tp_) inline future_t<> sleep_until(std::chrono::time_point<_Clock, _Duration> tp_)
{ {
co_await sleep_until_(std::chrono::time_point_cast<std::chrono::system_clock::duration>(tp_), *current_scheduler()); co_await sleep_until_(std::chrono::time_point_cast<std::chrono::system_clock::duration>(tp_), *current_scheduler());
} }

View File

@ -323,7 +323,9 @@ inline namespace when_v2
auto when_any(scheduler_t& sch, _Awaitable&&... args) auto when_any(scheduler_t& sch, _Awaitable&&... args)
-> detail::when_future_t<when_any_pair> -> detail::when_future_t<when_any_pair>
{ {
#pragma warning(disable : 6326) //warning C6326: Potential comparison of a constant with another constant.
detail::when_future_t<when_any_pair> awaitor{ sizeof...(_Awaitable) > 0 ? 1 : 0 }; detail::when_future_t<when_any_pair> awaitor{ sizeof...(_Awaitable) > 0 ? 1 : 0 };
#pragma warning(default : 6326)
awaitor._values->first = -1; awaitor._values->first = -1;
detail::when_any_one__(sch, awaitor._state.get(), awaitor._values, 0, std::forward<_Awaitable>(args)...); detail::when_any_one__(sch, awaitor._state.get(), awaitor._values, 0, std::forward<_Awaitable>(args)...);

View File

@ -143,7 +143,7 @@ static void resumable_mutex_async()
static future_t<> resumable_mutex_range_push(size_t idx, mutex_t a, mutex_t b, mutex_t c) static future_t<> resumable_mutex_range_push(size_t idx, mutex_t a, mutex_t b, mutex_t c)
{ {
for (int i = 0; i < 1000; ++i) for (int i = 0; i < 100000; ++i)
{ {
scoped_unlock_t __lockers = co_await mutex_t::lock(a, b, c); scoped_unlock_t __lockers = co_await mutex_t::lock(a, b, c);
assert(a.is_locked()); assert(a.is_locked());
@ -159,7 +159,7 @@ static future_t<> resumable_mutex_range_push(size_t idx, mutex_t a, mutex_t b, m
static future_t<> resumable_mutex_range_pop(size_t idx, mutex_t a, mutex_t b, mutex_t c) static future_t<> resumable_mutex_range_pop(size_t idx, mutex_t a, mutex_t b, mutex_t c)
{ {
for (int i = 0; i < 1000; ++i) for (int i = 0; i < 100000; ++i)
{ {
scoped_unlock_t __lockers = co_await mutex_t::lock(a, b, c); scoped_unlock_t __lockers = co_await mutex_t::lock(a, b, c);
assert(a.is_locked()); assert(a.is_locked());

View File

@ -43,8 +43,8 @@ int main(int argc, const char* argv[])
//test_ring_queue<resumef::ring_queue_spinlock<int, false, uint32_t>>(); //test_ring_queue<resumef::ring_queue_spinlock<int, false, uint32_t>>();
//test_ring_queue<resumef::ring_queue_lockfree<int, uint64_t>>(); //test_ring_queue<resumef::ring_queue_lockfree<int, uint64_t>>();
resumable_main_mutex(); //resumable_main_mutex();
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]));

View File

@ -46,7 +46,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset> <PlatformToolset>ClangCL</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>NotSet</CharacterSet> <CharacterSet>NotSet</CharacterSet>
</PropertyGroup> </PropertyGroup>
@ -77,7 +77,6 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet> <CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
<EnableClangTidyCodeAnalysis>true</EnableClangTidyCodeAnalysis>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
@ -158,7 +157,7 @@
<MultiProcessorCompilation>true</MultiProcessorCompilation> <MultiProcessorCompilation>true</MultiProcessorCompilation>
<SDLCheck> <SDLCheck>
</SDLCheck> </SDLCheck>
<EnablePREfast>false</EnablePREfast> <EnablePREfast>true</EnablePREfast>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion> <InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<OmitFramePointers>true</OmitFramePointers> <OmitFramePointers>true</OmitFramePointers>
<EnableFiberSafeOptimizations>true</EnableFiberSafeOptimizations> <EnableFiberSafeOptimizations>true</EnableFiberSafeOptimizations>