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 <errno.h>
17#include <signal.h>
18#include <stdio.h>
19#include <time.h>
20#include "test.h"
21
22#define MAX (3)
23static int count = 0;
24
25void timerHandler(int sig)
26{
27    if (count < MAX) {
28        count++;
29    }
30    printf("timer handler return\n");
31    return;
32}
33
34/**
35 * @tc.name      : timer_delete_0100
36 * @tc.desc      : Create a timer with a timeout of 1s and delete it
37 * @tc.level     : Level 0
38 */
39void timer_delete_0100(void)
40{
41    timer_t timerid;
42    struct sigevent sev;
43    struct itimerspec its;
44
45    sev.sigev_notify = SIGEV_SIGNAL;
46    sev.sigev_signo = SIGUSR1;
47    sev.sigev_value.sival_ptr = &timerid;
48    signal(SIGUSR1, timerHandler);
49
50    if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) == -1) {
51        t_error("%s timer create failed", __func__);
52        return;
53    }
54
55    its.it_value.tv_sec = 1;
56    its.it_value.tv_nsec = 0;
57    its.it_interval.tv_sec = its.it_value.tv_sec;
58    its.it_interval.tv_nsec = its.it_value.tv_nsec;
59
60    if (timer_settime(timerid, 0, &its, NULL) == -1) {
61        t_error("%s timer set time failed", __func__);
62        return;
63    }
64
65    while (count != MAX) {
66        sleep(1);
67    }
68
69    if (timer_delete(timerid) == -1 && errno == EINVAL) {
70        t_error("%s timer delete failed", __func__);
71    }
72}
73
74int main(int argc, char *argv[])
75{
76    timer_delete_0100();
77    return t_status;
78}