Home | History | Annotate | Download | only in 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 nanosleep() causes the current thread to be suspended
      9  * until the time interval in rqtp passes.
     10  */
     11 #include <stdio.h>
     12 #include <time.h>
     13 #include "posixtest.h"
     14 
     15 int main(void)
     16 {
     17 	struct timespec tssleepfor, tsstorage, tsbefore, tsafter;
     18 	int sleepnsec = 3;
     19 	int slepts = 0, sleptns = 0;
     20 
     21 	if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
     22 		perror("Error in clock_gettime()\n");
     23 		return PTS_UNRESOLVED;
     24 	}
     25 
     26 	tssleepfor.tv_sec = 0;
     27 	tssleepfor.tv_nsec = sleepnsec;
     28 	if (nanosleep(&tssleepfor, &tsstorage) != 0) {
     29 		printf("nanosleep() did not return success\n");
     30 		return PTS_UNRESOLVED;
     31 	}
     32 
     33 	if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
     34 		perror("Error in clock_gettime()\n");
     35 		return PTS_UNRESOLVED;
     36 	}
     37 
     38 	/*
     39 	 * Generic alg for calculating slept time.
     40 	 */
     41 	slepts = tsafter.tv_sec - tsbefore.tv_sec;
     42 	sleptns = tsafter.tv_nsec - tsbefore.tv_nsec;
     43 	if (sleptns < 0) {
     44 		sleptns = sleptns + 1000000000;
     45 		slepts = slepts - 1;
     46 	}
     47 
     48 	if ((slepts > 0) || (sleptns > sleepnsec)) {
     49 		printf("Test PASSED\n");
     50 		return PTS_PASS;
     51 	}
     52 
     53 	printf("nanosleep() did not sleep long enough\n");
     54 	return PTS_FAIL;
     55 }
     56