1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/*
*
* Embedded Linux library
*
* Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <errno.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <time.h>
#include <limits.h>
#include "useful.h"
#include "timeout.h"
#include "main-private.h"
#include "private.h"
/**
* SECTION:timeout
* @short_description: Timeout support
*
* Timeout support
*/
/**
* l_timeout:
*
* Opague object representing the timeout.
*/
struct l_timeout {
int fd;
l_timeout_notify_cb_t callback;
l_timeout_destroy_cb_t destroy;
void *user_data;
};
static void timeout_destroy(void *user_data)
{
struct l_timeout *timeout = user_data;
close(timeout->fd);
timeout->fd = -1;
if (timeout->destroy)
timeout->destroy(timeout->user_data);
}
static void timeout_callback(int fd, uint32_t events, void *user_data)
{
struct l_timeout *timeout = user_data;
uint64_t expired;
ssize_t result;
result = read(timeout->fd, &expired, sizeof(expired));
if (result != sizeof(expired))
return;
if (timeout->callback)
timeout->callback(timeout, timeout->user_data);
}
static inline int timeout_set(int fd, unsigned int seconds, long nanoseconds)
{
struct itimerspec itimer;
memset(&itimer, 0, sizeof(itimer));
itimer.it_interval.tv_sec = 0;
itimer.it_interval.tv_nsec = 0;
itimer.it_value.tv_sec = seconds;
itimer.it_value.tv_nsec = nanoseconds;
return timerfd_settime(fd, 0, &itimer, NULL);
}
static bool convert_ms(unsigned long milliseconds, unsigned int *seconds,
long *nanoseconds)
{
unsigned long big_seconds = milliseconds / 1000;
if (big_seconds > UINT_MAX)
return false;
*seconds = big_seconds;
*nanoseconds = (milliseconds % 1000) * 1000000L;
return true;
}
/**
* timeout_create_with_nanoseconds:
* @seconds: number of seconds
* @nanoseconds: number of nanoseconds
* @callback: timeout callback function
* @user_data: user data provided to timeout callback function
* @destroy: destroy function for user data
*
* Create new timeout callback handling.
*
* The timeout will only fire once. The timeout handling needs to be rearmed
* with one of the l_timeout_modify functions to trigger again.
*
* Returns: a newly allocated #l_timeout object. On failure, the function
* returns NULL.
**/
static struct l_timeout *timeout_create_with_nanoseconds(unsigned int seconds,
long nanoseconds, l_timeout_notify_cb_t callback,
void *user_data, l_timeout_destroy_cb_t destroy)
{
struct l_timeout *timeout;
int err;
if (unlikely(!callback))
return NULL;
timeout = l_new(struct l_timeout, 1);
timeout->callback = callback;
timeout->destroy = destroy;
timeout->user_data = user_data;
timeout->fd = timerfd_create(CLOCK_MONOTONIC,
TFD_NONBLOCK | TFD_CLOEXEC);
if (timeout->fd < 0) {
l_free(timeout);
return NULL;
}
if (seconds > 0 || nanoseconds > 0) {
if (timeout_set(timeout->fd, seconds, nanoseconds) < 0) {
close(timeout->fd);
l_free(timeout);
return NULL;
}
}
err = watch_add(timeout->fd, EPOLLIN | EPOLLONESHOT, timeout_callback,
timeout, timeout_destroy);
if (err < 0) {
l_free(timeout);
return NULL;
}
return timeout;
}
/**
* l_timeout_create:
* @seconds: timeout in seconds
* @callback: timeout callback function
* @user_data: user data provided to timeout callback function
* @destroy: destroy function for user data
*
* Create new timeout callback handling.
*
* The timeout will only fire once. The timeout handling needs to be rearmed
* with one of the l_timeout_modify functions to trigger again.
*
* Returns: a newly allocated #l_timeout object. On failure, the function
* returns NULL.
**/
LIB_EXPORT struct l_timeout *l_timeout_create(unsigned int seconds,
l_timeout_notify_cb_t callback,
void *user_data, l_timeout_destroy_cb_t destroy)
{
return timeout_create_with_nanoseconds(seconds, 0, callback,
user_data, destroy);
}
/**
* l_timeout_create_ms:
* @milliseconds: timeout in milliseconds
* @callback: timeout callback function
* @user_data: user data provided to timeout callback function
* @destroy: destroy function for user data
*
* Create new timeout callback handling.
*
* The timeout will only fire once. The timeout handling needs to be rearmed
* with one of the l_timeout_modify functions to trigger again.
*
* Returns: a newly allocated #l_timeout object. On failure, the function
* returns NULL.
**/
LIB_EXPORT struct l_timeout *l_timeout_create_ms(unsigned long milliseconds,
l_timeout_notify_cb_t callback,
void *user_data, l_timeout_destroy_cb_t destroy)
{
unsigned int seconds;
long nanoseconds;
if (!convert_ms(milliseconds, &seconds, &nanoseconds))
return NULL;
return timeout_create_with_nanoseconds(seconds, nanoseconds, callback,
user_data, destroy);
}
/**
* l_timeout_modify:
* @timeout: timeout object
* @seconds: timeout in seconds
*
* Modify an existing @timeout and rearm it.
**/
LIB_EXPORT void l_timeout_modify(struct l_timeout *timeout,
unsigned int seconds)
{
if (unlikely(!timeout))
return;
if (unlikely(timeout->fd < 0))
return;
if (seconds > 0) {
if (timeout_set(timeout->fd, seconds, 0) < 0)
return;
}
watch_modify(timeout->fd, EPOLLIN | EPOLLONESHOT, true);
}
/**
* l_timeout_modify_ms:
* @timeout: timeout object
* @milliseconds: number of milliseconds
*
* Modify an existing @timeout and rearm it.
**/
LIB_EXPORT void l_timeout_modify_ms(struct l_timeout *timeout,
unsigned long milliseconds)
{
if (unlikely(!timeout))
return;
if (unlikely(timeout->fd < 0))
return;
if (milliseconds > 0) {
unsigned int sec;
long nanosec;
if (!convert_ms(milliseconds, &sec, &nanosec) ||
timeout_set(timeout->fd, sec, nanosec) < 0)
return;
}
watch_modify(timeout->fd, EPOLLIN | EPOLLONESHOT, true);
}
/**
* l_timeout_remove:
* @timeout: timeout object
*
* Remove timeout handling.
**/
LIB_EXPORT void l_timeout_remove(struct l_timeout *timeout)
{
if (unlikely(!timeout))
return;
watch_remove(timeout->fd, false);
l_free(timeout);
}
/**
* l_timeout_set_callback:
* @timeout: timeout object
* @callback: The new callback
* @user_data: The new user_data
* @destroy: The new destroy function
*
* Sets the new notify callback for @timeout. If the old user_data object had
* a destroy function set, then that function will be called.
*/
LIB_EXPORT void l_timeout_set_callback(struct l_timeout *timeout,
l_timeout_notify_cb_t callback,
void *user_data,
l_timeout_destroy_cb_t destroy)
{
if (unlikely(!timeout))
return;
if (timeout->destroy)
timeout->destroy(timeout->user_data);
timeout->callback = callback;
timeout->user_data = user_data;
timeout->destroy = destroy;
}
|