1 /* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * This file is licensed under the GPL license. For the full content 4 * of this license, see the COPYING file at the top level of this 5 * source tree. 6 * adam li 7 * 8 * Test that CLOCK_THREAD_CPUTIME_ID is supported by timer_create(). 9 * 10 * Same test as 1-1.c. 11 */ 12 13 #include <time.h> 14 #include <signal.h> 15 #include <stdio.h> 16 #include <unistd.h> 17 #include <stdlib.h> 18 #include "posixtest.h" 19 20 #define SIGTOTEST SIGALRM 21 #define TIMERSEC 2 22 #define ACCEPTABLEDELTA 1 23 24 static volatile int caught_signal; 25 26 static void handler(int signo) 27 { 28 caught_signal = 1; 29 } 30 31 int main(void) 32 { 33 #if _POSIX_THREAD_CPUTIME == -1 34 printf("_POSIX_THREAD_CPUTIME not defined\n"); 35 return PTS_UNSUPPORTED; 36 #else 37 struct sigevent ev; 38 struct sigaction act; 39 timer_t tid; 40 struct itimerspec its; 41 struct timespec ts_start, ts_end; 42 int rc; 43 44 rc = sysconf(_SC_THREAD_CPUTIME); 45 if (rc == -1) { 46 printf("_SC_THREAD_CPUTIME unsupported\n"); 47 return PTS_UNSUPPORTED; 48 } 49 50 ev.sigev_notify = SIGEV_SIGNAL; 51 ev.sigev_signo = SIGTOTEST; 52 53 act.sa_handler = handler; 54 act.sa_flags = 0; 55 56 its.it_interval.tv_sec = 0; 57 its.it_interval.tv_nsec = 0; 58 its.it_value.tv_sec = TIMERSEC; 59 its.it_value.tv_nsec = 0; 60 61 if (sigemptyset(&act.sa_mask) == -1) { 62 perror("Error calling sigemptyset"); 63 return PTS_UNRESOLVED; 64 } 65 66 if (sigaction(SIGTOTEST, &act, 0) == -1) { 67 perror("Error calling sigaction"); 68 return PTS_UNRESOLVED; 69 } 70 71 if (timer_create(CLOCK_THREAD_CPUTIME_ID, &ev, &tid) != 0) { 72 perror("timer_create did not return success"); 73 return PTS_UNRESOLVED; 74 } 75 76 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts_start) != 0) { 77 perror("clock_gettime() failed"); 78 return PTS_UNRESOLVED; 79 } 80 81 if (timer_settime(tid, 0, &its, NULL) != 0) { 82 perror("timer_settime did not return success"); 83 return PTS_UNRESOLVED; 84 } 85 86 /* 87 * The bussy loop is intentional. The signal is send after 88 * two seconds of CPU time has been accumulated. 89 */ 90 while (!caught_signal) ; 91 92 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts_end) != 0) { 93 perror("clock_gettime() failed"); 94 return PTS_UNRESOLVED; 95 } 96 97 if (abs(ts_end.tv_sec - ts_start.tv_sec - TIMERSEC) <= ACCEPTABLEDELTA) { 98 printf("Test PASSED\n"); 99 return PTS_PASS; 100 } 101 102 printf("Timer did not last for correct amount of time\n" 103 "stop - start = %d - %d > %d\n", 104 (int)ts_end.tv_sec, (int)ts_start.tv_sec, 105 TIMERSEC + ACCEPTABLEDELTA); 106 return PTS_FAIL; 107 #endif 108 } 109