diff --git a/.github/ci-bionic/after_make.sh b/.github/ci-bionic/after_make.sh index 5ea56eb87..d1ec8cd7c 100644 --- a/.github/ci-bionic/after_make.sh +++ b/.github/ci-bionic/after_make.sh @@ -11,3 +11,6 @@ mkdir build cd build cmake .. make + +# return to the compilation place +cd ../../build diff --git a/CMakeLists.txt b/CMakeLists.txt index 62e222b6f..1c1ec8cb0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,7 @@ message(STATUS "\n\n-- ====== Finding Dependencies ======") #-------------------------------------- # Find ignition-math -ign_find_package(ignition-math6 REQUIRED_BY graphics events) +ign_find_package(ignition-math6 REQUIRED_BY graphics events VERSION 6.6) set(IGN_MATH_VER ${ignition-math6_VERSION_MAJOR}) #-------------------------------------- diff --git a/include/ignition/common/Time.hh b/include/ignition/common/Time.hh index 9c36f293d..0ea57300c 100644 --- a/include/ignition/common/Time.hh +++ b/include/ignition/common/Time.hh @@ -52,31 +52,31 @@ namespace ignition }; /// \brief Constructors - public: Time(); + public: IGN_DEPRECATED(4) Time(); /// \brief Copy constructor /// \param[in] time Time to copy - public: Time(const Time &_time); + public: IGN_DEPRECATED(4) Time(const Time &_time); /// \brief Constructor /// \param[in] _tv Time to initialize to - public: explicit Time(const struct timespec &_tv); + public: explicit IGN_DEPRECATED(4) Time(const struct timespec &_tv); /// \brief Constructor /// \param[in] _sec Seconds /// \param[in] _nsec Nanoseconds - public: Time(int32_t _sec, int32_t _nsec); + public: IGN_DEPRECATED(4) Time(int32_t _sec, int32_t _nsec); /// \brief Constuctor /// \param[in] _time Time in double format sec.nsec - public: explicit Time(double _time); + public: explicit IGN_DEPRECATED(4) Time(double _time); /// \brief Destructor public: virtual ~Time(); /// \brief Get the wall time /// \return the current time - public: static const Time &SystemTime(); + public: static const IGN_DEPRECATED(4) Time &SystemTime(); /// \brief Set to sec and nsec /// \param[in] _sec Seconds @@ -103,7 +103,7 @@ namespace ignition /// actual call to the system's sleep function. /// /// On Windows the return value is always common::Time::Zero. - public: static Time Sleep(const common::Time &_time); + public: static Time IGN_DEPRECATED(4) Sleep(const common::Time &_time); /// \brief Get the time as a string formatted as "DD hh:mm:ss.mmm", with /// the option to choose the start/end. diff --git a/include/ignition/common/Timer.hh b/include/ignition/common/Timer.hh index 6e10e66e6..706bcc6c2 100644 --- a/include/ignition/common/Timer.hh +++ b/include/ignition/common/Timer.hh @@ -46,21 +46,27 @@ namespace ignition /// \brief Get the elapsed time /// \return The time - public: Time Elapsed() const; + public: Time IGN_DEPRECATED(4) Elapsed() const; + + /// \brief Get the elapsed time + /// \return The elapsed time + public: std::chrono::duration ElapsedTime() const; /// \brief Stream operator friendly public: friend std::ostream &operator<<(std::ostream &out, const ignition::common::Timer &t) { - out << t.Elapsed(); + out << t.ElapsedTime().count(); return out; } + IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING /// \brief The time of the last call to Start - private: Time start; + private: std::chrono::steady_clock::time_point start; /// \brief The time when Stop was called. - private: Time stop; + private: std::chrono::steady_clock::time_point stop; + IGN_COMMON_WARN_RESUME__DLL_INTERFACE_MISSING /// \brief True if the timer is running. private: bool running; diff --git a/include/ignition/common/WorkerPool.hh b/include/ignition/common/WorkerPool.hh index 64d5d14ac..94da2b5a7 100644 --- a/include/ignition/common/WorkerPool.hh +++ b/include/ignition/common/WorkerPool.hh @@ -59,7 +59,16 @@ namespace ignition /// \returns true if all work was finished /// \remarks The return value can be false even when waiting forever if // the WorkerPool is destructed before all work is completed - public: bool WaitForResults(const Time &_timeout = Time::Zero); + public: bool IGN_DEPRECATED(4) WaitForResults(const Time &_timeout); + + /// \brief Waits until all work is done and threads are idle + /// \param[in] _timeout How long to wait, default to forever + /// \returns true if all work was finished + /// \remarks The return value can be false even when waiting forever if + // the WorkerPool is destructed before all work is completed + public: bool WaitForResults( + const std::chrono::steady_clock::duration &_timeout = + std::chrono::steady_clock::duration::zero()); IGN_COMMON_WARN_IGNORE__DLL_INTERFACE_MISSING /// \brief private implementation pointer diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 393e9328f..a16191b38 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,11 @@ ign_create_core_library( target_link_libraries(${PROJECT_LIBRARY_TARGET_NAME} PRIVATE ${DL_TARGET}) +# This is required by the WorkerPool::WaitForResults(const Time &_timeout) +# TODO(anyone): IGN_DEPRECATED(4). Remove this part when the method is removed +target_include_directories(${PROJECT_LIBRARY_TARGET_NAME} PRIVATE + ${ignition-math${IGN_MATH_VER}_INCLUDE_DIRS}) + # Handle non-Windows configuration settings if(NOT WIN32) diff --git a/src/Time.cc b/src/Time.cc index 0c0f581d8..21636746c 100644 --- a/src/Time.cc +++ b/src/Time.cc @@ -59,10 +59,25 @@ using namespace ignition; using namespace common; +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif Time Time::wallTime; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif struct timespec Time::clockResolution; +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif const Time Time::Zero = common::Time(0, 0); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif + const int32_t Time::nsInSec = 1000000000L; const int32_t Time::nsInMs = 1000000; @@ -99,13 +114,19 @@ Time::Time(const Time &_time) : sec(_time.sec), nsec(_time.nsec) { } - +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif ///////////////////////////////////////////////// Time::Time(int32_t _sec, int32_t _nsec) : sec(_sec), nsec(_nsec) { this->Correct(); } +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif ///////////////////////////////////////////////// Time::Time(double _time) @@ -162,7 +183,14 @@ float Time::Float() const ///////////////////////////////////////////////// Time Time::Sleep(const common::Time &_time) { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif Time result; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif if (_time >= clockResolution) { @@ -174,7 +202,14 @@ Time Time::Sleep(const common::Time &_time) if (interval.tv_sec < 0) { ignerr << "Cannot sleep for negative time[" << _time << "]\n"; - return result; +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif + return result; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } // This assert conforms to the manpage for nanosleep @@ -182,7 +217,14 @@ Time Time::Sleep(const common::Time &_time) { ignerr << "Nanoseconds of [" << interval.tv_nsec << "] must be in the range0 to 999999999.\n"; +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return result; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } #ifdef _WIN32 @@ -197,20 +239,41 @@ Time Time::Sleep(const common::Time &_time) if (timer == NULL) { ignerr << "Unable to create waitable timer. Sleep will be incorrect.\n"; +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return result; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } if (!SetWaitableTimer (timer, &sleepTime, 0, NULL, NULL, 0)) { ignerr << "Unable to use waitable timer. Sleep will be incorrect.\n"; +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return result; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } if (WaitForSingleObject (timer, INFINITE) != WAIT_OBJECT_0) { ignerr << "Unable to wait for a single object. Sleep will be incorrect.\n"; +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return result; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } result.sec = 0; @@ -241,7 +304,14 @@ Time Time::Sleep(const common::Time &_time) ignlog << "Sleep time is larger than clock resolution, skipping sleep\n"; } +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return result; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// @@ -256,10 +326,24 @@ Time &Time::operator=(const Time &_time) ///////////////////////////////////////////////// Time Time::operator+(const Time &_time) const { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif Time t(this->sec + _time.sec, this->nsec + _time.nsec); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif t.Correct(); +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return t; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// @@ -274,9 +358,16 @@ const Time &Time::operator+=(const Time &_time) ///////////////////////////////////////////////// Time Time::operator-(const Time &_time) const { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif Time t(this->sec - _time.sec, this->nsec - _time.nsec); t.Correct(); return t; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// @@ -291,9 +382,16 @@ const Time &Time::operator-=(const Time &_time) ///////////////////////////////////////////////// Time Time::operator*(const Time &_time) const { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif Time t(this->sec * _time.sec, this->nsec * _time.nsec); t.Correct(); return t; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// @@ -307,14 +405,26 @@ const Time &Time::operator*=(const Time &_time) ///////////////////////////////////////////////// Time Time::operator/(const Time &_time) const { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif Time result(*this); - +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif if (_time.sec == 0 && _time.nsec == 0) ignerr << "Time divide by zero\n"; else result.Set(this->Double() / _time.Double()); - +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return result; +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// @@ -333,7 +443,14 @@ bool Time::operator==(const Time &_time) const ///////////////////////////////////////////////// bool Time::operator==(double _time) const { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return *this == Time(_time); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// @@ -358,7 +475,14 @@ bool Time::operator<(const Time &_time) const ///////////////////////////////////////////////// bool Time::operator<(double _time) const { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return *this < Time(_time); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// @@ -370,7 +494,14 @@ bool Time::operator<=(const Time &_time) const ///////////////////////////////////////////////// bool Time::operator<=(double _time) const { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return *this <= Time(_time); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// @@ -382,7 +513,14 @@ bool Time::operator>(const Time &_time) const ///////////////////////////////////////////////// bool Time::operator>(double _time) const { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return *this > Time(_time); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// @@ -394,13 +532,27 @@ bool Time::operator>=(const Time &_time) const ///////////////////////////////////////////////// bool Time::operator>=(double _time) const { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return *this >= Time(_time); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// bool Time::operator>=(const struct timespec &_tv) const { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif return *this >= Time(_tv); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// diff --git a/src/Time_TEST.cc b/src/Time_TEST.cc index 98e896400..aa7ca404c 100644 --- a/src/Time_TEST.cc +++ b/src/Time_TEST.cc @@ -28,8 +28,12 @@ TEST(TimeTest, Time) common::Timer timer; timer.Start(); IGN_SLEEP_MS(100); - EXPECT_TRUE(timer.Elapsed() > common::Time(0, 100000000)); + EXPECT_TRUE(timer.ElapsedTime().count() > 0.1); +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif common::Time time; time = common::Time::SystemTime(); EXPECT_TRUE(common::Time::SystemTime() - time < common::Time(0, 1000000)); @@ -58,6 +62,9 @@ TEST(TimeTest, Time) EXPECT_FALSE(time != common::Time(1, 1999)); time += common::Time(0, 1); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif EXPECT_EQ(time, 1.0 + 2000*1e-9); EXPECT_FALSE(time != 1.0 + 2000*1e-9); @@ -66,12 +73,26 @@ TEST(TimeTest, Time) EXPECT_TRUE(time >= 0.1); time.Set(1, 1000); +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif time = common::Time(1, 1000) * common::Time(2, 2); EXPECT_TRUE(time == common::Time(2, 2000)); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif time.Set(1, 1000); +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif time = common::Time(1, 1000) / common::Time(2, 2); EXPECT_TRUE(time == common::Time(0, 500000499)); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif double sec = 1.0 + 1e-9; double msec = sec * 1e3; @@ -86,6 +107,10 @@ TEST(TimeTest, Time) ///////////////////////////////////////////////// TEST(TimeTest, String) { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif common::Time time(0); // Several combinations @@ -173,16 +198,29 @@ TEST(TimeTest, String) EXPECT_EQ(time.FormattedString(common::Time::FormatOption::MINUTES, common::Time::FormatOption::MINUTES), "4320"); - +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif // Large time time = common::Time(1234567890, 123456789); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif EXPECT_EQ(time.FormattedString(), "14288 23:31:30.123"); } ///////////////////////////////////////////////// TEST(TimeTest, Double) { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif common::Time time(1, 900000000); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif EXPECT_DOUBLE_EQ(1.9, time.Double()); time.Set(1, -900000000); @@ -198,7 +236,14 @@ TEST(TimeTest, Double) ///////////////////////////////////////////////// TEST(TimeTest, Float) { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif common::Time time(1, 900000000); +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif EXPECT_FLOAT_EQ(1.9f, time.Float()); time.Set(1, -900000000); @@ -214,6 +259,10 @@ TEST(TimeTest, Float) ///////////////////////////////////////////////// TEST(TimeTest, Sleep) { +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif common::Time sleepTime(1, 900000000); common::Time result = common::Time::Sleep(sleepTime); #ifdef _WIN32 @@ -222,6 +271,10 @@ TEST(TimeTest, Sleep) #else EXPECT_EQ(sleepTime, result); #endif + +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif } ///////////////////////////////////////////////// diff --git a/src/Timer.cc b/src/Timer.cc index ae6b8c05d..0a8bb5f2a 100644 --- a/src/Timer.cc +++ b/src/Timer.cc @@ -34,14 +34,14 @@ Timer::~Timer() ////////////////////////////////////////////////// void Timer::Start() { - this->start = Time::SystemTime(); + this->start = std::chrono::steady_clock::now(); this->running = true; } ////////////////////////////////////////////////// void Timer::Stop() { - this->stop = Time::SystemTime(); + this->stop = std::chrono::steady_clock::now(); this->running = false; } @@ -51,16 +51,32 @@ bool Timer::Running() const return this->running; } +#ifndef _WIN32 +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif ////////////////////////////////////////////////// Time Timer::Elapsed() const +{ + return common::Time(this->ElapsedTime().count()); +} +#ifndef _WIN32 +# pragma GCC diagnostic pop +#endif + +////////////////////////////////////////////////// +std::chrono::duration Timer::ElapsedTime() const { if (this->running) { - Time currentTime; - currentTime = Time::SystemTime(); - - return currentTime - this->start; + std::chrono::steady_clock::time_point currentTime; + currentTime = std::chrono::steady_clock::now(); + std::chrono::duration diff = currentTime - this->start; + return diff; } else - return this->stop - this->start; + { + std::chrono::duration diff = this->stop - this->start; + return diff; + } } diff --git a/src/WorkerPool.cc b/src/WorkerPool.cc index ccfbea2b3..8390faa92 100644 --- a/src/WorkerPool.cc +++ b/src/WorkerPool.cc @@ -21,6 +21,7 @@ #include #include "ignition/common/WorkerPool.hh" +#include "ignition/math/Helpers.hh" namespace igncmn = ignition::common; using namespace igncmn; @@ -165,6 +166,13 @@ void WorkerPool::AddWork(std::function _work, std::function _cb) ////////////////////////////////////////////////// bool WorkerPool::WaitForResults(const Time &_timeout) +{ + return WaitForResults(math::secNsecToDuration(_timeout.sec, _timeout.nsec)); +} + +////////////////////////////////////////////////// +bool WorkerPool::WaitForResults( + const std::chrono::steady_clock::duration &_timeout) { bool signaled = true; std::unique_lock queueLock(this->dataPtr->queueMtx); @@ -178,7 +186,7 @@ bool WorkerPool::WaitForResults(const Time &_timeout) if (!haveResults()) { - if (Time::Zero == _timeout) + if (std::chrono::steady_clock::duration::zero() == _timeout) { // Wait forever this->dataPtr->signalWorkDone.wait(queueLock); @@ -187,8 +195,7 @@ bool WorkerPool::WaitForResults(const Time &_timeout) { // Wait for timeout signaled = this->dataPtr->signalWorkDone.wait_for(queueLock, - std::chrono::seconds(_timeout.sec) + - std::chrono::nanoseconds(_timeout.nsec), + _timeout, haveResults); } } diff --git a/src/WorkerPool_TEST.cc b/src/WorkerPool_TEST.cc index 19d4dc43a..015e686f2 100644 --- a/src/WorkerPool_TEST.cc +++ b/src/WorkerPool_TEST.cc @@ -92,8 +92,7 @@ TEST(WorkerPool, WaitWithTimeout) { workSentinel = 5; }); - Time time(5); - EXPECT_TRUE(pool.WaitForResults(time)); + EXPECT_TRUE(pool.WaitForResults(std::chrono::seconds(5))); EXPECT_EQ(5, workSentinel); } @@ -105,8 +104,7 @@ TEST(WorkerPool, WaitWithTimeoutThatTimesOut) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); }); - Time time(0.0001); - EXPECT_FALSE(pool.WaitForResults(time)); + EXPECT_FALSE(pool.WaitForResults(std::chrono::nanoseconds(100000))); } ////////////////////////////////////////////////// @@ -133,8 +131,7 @@ TEST(WorkerPool, ThingsRunInParallel) std::this_thread::sleep_for(std::chrono::milliseconds(5)); ++sentinel; }); - Time time(0.009); - bool result = pool.WaitForResults(time); + bool result = pool.WaitForResults(std::chrono::milliseconds(9)); #ifdef __linux__ // the timing test is flaky on windows and mac EXPECT_TRUE(result);