2018-03-14 16:17:39 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/.
|
2018-03-14 16:17:39 +11:00
|
|
|
*
|
2019-02-02 14:55:13 +11:00
|
|
|
* Copyright 2018-2019 Danny Robson <danny@nerdcruft.net>
|
2018-03-14 16:17:39 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spinlock.hpp"
|
|
|
|
|
2019-02-02 14:55:13 +11:00
|
|
|
#include "primitive.hpp"
|
|
|
|
|
2018-03-14 16:17:39 +11:00
|
|
|
#include "../debug.hpp"
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::thread::spinlock;
|
2018-03-14 16:17:39 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
spinlock::spinlock ():
|
|
|
|
held (false)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-07-24 15:48:50 +10:00
|
|
|
spinlock::spinlock (spinlock &&rhs) noexcept:
|
2018-03-14 16:17:39 +11:00
|
|
|
held (rhs.held.load ())
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
spinlock::lock (void)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
bool expected = false;
|
2018-03-15 23:48:50 +11:00
|
|
|
if (held.compare_exchange_weak (expected, true))
|
2018-03-14 16:17:39 +11:00
|
|
|
return;
|
|
|
|
|
|
|
|
while (held)
|
2019-02-02 14:55:13 +11:00
|
|
|
pause ();
|
2018-03-14 16:17:39 +11:00
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
spinlock::unlock (void)
|
|
|
|
{
|
|
|
|
CHECK (held);
|
|
|
|
held = false;
|
|
|
|
}
|