• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <unistd.h>
32 #include <signal.h>
33 #include <time.h>
34 #include <sys/time.h>
35 #include <osTest.h>
36 #include "lt_timer_test.h"
37 
38 /* signo should be in the range SIGRTMIN to SIGRTMAX when SA_SIGINFO flag is set. */
39 #define SIG SIGRTMIN
40 #define CLOCKID CLOCK_REALTIME
41 
42 static int g_handlerFlag;
43 static int g_tmrOverrun;
44 
SigHandler(int sig,siginfo_t * si,void * uc)45 static void SigHandler(int sig, siginfo_t *si, void *uc)
46 {
47     if (si == nullptr) {
48         LogPrintln("[ERROR]sig %d, si %p, uc %p\n", sig, si, uc);
49         return;
50     }
51 
52 #ifdef TEST_ON_LINUX
53     timer_t timerid = *(timer_t *)si->si_value.sival_ptr;
54 #else // SA_SIGINFO not compatible with POSIX on HMOS
55     timer_t timerid = *reinterpret_cast<timer_t *>(si);
56 #endif
57 
58     g_tmrOverrun += timer_getoverrun(timerid);
59     LogPrintln("signo %d handlerFlag %d timer %p overrun %d\n", sig, ++g_handlerFlag, timerid, g_tmrOverrun);
60 }
61 
SigInfoTimerTest(void)62 static int SigInfoTimerTest(void)
63 {
64     int interval = 3; // 3 seconds
65     timer_t timerid;
66     struct sigevent sev;
67     struct itimerspec its;
68     sigset_t mask;
69     struct sigaction sa;
70     int ret;
71 
72     /* Install handler for timer signal. */
73     sa.sa_flags = SA_SIGINFO;
74     sa.sa_sigaction = SigHandler;
75     sigemptyset(&sa.sa_mask);
76     ret = sigaction(SIG, &sa, nullptr);
77     LogPrintln("sigaction %d: %d", SIG, ret);
78     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
79 
80     /* Block timer signal */
81     sigemptyset(&mask);
82     sigaddset(&mask, SIG);
83     ret = sigprocmask(SIG_BLOCK, &mask, nullptr);
84     LogPrintln("sigprocmask setmask %d: %d", SIG, ret);
85     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
86 
87     /* Create the timer */
88     sev.sigev_notify = SIGEV_SIGNAL;
89     sev.sigev_signo = SIG;
90     sev.sigev_value.sival_ptr = &timerid;
91     ret = timer_create(CLOCKID, &sev, &timerid);
92     LogPrintln("timer_create %p: %d", timerid, ret);
93     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
94 
95     /* Start the timer */
96     its.it_value.tv_sec = 0;
97     its.it_value.tv_nsec = 990000000; // 990000000, 0.99s
98     its.it_interval.tv_sec = its.it_value.tv_sec;
99     its.it_interval.tv_nsec = its.it_value.tv_nsec;
100 
101     ret = timer_settime(timerid, 0, &its, nullptr);
102     LogPrintln("timer_settime %p: %d", timerid, ret);
103     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
104 
105     /* Sleep for a while */
106     LogPrintln("sleep %ds", interval);
107     sleep(interval); // should not be interrupted
108 
109     /* Get the timer's time */
110     ret = timer_gettime(timerid, &its);
111     LogPrintln("timer_gettime %p: %d", timerid, ret);
112     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
113 
114     /* Get the timer's overruns */
115     ret = timer_getoverrun(timerid);
116     LogPrintln("timer_getoverrun %p: %d", timerid, ret);
117     ICUNIT_ASSERT_NOT_EQUAL(ret, -1, ret);
118 
119     /* Unlock the timer signal */
120     ret = sigprocmask(SIG_UNBLOCK, &mask, nullptr);
121     LogPrintln("sigprocmask unblock %d: %d", SIG, ret);
122     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
123 
124     LogPrintln("sleep another %ds", interval);
125     sleep(interval); // should be interrupted
126     LogPrintln("sleep time over, g_handlerFlag = %d", g_handlerFlag);
127 
128     ret = timer_delete(timerid);
129     LogPrintln("timer_delete %p %d", timerid, ret);
130     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
131 
132     ICUNIT_ASSERT_NOT_EQUAL(g_handlerFlag, 0, g_handlerFlag);
133     ICUNIT_ASSERT_NOT_EQUAL(g_tmrOverrun, 0, g_tmrOverrun);
134 
135     return 0;
136 }
137 
TimerTest004(void)138 void TimerTest004(void)
139 {
140     TEST_ADD_CASE(__FUNCTION__, SigInfoTimerTest, TEST_POSIX, TEST_SWTMR, TEST_LEVEL0, TEST_FUNCTION);
141 }
142