[PATCH] tests: use nanosleep instead of usleep

Marek Chalupa mchqwerty at gmail.com
Tue Sep 9 00:48:40 PDT 2014


man usleep says that bahaviour of using usleep with SIGALRM signal
is unspecified. So use nanosleep instead.

Signed-off-by: Marek Chalupa <mchqwerty at gmail.com>
---
 tests/display-test.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/tests/display-test.c b/tests/display-test.c
index 1289866..4744945 100644
--- a/tests/display-test.c
+++ b/tests/display-test.c
@@ -333,12 +333,18 @@ register_reading(struct wl_display *display)
 	assert(wl_display_flush(display) >= 0);
 }
 
+#define USEC_TO_NSEC(n) (1000 * (n))
+
 /* create thread that will call prepare+read so that
  * it will block */
 static pthread_t
 create_thread(struct client *c, void *(*func)(void*))
 {
 	pthread_t thread;
+	struct timespec ts = {
+		.tv_sec = 0,
+		.tv_nsec = USEC_TO_NSEC(500)
+	};
 
 	c->display_stopped = 0;
 	/* func must set display->stopped to 1 before sleeping */
@@ -346,11 +352,13 @@ create_thread(struct client *c, void *(*func)(void*))
 
 	/* make sure the thread is sleeping. It's a little bit racy
 	 * (setting display_stopped to 1 and calling wl_display_read_events)
-	 * so call usleep once again after the loop ends - it should
+	 * so call nanosleep once again after the loop ends - it should
 	 * be sufficient... */
 	while (c->display_stopped == 0)
-		usleep(500);
-	usleep(10000);
+		assert(nanosleep(&ts, NULL) == 0);
+
+	ts.tv_nsec = USEC_TO_NSEC(10000);
+	assert(nanosleep(&ts, NULL) == 0);
 
 	return thread;
 }
@@ -502,6 +510,10 @@ threading_read_after_error(void)
 {
 	struct client *c = client_connect();
 	pthread_t thread;
+	struct timespec ts = {
+		.tv_sec = 0,
+		.tv_nsec = USEC_TO_NSEC(500)
+	};
 
 	/* create an error */
 	close(wl_display_get_fd(c->wl_display));
@@ -517,8 +529,10 @@ threading_read_after_error(void)
 
 	/* make sure thread is sleeping */
 	while (c->display_stopped == 0)
-		usleep(500);
-	usleep(10000);
+		assert(nanosleep(&ts, NULL) == 0);
+
+	ts.tv_nsec = USEC_TO_NSEC(10000);
+	assert(nanosleep(&ts, NULL) == 0);
 
 	assert(wl_display_read_events(c->wl_display) == -1);
 
-- 
1.9.3



More information about the wayland-devel mailing list