summaryrefslogtreecommitdiff
path: root/lib/igt_aux.h
diff options
context:
space:
mode:
authorPaulo Zanoni <paulo.r.zanoni@intel.com>2014-12-03 16:12:49 -0200
committerPaulo Zanoni <paulo.r.zanoni@intel.com>2015-05-05 17:27:49 -0300
commit9bb04d3aa6a2b1b86e091967e312a19ce337df4d (patch)
tree4bc4ddc0b7f75b5c8156599f36dc08991b223811 /lib/igt_aux.h
parenta734ac2058fd0cce842a4290335c0697d89b2d63 (diff)
lib: add igt_wait()
Just a little helper for code that needs to wait for a certain condition to happen. It has the nice advantage that it can survive the signal helper. Despite the callers added in this patch, there is another that will go in a separate patch, and another in a new IGT test file that I plan to push later. v2: Check COND again before returning in case we hit the timeout. Signed-off-by: Paulo Zanoni <paulo.r.zanoni@intel.com>
Diffstat (limited to 'lib/igt_aux.h')
-rw-r--r--lib/igt_aux.h43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/igt_aux.h b/lib/igt_aux.h
index 7a5078b0..b2dc2679 100644
--- a/lib/igt_aux.h
+++ b/lib/igt_aux.h
@@ -30,6 +30,7 @@
#include <intel_bufmgr.h>
#include <stdbool.h>
+#include <sys/time.h>
extern drm_intel_bo **trash_bos;
extern int num_trash_bos;
@@ -102,4 +103,46 @@ void intel_require_memory(uint32_t count, uint32_t size, unsigned mode);
void igt_lock_mem(size_t size);
void igt_unlock_mem(void);
+/**
+ * igt_wait:
+ * @COND: condition to wait
+ * @timeout_ms: timeout in milliseconds
+ * @interval_ms: amount of time we try to sleep between COND checks
+ *
+ * Waits until COND evaluates to true or the timeout passes.
+ *
+ * It is safe to call this macro if the signal helper is active. The only
+ * problem is that the usleep() calls will return early, making us evaluate COND
+ * too often, possibly eating valuable CPU cycles.
+ *
+ * Returns:
+ * True of COND evaluated to true, false otherwise.
+ */
+#define igt_wait(COND, timeout_ms, interval_ms) ({ \
+ struct timeval start_, end_, diff_; \
+ int elapsed_ms_; \
+ bool ret_ = false; \
+ \
+ igt_assert(gettimeofday(&start_, NULL) == 0); \
+ do { \
+ if (COND) { \
+ ret_ = true; \
+ break; \
+ } \
+ \
+ usleep(interval_ms * 1000); \
+ \
+ igt_assert(gettimeofday(&end_, NULL) == 0); \
+ timersub(&end_, &start_, &diff_); \
+ \
+ elapsed_ms_ = diff_.tv_sec * 1000 + \
+ diff_.tv_usec / 1000; \
+ } while (elapsed_ms_ < timeout_ms); \
+ \
+ if (!ret_ && (COND)) \
+ ret_ = true; \
+ \
+ ret_; \
+})
+
#endif /* IGT_AUX_H */