Home | History | Annotate | Download | only in clock_nanosleep
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7 
      8  * Test that if TIMER_ABSTIME is set in flags, then clock_nanosleep()
      9  * sleeps for the absolute time specified in rqtp.
     10  *
     11  * - Get the current time.
     12  * - Set clock_nanosleep() to sleep for current time + SLEEPSEC
     13  *   seconds.
     14  * - Get the current time after sleeping and ensure it is within
     15  *   ACCEPTABLEDELTA of current time + SLEEPSEC
     16  */
     17 #include <stdio.h>
     18 #include <time.h>
     19 #include "posixtest.h"
     20 
     21 #define SLEEPSEC 3
     22 #define ACCEPTABLEDELTA 1
     23 
     24 int main(void)
     25 {
     26 	struct timespec tssleep, tsbefore, tsafter;
     27 	int sleepuntilsec, flags = 0;
     28 
     29 	if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
     30 		perror("clock_gettime() did not return success\n");
     31 		return PTS_UNRESOLVED;
     32 	}
     33 
     34 	sleepuntilsec = tsbefore.tv_sec + SLEEPSEC;
     35 	tssleep.tv_sec = sleepuntilsec;
     36 	tssleep.tv_nsec = tsbefore.tv_nsec;
     37 
     38 	flags |= TIMER_ABSTIME;
     39 	if (clock_nanosleep(CLOCK_REALTIME, flags, &tssleep, NULL) != 0) {
     40 		printf("clock_nanosleep() did not return success\n");
     41 		return PTS_UNRESOLVED;
     42 	}
     43 
     44 	if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
     45 		perror("Error in clock_gettime()\n");
     46 		return PTS_UNRESOLVED;
     47 	}
     48 
     49 	if (tsafter.tv_sec >= sleepuntilsec) {
     50 		if (tsafter.tv_sec <= (sleepuntilsec + ACCEPTABLEDELTA)) {
     51 			printf("Test PASSED\n");
     52 			return PTS_PASS;
     53 		} else {
     54 			printf("clock_nanosleep() slept too long\n");
     55 			return PTS_FAIL;
     56 		}
     57 	}
     58 
     59 	printf("clock_nanosleep() did not sleep long enough\n");
     60 	return PTS_FAIL;
     61 }
     62