| 1 | #include <pthread.h>
|
|---|
| 2 | #include <boost/chrono.hpp>
|
|---|
| 3 | #include <boost/thread.hpp>
|
|---|
| 4 | #include "condition_variable_steady.hpp"
|
|---|
| 5 |
|
|---|
| 6 | #include <boost/program_options.hpp>
|
|---|
| 7 | #include <iostream>
|
|---|
| 8 | #include <string>
|
|---|
| 9 | #include <stdint.h>
|
|---|
| 10 |
|
|---|
| 11 | #define WAIT_UNTIL 1
|
|---|
| 12 |
|
|---|
| 13 | using namespace std;
|
|---|
| 14 | using namespace boost;
|
|---|
| 15 | namespace ch = boost::chrono;
|
|---|
| 16 | namespace po = boost::program_options;
|
|---|
| 17 |
|
|---|
| 18 | int main(int argc, char ** argv)
|
|---|
| 19 | {
|
|---|
| 20 | string type;
|
|---|
| 21 | uint32_t sleepTime;
|
|---|
| 22 |
|
|---|
| 23 | po::options_description desc("Arguments");
|
|---|
| 24 | desc.add_options()
|
|---|
| 25 | ("help", "Help message")
|
|---|
| 26 | ("type", po::value<string>(&type)->default_value("steady"),
|
|---|
| 27 | "Type of test: steady, until (wait_until), for (wait_for) or sleep (sleep_until)")
|
|---|
| 28 | ("time", po::value<uint32_t>(&sleepTime)->default_value(10), "Sleep time (sec)");
|
|---|
| 29 |
|
|---|
| 30 | try {
|
|---|
| 31 | po::variables_map vm;
|
|---|
| 32 | po::store(po::parse_command_line(argc, argv, desc), vm);
|
|---|
| 33 | po::notify(vm);
|
|---|
| 34 | if (vm.count("help"))
|
|---|
| 35 | throw po::error("");
|
|---|
| 36 | } catch(const std::exception&) {
|
|---|
| 37 | cout << desc << endl;
|
|---|
| 38 | return 1;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | ch::steady_clock::time_point startSteady, endSteady;
|
|---|
| 42 | ch::system_clock::time_point startSystem, endSystem;
|
|---|
| 43 |
|
|---|
| 44 | startSteady = ch::steady_clock::now();
|
|---|
| 45 | startSystem = ch::system_clock::now();
|
|---|
| 46 |
|
|---|
| 47 | boost::mutex signalLock;
|
|---|
| 48 | boost::condition_variable signal; // Signal for Timer Queue Processing thread
|
|---|
| 49 | boost::condition_variable_steady steady_signal;
|
|---|
| 50 |
|
|---|
| 51 | cout << "Waiting for " << sleepTime << " sec" << endl;
|
|---|
| 52 |
|
|---|
| 53 | if (type == "sleep") {
|
|---|
| 54 | cout << "SLEEP_UNTIL" << endl;
|
|---|
| 55 | boost::this_thread::sleep_until(startSteady + ch::seconds(sleepTime));
|
|---|
| 56 | } else if (type == "steady") {
|
|---|
| 57 | cout << "STEADY_WAIT" << endl;
|
|---|
| 58 | boost::unique_lock<boost::mutex> lock(signalLock);
|
|---|
| 59 | ch::time_point<ch::steady_clock> wait_time = startSteady + ch::seconds(sleepTime);
|
|---|
| 60 | steady_signal.wait_until(lock, wait_time);
|
|---|
| 61 | } else if (type == "until") {
|
|---|
| 62 | cout << "WAIT_UNTIL" << endl;
|
|---|
| 63 | boost::unique_lock<boost::mutex> lock(signalLock);
|
|---|
| 64 | ch::time_point<ch::steady_clock> wait_time = startSteady + ch::seconds(sleepTime);
|
|---|
| 65 | signal.wait_until(lock, wait_time);
|
|---|
| 66 | } else if (type == "for") {
|
|---|
| 67 | cout << "WAIT_FOR" << endl;
|
|---|
| 68 | boost::unique_lock<boost::mutex> lock(signalLock);
|
|---|
| 69 | signal.wait_for(lock, ch::seconds(sleepTime));
|
|---|
| 70 | } else {
|
|---|
| 71 | cout << "Unexpected test type: '" << type << "'" << endl;
|
|---|
| 72 | return 1;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | endSteady = ch::steady_clock::now();
|
|---|
| 76 | endSystem = ch::system_clock::now();
|
|---|
| 77 |
|
|---|
| 78 | int ms = ch::duration_cast<ch::milliseconds>(endSteady - startSteady).count();
|
|---|
| 79 | cout << "Steady duration/start/stop: " << (float)ms / 1000.0 << endl;
|
|---|
| 80 | cout << " " << startSteady << " / " << endSteady << endl;
|
|---|
| 81 | cout << "System duration/start/stop: " << ch::duration_cast<ch::seconds>(endSystem - startSystem) << endl;
|
|---|
| 82 | cout << " " << startSystem << " / " << endSystem << endl;
|
|---|
| 83 | }
|
|---|