Merge pull request #11772 from v1993/polyfill-thread-fixes

common/polyfill_thread: use std::forward where appropriate, qualify std::move calls
This commit is contained in:
liamwhite 2023-10-13 15:15:25 -04:00 committed by GitHub
commit a8bd02acd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,12 +15,13 @@
#include <condition_variable> #include <condition_variable>
#include <stop_token> #include <stop_token>
#include <thread> #include <thread>
#include <utility>
namespace Common { namespace Common {
template <typename Condvar, typename Lock, typename Pred> template <typename Condvar, typename Lock, typename Pred>
void CondvarWait(Condvar& cv, std::unique_lock<Lock>& lk, std::stop_token token, Pred&& pred) { void CondvarWait(Condvar& cv, std::unique_lock<Lock>& lk, std::stop_token token, Pred&& pred) {
cv.wait(lk, token, std::move(pred)); cv.wait(lk, token, std::forward<Pred>(pred));
} }
template <typename Rep, typename Period> template <typename Rep, typename Period>
@ -109,7 +110,7 @@ public:
// Insert the callback. // Insert the callback.
stop_state_callback ret = ++m_next_callback; stop_state_callback ret = ++m_next_callback;
m_callbacks.emplace(ret, move(f)); m_callbacks.emplace(ret, std::move(f));
return ret; return ret;
} }
@ -162,7 +163,7 @@ private:
friend class stop_source; friend class stop_source;
template <typename Callback> template <typename Callback>
friend class stop_callback; friend class stop_callback;
stop_token(shared_ptr<polyfill::stop_state> stop_state) : m_stop_state(move(stop_state)) {} stop_token(shared_ptr<polyfill::stop_state> stop_state) : m_stop_state(std::move(stop_state)) {}
private: private:
shared_ptr<polyfill::stop_state> m_stop_state; shared_ptr<polyfill::stop_state> m_stop_state;
@ -198,7 +199,7 @@ public:
private: private:
friend class jthread; friend class jthread;
explicit stop_source(shared_ptr<polyfill::stop_state> stop_state) explicit stop_source(shared_ptr<polyfill::stop_state> stop_state)
: m_stop_state(move(stop_state)) {} : m_stop_state(std::move(stop_state)) {}
private: private:
shared_ptr<polyfill::stop_state> m_stop_state; shared_ptr<polyfill::stop_state> m_stop_state;
@ -218,16 +219,16 @@ public:
C&& cb) noexcept(is_nothrow_constructible_v<Callback, C>) C&& cb) noexcept(is_nothrow_constructible_v<Callback, C>)
: m_stop_state(st.m_stop_state) { : m_stop_state(st.m_stop_state) {
if (m_stop_state) { if (m_stop_state) {
m_callback = m_stop_state->insert_callback(move(cb)); m_callback = m_stop_state->insert_callback(std::move(cb));
} }
} }
template <typename C> template <typename C>
requires constructible_from<Callback, C> requires constructible_from<Callback, C>
explicit stop_callback(stop_token&& st, explicit stop_callback(stop_token&& st,
C&& cb) noexcept(is_nothrow_constructible_v<Callback, C>) C&& cb) noexcept(is_nothrow_constructible_v<Callback, C>)
: m_stop_state(move(st.m_stop_state)) { : m_stop_state(std::move(st.m_stop_state)) {
if (m_stop_state) { if (m_stop_state) {
m_callback = m_stop_state->insert_callback(move(cb)); m_callback = m_stop_state->insert_callback(std::move(cb));
} }
} }
~stop_callback() { ~stop_callback() {
@ -260,7 +261,7 @@ public:
typename = enable_if_t<!is_same_v<remove_cvref_t<F>, jthread>>> typename = enable_if_t<!is_same_v<remove_cvref_t<F>, jthread>>>
explicit jthread(F&& f, Args&&... args) explicit jthread(F&& f, Args&&... args)
: m_stop_state(make_shared<polyfill::stop_state>()), : m_stop_state(make_shared<polyfill::stop_state>()),
m_thread(make_thread(move(f), move(args)...)) {} m_thread(make_thread(std::forward<F>(f), std::forward<Args>(args)...)) {}
~jthread() { ~jthread() {
if (joinable()) { if (joinable()) {
@ -317,9 +318,9 @@ private:
template <typename F, typename... Args> template <typename F, typename... Args>
thread make_thread(F&& f, Args&&... args) { thread make_thread(F&& f, Args&&... args) {
if constexpr (is_invocable_v<decay_t<F>, stop_token, decay_t<Args>...>) { if constexpr (is_invocable_v<decay_t<F>, stop_token, decay_t<Args>...>) {
return thread(move(f), get_stop_token(), move(args)...); return thread(std::forward<F>(f), get_stop_token(), std::forward<Args>(args)...);
} else { } else {
return thread(move(f), move(args)...); return thread(std::forward<F>(f), std::forward<Args>(args)...);
} }
} }