2018-03-14 16:39:10 +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:39:10 +11:00
|
|
|
*
|
|
|
|
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2018-03-23 14:10:20 +11:00
|
|
|
#include "thread/flag.hpp"
|
|
|
|
#include "thread/ticketlock.hpp"
|
2019-06-22 15:46:34 +10:00
|
|
|
#include "thread/thread.hpp"
|
|
|
|
#include "thread/mutex.hpp"
|
2018-03-14 16:39:10 +11:00
|
|
|
|
2019-06-22 15:46:34 +10:00
|
|
|
#include "tap.hpp"
|
2018-03-14 16:39:10 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
using ffs_t = std::chrono::high_resolution_clock;
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
fight (cruft::thread::flag &start, cruft::thread::ticketlock &l, int total, ffs_t::time_point &finish) {
|
2018-03-14 16:39:10 +11:00
|
|
|
start.wait ();
|
|
|
|
|
|
|
|
for (int count = 0; count < total; ++count)
|
|
|
|
std::lock_guard g (l);
|
|
|
|
|
|
|
|
finish = std::chrono::high_resolution_clock::now ();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
main ()
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2018-03-14 16:39:10 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::thread::ticketlock l;
|
2018-03-14 16:39:10 +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 (true || cruft::thread::thread::hardware_concurrency () < 2) {
|
2018-03-14 16:39:10 +11:00
|
|
|
tap.skip ("reasonably fair under contention");
|
|
|
|
} else {
|
|
|
|
// measure fairness under contention is below some threshold
|
|
|
|
//
|
|
|
|
// * create two threads that will fight over a lock
|
|
|
|
// * measure the start time
|
|
|
|
// * fire an event to start the threads
|
|
|
|
// * record the time the threads finish
|
|
|
|
// * compare the difference in runtimes as a percentage
|
|
|
|
//
|
|
|
|
// we have to be reasonably generous with our test at the end because
|
|
|
|
// there's liable to be a lot of noise in the measurement with a test
|
|
|
|
// that runs in a short enough time period.
|
|
|
|
constexpr int iterations = 1 << 16;
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::thread::flag start_flag;
|
2018-03-14 16:39:10 +11:00
|
|
|
|
|
|
|
ffs_t::time_point a_finish, b_finish;
|
|
|
|
|
2019-06-22 15:46:34 +10:00
|
|
|
cruft::thread::thread a (
|
|
|
|
fight,
|
|
|
|
std::ref (start_flag),
|
|
|
|
std::ref (l),
|
|
|
|
iterations,
|
|
|
|
std::ref (a_finish)
|
|
|
|
);
|
|
|
|
|
|
|
|
cruft::thread::thread b (
|
|
|
|
fight,
|
|
|
|
std::ref (start_flag),
|
|
|
|
std::ref (l),
|
|
|
|
iterations,
|
|
|
|
std::ref (b_finish)
|
|
|
|
);
|
2018-03-14 16:39:10 +11:00
|
|
|
|
|
|
|
auto start = std::chrono::high_resolution_clock::now ();
|
2018-08-13 14:51:33 +10:00
|
|
|
start_flag.notify_all ();
|
2018-03-14 16:39:10 +11:00
|
|
|
|
|
|
|
a.join ();
|
|
|
|
b.join ();
|
|
|
|
|
|
|
|
auto a_total = std::chrono::duration_cast<std::chrono::nanoseconds> (a_finish - start).count ();
|
|
|
|
auto b_total = std::chrono::duration_cast<std::chrono::nanoseconds> (b_finish - start).count ();
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
auto diff = cruft::max (a_total, b_total) / float (cruft::min (a_total, b_total));
|
|
|
|
auto rel = cruft::abs (1 - diff);
|
2018-03-14 16:39:10 +11:00
|
|
|
tap.expect_lt (rel, 0.1f, "reasonably fair under contention");
|
|
|
|
}
|
2018-03-20 14:50:46 +11:00
|
|
|
|
|
|
|
return tap.status ();
|
2018-03-14 16:39:10 +11:00
|
|
|
}
|