From 1c5abaa518343cd1d56e4b02795e96997003d41b Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 19 Sep 2017 17:50:02 +0300 Subject: [PATCH] Remove mutex locks from rpl for now. --- Telegram/SourceFiles/rpl/consumer.h | 53 ++++----------------- Telegram/SourceFiles/rpl/details/callable.h | 27 ++++++++--- 2 files changed, 31 insertions(+), 49 deletions(-) diff --git a/Telegram/SourceFiles/rpl/consumer.h b/Telegram/SourceFiles/rpl/consumer.h index 3c41ec9b7..f39369b03 100644 --- a/Telegram/SourceFiles/rpl/consumer.h +++ b/Telegram/SourceFiles/rpl/consumer.h @@ -20,7 +20,6 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org */ #pragma once -#include #include #include #include @@ -138,7 +137,6 @@ public: protected: lifetime _lifetime; bool _terminated = false; - std::mutex _mutex; }; @@ -279,10 +277,7 @@ inline void consumer::terminate() const { template inline void consumer::abstract_instance::add_lifetime( lifetime &&lifetime) { - std::unique_lock lock(_mutex); if (_terminated) { - lock.unlock(); - lifetime.destroy(); } else { _lifetime.add(std::move(lifetime)); @@ -293,20 +288,15 @@ template template inline Type *consumer::abstract_instance::make_state( Args&& ...args) { - std::unique_lock lock(_mutex); Expects(!_terminated); return _lifetime.make_state(std::forward(args)...); } template inline void consumer::abstract_instance::terminate() { - std::unique_lock lock(_mutex); if (!_terminated) { _terminated = true; - auto handler = std::exchange(_lifetime, lifetime()); - lock.unlock(); - - handler.destroy(); + base::take(_lifetime).destroy(); } } @@ -314,17 +304,11 @@ template template bool consumer::instance::put_next( Value &&value) { - std::unique_lock lock(this->_mutex); if (this->_terminated) { return false; } auto handler = this->_next; - lock.unlock(); - - details::callable_helper( - handler, - std::move(value), - details::is_callable_plain()); + details::callable_invoke(std::move(handler), std::move(value)); return true; } @@ -332,17 +316,11 @@ template template bool consumer::instance::put_next_copy( const Value &value) { - std::unique_lock lock(this->_mutex); if (this->_terminated) { return false; } auto handler = this->_next; - lock.unlock(); - - details::const_ref_call_helper( - handler, - value, - details::allows_const_ref()); + details::const_ref_call_invoke(std::move(handler), value); return true; } @@ -350,12 +328,10 @@ template template void consumer::instance::put_error( Error &&error) { - std::unique_lock lock(this->_mutex); if (!this->_terminated) { - auto handler = std::move(this->_error); - lock.unlock(); - - details::callable_invoke(handler, std::move(error)); + details::callable_invoke( + std::move(this->_error), + std::move(error)); this->terminate(); } } @@ -364,15 +340,10 @@ template template void consumer::instance::put_error_copy( const Error &error) { - std::unique_lock lock(this->_mutex); if (!this->_terminated) { - auto handler = std::move(this->_error); - lock.unlock(); - - details::const_ref_call_helper( - handler, - error, - details::allows_const_ref()); + details::const_ref_call_invoke( + std::move(this->_error), + error); this->terminate(); } } @@ -380,12 +351,8 @@ void consumer::instance::put_error_copy( template template void consumer::instance::put_done() { - std::unique_lock lock(this->_mutex); if (!this->_terminated) { - auto handler = std::move(this->_done); - lock.unlock(); - - handler(); + std::move(this->_done)(); this->terminate(); } } diff --git a/Telegram/SourceFiles/rpl/details/callable.h b/Telegram/SourceFiles/rpl/details/callable.h index 6b5b853e0..146f1a479 100644 --- a/Telegram/SourceFiles/rpl/details/callable.h +++ b/Telegram/SourceFiles/rpl/details/callable.h @@ -21,6 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #pragma once #include "base/build_config.h" +#include // Custom libc++ build used for old OS X versions already has this. @@ -150,12 +151,14 @@ constexpr bool is_callable_v = is_callable::value; template inline decltype(auto) callable_helper(Method &&method, Arg &&arg, std::true_type) { - return std::move(method)(std::forward(arg)); + return std::forward(method)(std::forward(arg)); } template inline decltype(auto) callable_helper(Method &&method, Arg &&arg, std::false_type) { - return std::apply(std::move(method), std::forward(arg)); + return std::apply( + std::forward(method), + std::forward(arg)); } template @@ -192,19 +195,31 @@ struct allows_const_ref template inline decltype(auto) const_ref_call_helper( - Method &method, + Method &&method, const Arg &arg, std::true_type) { - return callable_invoke(method, arg); + return callable_invoke(std::forward(method), arg); } template inline decltype(auto) const_ref_call_helper( - Method &method, + Method &&method, const Arg &arg, std::false_type) { auto copy = arg; - return callable_invoke(method, std::move(copy)); + return callable_invoke( + std::forward(method), + std::move(copy)); +} + +template +inline decltype(auto) const_ref_call_invoke( + Method &&method, + const Arg &arg) { + return const_ref_call_helper( + std::forward(method), + arg, + allows_const_ref()); } } // namespace details