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
|
|
|
*
|
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spinlock.hpp"
|
|
|
|
|
|
|
|
#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)
|
|
|
|
__asm__ volatile ("pause");
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
spinlock::unlock (void)
|
|
|
|
{
|
|
|
|
CHECK (held);
|
|
|
|
held = false;
|
|
|
|
}
|