<p>Tristan Hsieh has uploaded this change for <strong>review</strong>.</p><p><a href="https://review.coreboot.org/29315">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">include/timer.h: Add wait_cond_timeout_usecs macro<br><br>Add wait_cond_timeout_usecs and wait_cond_timeout_msecs to wait until a<br>condition gets true or a timeout elapses.<br><br>BUG=b:80501386<br>BRANCH=none<br>TEST=Built and booted on Kukui<br><br>Change-Id: I6de38ee00673c46332ae92b8a11099485de5327a<br>Signed-off-by: Tristan Shieh <tristan.shieh@mediatek.com><br>---<br>M src/include/timer.h<br>1 file changed, 43 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://review.coreboot.org:29418/coreboot refs/changes/15/29315/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/include/timer.h b/src/include/timer.h</span><br><span>index 49eae5e..a5ee72b 100644</span><br><span>--- a/src/include/timer.h</span><br><span>+++ b/src/include/timer.h</span><br><span>@@ -195,4 +195,47 @@</span><br><span>         return stopwatch_duration_usecs(sw) / USECS_PER_MSEC;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Return remaining time.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static inline long stopwatch_remain_usecs(struct stopwatch *sw)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        return mono_time_diff_microseconds(&sw->current, &sw->expires);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static inline long stopwatch_remain_msecs(struct stopwatch *sw)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        return stopwatch_remain_usecs(sw) / USECS_PER_MSEC;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Wait until a condition gets true or a timeout elapses.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * condition: a C expression to wait for</span><br><span style="color: hsl(120, 100%, 40%);">+ * timeout: timeout, in microseconds</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Returns:</span><br><span style="color: hsl(120, 100%, 40%);">+ * 0 if the condition evaluated to false after the timeout elapsed,</span><br><span style="color: hsl(120, 100%, 40%);">+ * 1 if the condition evaluated to true after the timeout elapsed,</span><br><span style="color: hsl(120, 100%, 40%);">+ * or the remaining microseconds (at least 1) if the condition evaluated</span><br><span style="color: hsl(120, 100%, 40%);">+ * to true before the timeout elapsed.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#define wait_cond_usecs(condition, timeout)                       \</span><br><span style="color: hsl(120, 100%, 40%);">+({                                                           \</span><br><span style="color: hsl(120, 100%, 40%);">+     long __ret = 0;                                         \</span><br><span style="color: hsl(120, 100%, 40%);">+     struct stopwatch __sw;                                  \</span><br><span style="color: hsl(120, 100%, 40%);">+     stopwatch_init_usecs_expire(&__sw, timeout);                \</span><br><span style="color: hsl(120, 100%, 40%);">+     do {                                                    \</span><br><span style="color: hsl(120, 100%, 40%);">+             if (condition) {                                \</span><br><span style="color: hsl(120, 100%, 40%);">+                     __ret = stopwatch_expired(&__sw) ? 1 :      \</span><br><span style="color: hsl(120, 100%, 40%);">+                             stopwatch_remain_usecs(&__sw);      \</span><br><span style="color: hsl(120, 100%, 40%);">+                     break;                                  \</span><br><span style="color: hsl(120, 100%, 40%);">+             }                                               \</span><br><span style="color: hsl(120, 100%, 40%);">+     } while (!stopwatch_expired(&__sw));                        \</span><br><span style="color: hsl(120, 100%, 40%);">+     __ret;                                                  \</span><br><span style="color: hsl(120, 100%, 40%);">+})</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define wait_cond_msecs(condition, timeout)                    \</span><br><span style="color: hsl(120, 100%, 40%);">+     wait_cond_usecs(condition, (timeout) * USECS_PER_MSEC)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #endif /* TIMER_H */</span><br><span></span><br></pre><p>To view, visit <a href="https://review.coreboot.org/29315">change 29315</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://review.coreboot.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://review.coreboot.org/29315"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: coreboot </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I6de38ee00673c46332ae92b8a11099485de5327a </div>
<div style="display:none"> Gerrit-Change-Number: 29315 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Tristan Hsieh <tristan.shieh@mediatek.com> </div>