• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #include <stdio.h>
29 #include <time.h>
30 #include <signal.h>
31 #include <pthread.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 static timer_t   tid;
36 static int       count;
37 
38 /* this test program is there to check that disarming a timer
39  * can be done by calling timer_settime() with an it_interval
40  * value of 0
41  */
42 void
handle(sigval_t v)43 handle(sigval_t v)
44 {
45     time_t  t;
46     char    p[32];
47     struct itimerspec  ts;
48 
49     time(&t);
50     strftime(p, sizeof(p), "%T", localtime(&t));
51     printf("%s thread %d, val = %d, signal captured.\n",
52             p,  (int)pthread_self(), v.sival_int);
53     count += 1;
54 
55     /* this should disable the timer, and hence make 'count' no more than 1 */
56     ts.it_value.tv_sec = 0;
57     ts.it_value.tv_nsec = 0;
58     ts.it_interval.tv_sec  = 1;
59     ts.it_interval.tv_nsec = 0;
60     timer_settime(tid, TIMER_ABSTIME, &ts, NULL);
61 
62     return;
63 }
64 
65 int
create(int seconds,int id)66 create(int seconds, int id)
67 {
68     struct sigevent se;
69     struct itimerspec ts, ots;
70 
71     memset(&se, 0, sizeof (se));
72     se.sigev_notify = SIGEV_THREAD;
73     se.sigev_notify_function = handle;
74     se.sigev_value.sival_int = id;
75 
76     if (timer_create (CLOCK_REALTIME, &se, &tid) < 0)
77     {
78         perror ("timer_creat");
79         return -1;
80     }
81     puts ("timer_create successfully.");
82     ts.it_value.tv_sec =  0;
83     ts.it_value.tv_nsec = 1;
84     ts.it_interval.tv_sec = seconds;
85     ts.it_interval.tv_nsec = 0;
86     if (timer_settime (tid, TIMER_ABSTIME, &ts, &ots) < 0)
87     {
88         perror ("timer_settime");
89         return -1;
90     }
91     return 0;
92 }
93 
94 int
main(void)95 main (void)
96 {
97     if (create (1, 1) < 0) return 1;
98     sleep (4);
99 
100     if (count == 1) {
101         printf("OK\n");
102         return 0;
103     } else {
104         printf("KO (count=%d)\n", count);
105         return 1;
106     }
107 }
108