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 if clock_nanosleep()
      9  * parameter rqtp specifies a time in the past, clock_nanosleep()
     10  * returns immediately and no suspension occurs.
     11  *
     12  * - Get the current time.
     13  * - Set clock_nanosleep() to sleep until current time - SLEEPSEC
     14  *   seconds.
     15  * - Get the current time after sleeping and ensure it is within
     16  *   ACCEPTABLEDELTA of current time (i.e., didn't sleep)
     17  */
     18 #include <stdio.h>
     19 #include <time.h>
     20 #include "posixtest.h"
     21 
     22 #define SLEEPSEC 3
     23 #define ACCEPTABLEDELTA 1
     24 
     25 int main(void)
     26 {
     27 	struct timespec tssleep, tsbefore, tsafter;
     28 	int sleepuntilsec, flags = 0;
     29 
     30 	if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
     31 		perror("clock_gettime() did not return success\n");
     32 		return PTS_UNRESOLVED;
     33 	}
     34 
     35 	sleepuntilsec = tsbefore.tv_sec - SLEEPSEC;
     36 	tssleep.tv_sec = sleepuntilsec;
     37 	tssleep.tv_nsec = tsbefore.tv_nsec;
     38 
     39 	flags |= TIMER_ABSTIME;
     40 	if (clock_nanosleep(CLOCK_REALTIME, flags, &tssleep, NULL) != 0) {
     41 		printf("clock_nanosleep() did not return success\n");
     42 		return PTS_UNRESOLVED;
     43 	}
     44 
     45 	if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
     46 		perror("Error in clock_gettime()\n");
     47 		return PTS_UNRESOLVED;
     48 	}
     49 
     50 	if (tsafter.tv_sec >= tsbefore.tv_sec) {
     51 		if (tsafter.tv_sec <= (tsbefore.tv_sec + ACCEPTABLEDELTA)) {
     52 			printf("Test PASSED\n");
     53 			return PTS_PASS;
     54 		} else {
     55 			printf("FAIL:  clock_nanosleep() slept\n");
     56 			return PTS_FAIL;
     57 		}
     58 	}
     59 
     60 	printf("FAIL:  error in clock_gettime()\n");
     61 	return PTS_FAIL;
     62 }
     63