Home | History | Annotate | Download | only in timer_gettime
      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 timer_gettime() returns a relative amount even on
      9  * absolute timers.
     10  * - Create and arm an absolute timer.
     11  * - Call timer_gettime()
     12  * - Ensure the value returned is within ACCEPTABLEDELTA less than
     13  *   the TIMERSEC.  If it is, the values returned were indeed relative.
     14  *
     15  * Signal SIGCONT will be used so that it will not affect the test if
     16  * the timer expires.
     17  * Clock CLOCK_REALTIME will be used.
     18  */
     19 
     20 #include <time.h>
     21 #include <signal.h>
     22 #include <stdio.h>
     23 #include <unistd.h>
     24 #include <stdlib.h>
     25 #include "posixtest.h"
     26 
     27 #define TIMERSEC 2
     28 #define SLEEPDELTA 3
     29 #define ACCEPTABLEDELTA 1
     30 
     31 int main(void)
     32 {
     33 	struct sigevent ev;
     34 	timer_t tid;
     35 	struct itimerspec itsset, itsget;
     36 	struct timespec ts;
     37 	int flags = 0;
     38 	int delta;
     39 
     40 	ev.sigev_notify = SIGEV_SIGNAL;
     41 	ev.sigev_signo = SIGCONT;
     42 
     43 	if (timer_create(CLOCK_REALTIME, &ev, &tid) != 0) {
     44 		perror("timer_create() did not return success\n");
     45 		return PTS_UNRESOLVED;
     46 	}
     47 
     48 	if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
     49 		perror("clock_gettime() did not return success\n");
     50 		return PTS_UNRESOLVED;
     51 	}
     52 
     53 	itsset.it_interval.tv_sec = 0;
     54 	itsset.it_interval.tv_nsec = 0;
     55 	itsset.it_value.tv_sec = ts.tv_sec + TIMERSEC;
     56 	itsset.it_value.tv_nsec = ts.tv_nsec;
     57 
     58 	flags |= TIMER_ABSTIME;
     59 	if (timer_settime(tid, flags, &itsset, NULL) != 0) {
     60 		perror("timer_settime() did not return success\n");
     61 		return PTS_UNRESOLVED;
     62 	}
     63 
     64 	if (timer_gettime(tid, &itsget) != 0) {
     65 		perror("timer_gettime() did not return success\n");
     66 		return PTS_UNRESOLVED;
     67 	}
     68 
     69 	delta = TIMERSEC - itsget.it_value.tv_sec;
     70 
     71 	if (delta < 0) {
     72 		printf("FAIL:  timer_gettime() value > timer_settime()\n");
     73 		printf("%d > %d\n", (int)itsget.it_value.tv_sec,
     74 		       (int)itsset.it_value.tv_sec);
     75 		return PTS_FAIL;
     76 	}
     77 
     78 	if (delta <= ACCEPTABLEDELTA) {
     79 		printf("Test PASSED\n");
     80 		return PTS_PASS;
     81 	}
     82 
     83 	printf("FAIL:  timer_gettime() value !~= timer_settime()\n");
     84 	printf("%d !~= %d\n", (int)itsget.it_value.tv_sec,
     85 	       (int)itsset.it_value.tv_sec);
     86 	return PTS_FAIL;
     87 }
     88