• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <errno.h>
18 #include <signal.h>
19 #include <time.h>
20 #include <unistd.h>
21 
22 #include "test.h"
23 #define SIGNUM 40
24 static int count = 0;
25 static timer_t timerid;
26 
handler(int sig)27 void handler(int sig)
28 {
29     count++;
30     usleep(1000000);
31     int ret = timer_getoverrun(timerid);
32     if (ret < 0) {
33         t_error("%s timer_getoverrun failed, ret is %d\n", __func__, ret);
34     }
35     return;
36 }
37 
38 /**
39  * @tc.name      : timer_getoverrun_0100
40  * @tc.desc      : Get overrun count for a per-process timer
41  * @tc.level     : Level 0
42  */
timer_getoverrun_0100(void)43 void timer_getoverrun_0100(void)
44 {
45     struct sigevent sev;
46 
47     sev.sigev_notify = SIGEV_SIGNAL;
48     sev.sigev_signo = SIGNUM;
49     sev.sigev_value.sival_ptr = &timerid;
50 
51     signal(SIGNUM, handler);
52 
53     int result = timer_create(CLOCK_REALTIME, &sev, &timerid);
54     if (result != 0) {
55         t_error("%s failed: result = %d\n", __func__, result);
56     }
57 
58     struct itimerspec its;
59     its.it_value.tv_sec = 0;
60     its.it_value.tv_nsec = 1000000;
61     its.it_interval.tv_sec = 0;
62     its.it_interval.tv_nsec = 0;
63 
64     result = timer_settime(timerid, 0, &its, NULL);
65     if (result != 0) {
66         t_error("%s failed: result = %d\n", __func__, result);
67     }
68 
69     while (count <= 0) {
70         sleep(1);
71     }
72     result = timer_delete(timerid);
73     if (result != 0) {
74         t_error("%s failed: result = %d\n", __func__, result);
75     }
76 }
77 
78 /**
79  * @tc.name      : timer_getoverrun_0200
80  * @tc.desc      : Invalid timerid
81  * @tc.level     : Level 2
82  */
timer_getoverrun_0200(void)83 void timer_getoverrun_0200(void)
84 {
85     int result = timer_getoverrun(NULL);
86     if (result != -1) {
87         t_error("%s timer_getoverrun failed, ret is %d\n", __func__, result);
88     }
89 }
90 
main(int argc,char * argv[])91 int main(int argc, char *argv[])
92 {
93     timer_getoverrun_0100();
94     timer_getoverrun_0200();
95 
96     return t_status;
97 }
98