job/spinlock: add simple spinlock implementation

This commit is contained in:
Danny Robson 2018-03-14 16:17:39 +11:00
parent 3a26a98db7
commit f04e6e4de5
4 changed files with 177 additions and 0 deletions

View File

@ -270,6 +270,8 @@ list (
job/flag.hpp job/flag.hpp
job/queue.cpp job/queue.cpp
job/queue.hpp job/queue.hpp
job/spinlock.cpp
job/spinlock.hpp
json/fwd.hpp json/fwd.hpp
json/except.cpp json/except.cpp
json/except.hpp json/except.hpp
@ -480,6 +482,7 @@ if (TESTS)
job/event job/event
job/flag job/flag
job/queue job/queue
job/spinlock
json_types json_types
json2/event json2/event
maths maths

57
job/spinlock.cpp Normal file
View File

@ -0,0 +1,57 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/
#include "spinlock.hpp"
#include "../debug.hpp"
using util::job::spinlock;
///////////////////////////////////////////////////////////////////////////////
spinlock::spinlock ():
held (false)
{ ; }
//-----------------------------------------------------------------------------
spinlock::spinlock (spinlock &&rhs):
held (rhs.held.load ())
{ ; }
///////////////////////////////////////////////////////////////////////////////
void
spinlock::lock (void)
{
do {
bool expected = false;
if (held.compare_exchange_strong (expected, true))
return;
while (held)
__asm__ volatile ("pause");
} while (true);
}
//-----------------------------------------------------------------------------
void
spinlock::unlock (void)
{
CHECK (held);
held = false;
}

52
job/spinlock.hpp Normal file
View File

@ -0,0 +1,52 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_JOB_SPINLOCK_HPP
#define CRUFT_UTIL_JOB_SPINLOCK_HPP
#include <atomic>
namespace util::job {
/// a CPU intensive, but lower latency, lock.
///
/// std::atomic_flag seems like it might have been a good option on which
/// to base our implementation, but it appears potentially expensive to
/// spin over without a cheap read operation.
///
/// satisfies BasicLockable.
class spinlock {
public:
spinlock ();
spinlock (spinlock &&);
spinlock& operator= (spinlock &&);
spinlock (const spinlock&) = delete;
spinlock& operator= (const spinlock&) = delete;
void lock ();
void unlock ();
private:
static_assert (
std::atomic<bool>::is_always_lock_free,
"we require a lock free primitive or the entire point is moot"
);
std::atomic<bool> held;
};
};
#endif

65
test/job/spinlock.cpp Normal file
View File

@ -0,0 +1,65 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/
#include "job/flag.hpp"
#include "job/spinlock.hpp"
#include "tap.hpp"
#include <thread>
#include <mutex>
///////////////////////////////////////////////////////////////////////////////
void
fight (util::job::flag &start, util::job::spinlock &l, int iterations)
{
start.wait ();
for (int count = 0; count < iterations; ++count)
std::lock_guard g (l);
};
//-----------------------------------------------------------------------------
int
main ()
{
util::TAP::logger tap;
util::job::spinlock l;
l.lock ();
tap.expect (true, "locked without contention");
l.unlock ();
tap.expect (true, "unlocked without contention");
if (std::thread::hardware_concurrency () < 2) {
tap.skip ("n-way fight");
} else {
constexpr int iterations = 1 << 16;
util::job::flag start_flag;
std::vector<std::thread> contestants;
for (unsigned i = 0; i < std::thread::hardware_concurrency (); ++i)
contestants.emplace_back (fight, std::ref (start_flag), std::ref (l), iterations);
start_flag.notify ();
for (auto &t: contestants)
t.join ();
tap.expect (true, "n-way fight, %! contestants", std::thread::hardware_concurrency ());
}
}