thread/spinlock: don't try to lock during a move operation

This commit is contained in:
Danny Robson 2019-05-24 11:47:19 +10:00
parent ec883c3009
commit 747cfa9237
2 changed files with 7 additions and 14 deletions

View File

@ -30,14 +30,7 @@ spinlock::spinlock (spinlock &&rhs) noexcept:
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
spinlock& spinlock::operator= (spinlock &&rhs) noexcept spinlock& spinlock::operator= (spinlock &&rhs) noexcept
{ {
lock ();
rhs.lock ();
held = rhs.held.load (); held = rhs.held.load ();
rhs.unlock ();
unlock ();
return *this; return *this;
} }

View File

@ -3,22 +3,24 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018-2019 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_THREAD_SPINLOCK_HPP #pragma once
#define CRUFT_UTIL_THREAD_SPINLOCK_HPP
#include <atomic> #include <atomic>
namespace cruft::thread { namespace cruft::thread {
/// a CPU intensive, but lower latency, lock. /// A CPU intensive, but lower latency, lock.
/// ///
/// std::atomic_flag seems like it might have been a good option on which /// std::atomic_flag seems like it might have been a good option on which
/// to base our implementation, but it appears potentially expensive to /// to base our implementation, but it appears potentially expensive to
/// spin over without a cheap read operation. /// spin over without a cheap read operation.
/// ///
/// satisfies BasicLockable. /// Satisfies BasicLockable.
///
/// NOTE: All move operations assume that either the lock is held by the
/// client performing the move, or that is not held by any client.
class spinlock { class spinlock {
public: public:
spinlock (); spinlock ();
@ -40,5 +42,3 @@ namespace cruft::thread {
std::atomic<bool> held; std::atomic<bool> held;
}; };
}; };
#endif