2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/timerfd.h>
22 #include "vine-event-loop.h"
24 #include "vine-timer.h"
28 #define TIMER_SIGNAL_NO SIGRTMIN
29 VineTimer::VineTimer()
30 : __data(NULL), __timer_fd(-1)
32 VINE_LOGD("Timer is created [%p]", this);
36 VineTimer::~VineTimer()
41 void VineTimer::start(unsigned int timeout_ms,
42 std::function<void(void *user_data)> callback, void *user_data)
44 __timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
45 if (__timer_fd == -1) {
46 VINE_LOGE("timerfd_create fails %d", errno);
50 __timer_cb = callback;
53 struct itimerspec value;
54 value.it_interval.tv_sec = 0;
55 value.it_interval.tv_nsec = 0;
56 value.it_value.tv_sec = timeout_ms / 1000;
57 value.it_value.tv_nsec = (timeout_ms % 1000) * 1000000;
58 if (timerfd_settime(__timer_fd, 0, &value, NULL) != 0) {
59 VINE_LOGE("timerfd_settime fails %d", errno);
64 vine_event_loop_add_io_handler(__timer_fd, VINE_POLLIN, __timer_handler, this);
65 VINE_LOGI("Start VineTimer[%p] timer_fd[%d]", this, __timer_fd);
68 void VineTimer::stop()
70 VINE_LOGI("Stop VineTimer[%p] timer_fd[%d]", this, __timer_fd);
71 if (__timer_fd != -1) {
72 vine_event_loop_del_io_handler(__timer_fd);
78 void VineTimer::expired()
80 VINE_LOGI("VineTimer[%p] is expired. timer_fd[%d]", this, __timer_fd);
86 bool VineTimer::is_running()
88 return __timer_fd >= 0;
91 void VineTimer::__timer_handler(int fd, int events, void *user_data)
95 VineTimer *timer = (VineTimer *)user_data;