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>
|
|
|
|
*/
|
|
|
|
|
2018-03-23 14:10:20 +11:00
|
|
|
#include "thread/flag.hpp"
|
|
|
|
#include "thread/spinlock.hpp"
|
2019-06-22 15:46:34 +10:00
|
|
|
#include "thread/thread.hpp"
|
|
|
|
|
2018-03-14 16:17:39 +11:00
|
|
|
#include "tap.hpp"
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
fight (cruft::thread::flag &start, cruft::thread::spinlock &l, int iterations)
|
2018-03-14 16:17:39 +11:00
|
|
|
{
|
|
|
|
start.wait ();
|
|
|
|
for (int count = 0; count < iterations; ++count)
|
|
|
|
std::lock_guard g (l);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
main ()
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2018-03-14 16:17:39 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::thread::spinlock l;
|
2018-03-14 16:17:39 +11:00
|
|
|
l.lock ();
|
|
|
|
tap.expect (true, "locked without contention");
|
|
|
|
|
|
|
|
l.unlock ();
|
|
|
|
tap.expect (true, "unlocked without contention");
|
|
|
|
|
2019-06-22 15:46:34 +10:00
|
|
|
if (cruft::thread::thread::hardware_concurrency () < 2) {
|
2018-03-14 16:17:39 +11:00
|
|
|
tap.skip ("n-way fight");
|
|
|
|
} else {
|
2018-03-15 23:49:12 +11:00
|
|
|
constexpr int iterations = 1 << 12;
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::thread::flag start_flag;
|
2018-03-14 16:17:39 +11:00
|
|
|
|
2019-06-22 15:46:34 +10:00
|
|
|
std::vector<cruft::thread::thread> contestants;
|
|
|
|
for (unsigned i = 0; i < cruft::thread::thread::hardware_concurrency (); ++i)
|
2018-03-14 16:17:39 +11:00
|
|
|
contestants.emplace_back (fight, std::ref (start_flag), std::ref (l), iterations);
|
|
|
|
|
2018-08-13 14:51:33 +10:00
|
|
|
start_flag.notify_all ();
|
2018-03-14 16:17:39 +11:00
|
|
|
|
|
|
|
for (auto &t: contestants)
|
|
|
|
t.join ();
|
|
|
|
|
2019-06-22 15:46:34 +10:00
|
|
|
tap.expect (true, "n-way fight, %! contestants", cruft::thread::thread::hardware_concurrency ());
|
2018-03-14 16:17:39 +11:00
|
|
|
}
|
2018-03-20 14:50:46 +11:00
|
|
|
|
|
|
|
return tap.status ();
|
2018-03-14 16:17:39 +11:00
|
|
|
}
|