diff options
Diffstat (limited to 'base/properties.cpp')
-rw-r--r-- | base/properties.cpp | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/base/properties.cpp b/base/properties.cpp index 32c012830..816bca0eb 100644 --- a/base/properties.cpp +++ b/base/properties.cpp | |||
@@ -101,22 +101,24 @@ static void WaitForPropertyCallback(void* data_ptr, const char*, const char* val | |||
101 | } | 101 | } |
102 | 102 | ||
103 | // TODO: chrono_utils? | 103 | // TODO: chrono_utils? |
104 | static void DurationToTimeSpec(timespec& ts, std::chrono::nanoseconds d) { | 104 | static void DurationToTimeSpec(timespec& ts, const std::chrono::milliseconds d) { |
105 | auto s = std::chrono::duration_cast<std::chrono::seconds>(d); | 105 | auto s = std::chrono::duration_cast<std::chrono::seconds>(d); |
106 | auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(d - s); | 106 | auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(d - s); |
107 | ts.tv_sec = s.count(); | 107 | ts.tv_sec = s.count(); |
108 | ts.tv_nsec = ns.count(); | 108 | ts.tv_nsec = ns.count(); |
109 | } | 109 | } |
110 | 110 | ||
111 | // TODO: boot_clock? | ||
111 | using AbsTime = std::chrono::time_point<std::chrono::steady_clock>; | 112 | using AbsTime = std::chrono::time_point<std::chrono::steady_clock>; |
112 | 113 | ||
113 | static void UpdateTimeSpec(timespec& ts, | 114 | static void UpdateTimeSpec(timespec& ts, std::chrono::milliseconds relative_timeout, |
114 | const AbsTime& timeout) { | 115 | const AbsTime& start_time) { |
115 | auto now = std::chrono::steady_clock::now(); | 116 | auto now = std::chrono::steady_clock::now(); |
116 | auto remaining_timeout = std::chrono::duration_cast<std::chrono::nanoseconds>(timeout - now); | 117 | auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time); |
117 | if (remaining_timeout < 0ns) { | 118 | if (time_elapsed >= relative_timeout) { |
118 | ts = { 0, 0 }; | 119 | ts = { 0, 0 }; |
119 | } else { | 120 | } else { |
121 | auto remaining_timeout = relative_timeout - time_elapsed; | ||
120 | DurationToTimeSpec(ts, remaining_timeout); | 122 | DurationToTimeSpec(ts, remaining_timeout); |
121 | } | 123 | } |
122 | } | 124 | } |
@@ -127,11 +129,7 @@ static void UpdateTimeSpec(timespec& ts, | |||
127 | // Returns nullptr on timeout. | 129 | // Returns nullptr on timeout. |
128 | static const prop_info* WaitForPropertyCreation(const std::string& key, | 130 | static const prop_info* WaitForPropertyCreation(const std::string& key, |
129 | const std::chrono::milliseconds& relative_timeout, | 131 | const std::chrono::milliseconds& relative_timeout, |
130 | AbsTime& absolute_timeout) { | 132 | const AbsTime& start_time) { |
131 | // TODO: boot_clock? | ||
132 | auto now = std::chrono::steady_clock::now(); | ||
133 | absolute_timeout = now + relative_timeout; | ||
134 | |||
135 | // Find the property's prop_info*. | 133 | // Find the property's prop_info*. |
136 | const prop_info* pi; | 134 | const prop_info* pi; |
137 | unsigned global_serial = 0; | 135 | unsigned global_serial = 0; |
@@ -139,17 +137,16 @@ static const prop_info* WaitForPropertyCreation(const std::string& key, | |||
139 | // The property doesn't even exist yet. | 137 | // The property doesn't even exist yet. |
140 | // Wait for a global change and then look again. | 138 | // Wait for a global change and then look again. |
141 | timespec ts; | 139 | timespec ts; |
142 | UpdateTimeSpec(ts, absolute_timeout); | 140 | UpdateTimeSpec(ts, relative_timeout, start_time); |
143 | if (!__system_property_wait(nullptr, global_serial, &global_serial, &ts)) return nullptr; | 141 | if (!__system_property_wait(nullptr, global_serial, &global_serial, &ts)) return nullptr; |
144 | } | 142 | } |
145 | return pi; | 143 | return pi; |
146 | } | 144 | } |
147 | 145 | ||
148 | bool WaitForProperty(const std::string& key, | 146 | bool WaitForProperty(const std::string& key, const std::string& expected_value, |
149 | const std::string& expected_value, | ||
150 | std::chrono::milliseconds relative_timeout) { | 147 | std::chrono::milliseconds relative_timeout) { |
151 | AbsTime absolute_timeout; | 148 | auto start_time = std::chrono::steady_clock::now(); |
152 | const prop_info* pi = WaitForPropertyCreation(key, relative_timeout, absolute_timeout); | 149 | const prop_info* pi = WaitForPropertyCreation(key, relative_timeout, start_time); |
153 | if (pi == nullptr) return false; | 150 | if (pi == nullptr) return false; |
154 | 151 | ||
155 | WaitForPropertyData data; | 152 | WaitForPropertyData data; |
@@ -162,7 +159,7 @@ bool WaitForProperty(const std::string& key, | |||
162 | if (data.done) return true; | 159 | if (data.done) return true; |
163 | 160 | ||
164 | // It didn't, so wait for the property to change before checking again. | 161 | // It didn't, so wait for the property to change before checking again. |
165 | UpdateTimeSpec(ts, absolute_timeout); | 162 | UpdateTimeSpec(ts, relative_timeout, start_time); |
166 | uint32_t unused; | 163 | uint32_t unused; |
167 | if (!__system_property_wait(pi, data.last_read_serial, &unused, &ts)) return false; | 164 | if (!__system_property_wait(pi, data.last_read_serial, &unused, &ts)) return false; |
168 | } | 165 | } |
@@ -170,8 +167,8 @@ bool WaitForProperty(const std::string& key, | |||
170 | 167 | ||
171 | bool WaitForPropertyCreation(const std::string& key, | 168 | bool WaitForPropertyCreation(const std::string& key, |
172 | std::chrono::milliseconds relative_timeout) { | 169 | std::chrono::milliseconds relative_timeout) { |
173 | AbsTime absolute_timeout; | 170 | auto start_time = std::chrono::steady_clock::now(); |
174 | return (WaitForPropertyCreation(key, relative_timeout, absolute_timeout) != nullptr); | 171 | return (WaitForPropertyCreation(key, relative_timeout, start_time) != nullptr); |
175 | } | 172 | } |
176 | 173 | ||
177 | } // namespace base | 174 | } // namespace base |