-
Notifications
You must be signed in to change notification settings - Fork 221
Usleep #1597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Usleep #1597
Changes from all commits
9ea56ee
098bed3
f8eb596
889137b
d1f6b5f
baa4005
bc04627
25f5425
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,8 +40,10 @@ | |
|
|
||
| namespace ola { | ||
|
|
||
| static const int USEC_IN_SECONDS = 1000000; | ||
| static const int ONE_THOUSAND = 1000; | ||
| static const int MSECS_IN_SECOND = ONE_THOUSAND; | ||
| static const int USECS_IN_SECOND = MSECS_IN_SECOND * ONE_THOUSAND; | ||
| static const uint64_t NSECS_IN_SECOND = USECS_IN_SECOND * ONE_THOUSAND; | ||
|
|
||
| /** | ||
| * Don't use this class directly. It's an implementation detail of TimeInterval | ||
|
|
@@ -90,20 +92,26 @@ class BaseTimeVal { | |
| * @brief Returns the microseconds portion of the BaseTimeVal | ||
| * @return The microseconds portion of the BaseTimeVal | ||
| */ | ||
| int32_t MicroSeconds() const { return static_cast<int32_t>(m_tv.tv_usec); } | ||
| int32_t Microseconds() const { return static_cast<int32_t>(m_tv.tv_usec); } | ||
|
|
||
| /** | ||
| * @brief Returns the entire BaseTimeVal as milliseconds | ||
| * @return The entire BaseTimeVal in milliseconds | ||
| */ | ||
| int64_t InMilliSeconds() const; | ||
| int64_t InMilliseconds() const; | ||
|
|
||
| /** | ||
| * @brief Returns the entire BaseTimeVal as microseconds | ||
| * @return The entire BaseTimeVal in microseconds | ||
| */ | ||
| int64_t AsInt() const; | ||
|
|
||
| /** | ||
| * @brief InMicroseconds wrapper for AsInt() for name consistency. | ||
| * @return The entire BaseTimeVal in microseconds | ||
| */ | ||
| int64_t InMicroseconds() const { return this->AsInt(); } | ||
|
|
||
| std::string ToString() const; | ||
|
|
||
| private: | ||
|
|
@@ -157,9 +165,10 @@ class TimeInterval { | |
| void AsTimeval(struct timeval *tv) const { m_interval.AsTimeval(tv); } | ||
|
|
||
| time_t Seconds() const { return m_interval.Seconds(); } | ||
| int32_t MicroSeconds() const { return m_interval.MicroSeconds(); } | ||
| int32_t Microseconds() const { return m_interval.Microseconds(); } | ||
|
|
||
| int64_t InMilliSeconds() const { return m_interval.InMilliSeconds(); } | ||
| int64_t InMilliseconds() const { return m_interval.InMilliseconds(); } | ||
| int64_t InMicroseconds() const { return m_interval.InMicroseconds(); } | ||
| int64_t AsInt() const { return m_interval.AsInt(); } | ||
|
|
||
| std::string ToString() const { return m_interval.ToString(); } | ||
|
|
@@ -211,7 +220,7 @@ class TimeStamp { | |
| bool IsSet() const { return m_tv.IsSet(); } | ||
|
|
||
| time_t Seconds() const { return m_tv.Seconds(); } | ||
| int32_t MicroSeconds() const { return m_tv.MicroSeconds(); } | ||
| int32_t Microseconds() const { return m_tv.Microseconds(); } | ||
|
|
||
| std::string ToString() const { return m_tv.ToString(); } | ||
|
|
||
|
|
@@ -257,5 +266,33 @@ class MockClock: public Clock { | |
| private: | ||
| TimeInterval m_offset; | ||
| }; | ||
|
|
||
| enum TimerGranularity { UNKNOWN, GOOD, BAD }; | ||
|
|
||
| /** | ||
| * @brief The Sleep class implements usleep with some granulatiry detection. | ||
| */ | ||
| class Sleep { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should probably be called GranularSleep or something should we ever have to do something like wrap/override usleep or similar.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole point of this class is to do usleep without the various test systems screaming at us that usleep shouldn't be used. |
||
| public: | ||
| explicit Sleep(std::string caller); | ||
|
|
||
| void SetCaller(std::string caller) { m_caller = caller; } | ||
|
|
||
| void Usleep(TimeInterval requested); | ||
| void Usleep(uint32_t requested); | ||
| void Usleep(timespec requested); | ||
|
|
||
| TimerGranularity GetGranularity() { return m_granularity; } | ||
| bool CheckTimeGranularity(uint64_t wanted, uint64_t max_deviation); | ||
| private: | ||
| std::string m_caller; | ||
| uint64_t m_wanted_granularity; | ||
| uint64_t m_max_granularity_deviation; | ||
| uint64_t m_clock_overhead; | ||
|
|
||
| static const uint32_t BAD_GRANULARITY_LIMIT = 10; | ||
|
|
||
| TimerGranularity m_granularity = UNKNOWN; | ||
| }; | ||
| } // namespace ola | ||
| #endif // INCLUDE_OLA_CLOCK_H_ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be using the existing maths in TimeInterval rather than rewriting that code here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I follow, we have a TimeIntervalToTimeSpec function somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(It does make sense to add such a function but I did not see any such thing listed in the header)