2018-03-14 16:39:10 +11:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
*/
|
|
|
|
|
2018-03-23 14:10:20 +11:00
|
|
|
#include "thread/flag.hpp"
|
|
|
|
#include "thread/ticketlock.hpp"
|
2018-03-14 16:39:10 +11:00
|
|
|
#include "tap.hpp"
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
using ffs_t = std::chrono::high_resolution_clock;
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
2018-03-23 14:10:20 +11:00
|
|
|
fight (util::thread::flag &start, util::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 ()
|
|
|
|
{
|
|
|
|
util::TAP::logger tap;
|
|
|
|
|
2018-03-23 14:10:20 +11:00
|
|
|
util::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");
|
|
|
|
|
2018-03-22 16:10:06 +11:00
|
|
|
if (true || std::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-03-23 14:10:20 +11:00
|
|
|
util::thread::flag start_flag;
|
2018-03-14 16:39:10 +11:00
|
|
|
|
|
|
|
ffs_t::time_point a_finish, b_finish;
|
|
|
|
|
|
|
|
std::thread a (fight, std::ref (start_flag), std::ref (l), iterations, std::ref (a_finish));
|
|
|
|
std::thread b (fight, std::ref (start_flag), std::ref (l), iterations, std::ref (b_finish));
|
|
|
|
|
|
|
|
auto start = std::chrono::high_resolution_clock::now ();
|
|
|
|
start_flag.notify ();
|
|
|
|
|
|
|
|
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 ();
|
|
|
|
|
|
|
|
auto diff = util::max (a_total, b_total) / float (util::min (a_total, b_total));
|
|
|
|
auto rel = util::abs (1 - diff);
|
|
|
|
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
|
|
|
}
|