sc/timer/sc_timer.h

146 lines
4.5 KiB
C
Raw Normal View History

2020-11-11 01:19:49 +03:00
/*
* BSD-3-Clause
2020-11-11 01:19:49 +03:00
*
* Copyright 2021 Ozan Tezcan
* All rights reserved.
2020-11-11 01:19:49 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
2020-11-11 01:19:49 +03:00
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
2020-11-11 01:19:49 +03:00
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
2020-11-11 01:19:49 +03:00
*/
#ifndef SC_TIMER_H
#define SC_TIMER_H
2021-04-13 22:36:50 +03:00
#define SC_TIMER_VERSION "2.0.0"
2021-02-14 16:19:26 +03:00
2020-11-11 01:19:49 +03:00
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else
#define sc_timer_malloc malloc
#define sc_timer_free free
#endif
2020-11-11 01:19:49 +03:00
#define SC_TIMER_INVALID UINT64_MAX
struct sc_timer_data {
uint64_t timeout;
uint64_t type;
void *data;
2020-11-11 01:19:49 +03:00
};
struct sc_timer {
uint64_t timestamp;
uint32_t head;
uint32_t wheel;
struct sc_timer_data *list;
2020-11-11 01:19:49 +03:00
};
/**
2021-02-03 08:09:50 +03:00
* Init timer
*
* @param t timer
* @param timestamp current timestamp. Use monotonic timer source.
2020-11-11 01:19:49 +03:00
*/
void sc_timer_init(struct sc_timer *t, uint64_t timestamp);
2020-11-11 01:19:49 +03:00
/**
* Destroy timer.
* @param t timer
2020-11-11 01:19:49 +03:00
*/
void sc_timer_term(struct sc_timer *t);
2020-11-11 01:19:49 +03:00
/**
* Remove all timers without deallocating underlying memory.
* @param t timer
2020-11-11 01:19:49 +03:00
*/
void sc_timer_clear(struct sc_timer *t);
2020-11-11 01:19:49 +03:00
/**
* Add timer
2021-02-05 20:46:59 +03:00
* 'timeout' is relative to latest 'timestamp' value given to the 'timer'.
2020-11-11 01:19:49 +03:00
*
2023-04-21 00:43:26 +03:00
* e.g.,
* sc_timer_init(&timer, 1000); // Current timestamp is 1000.
2020-11-11 01:19:49 +03:00
* sc_timer_add(&timer, arg, 10); // Timeout will be at 1010.
* sc_timer_timeout(&timer, 2000, arg, callback); // Timestamp is now 2000.
* sc_timer_add(&timer, arg, 10); // Timeout will be at 2010.
*
*
* @param t timer
2021-02-03 08:09:50 +03:00
* @param timeout timeout value, this is relative to 'sc_timer_init's timer.
2023-04-21 00:43:26 +03:00
* e.g., sc_timer_init(&timer, 10); // say, start time is 10
* milliseconds
2021-02-03 08:09:50 +03:00
* @param data user data to pass into callback on 'sc_timer_timeout' call.
* @param type user data to pass into callback on 'sc_timer_timeout' call.
2020-11-11 01:19:49 +03:00
* @return SC_TIMER_INVALID on out of memory. Otherwise, timer id. You
2021-02-03 08:09:50 +03:00
* can cancel this timer via this id later.
2020-11-11 01:19:49 +03:00
*/
uint64_t sc_timer_add(struct sc_timer *t, uint64_t timeout, uint64_t type,
void *data);
2020-11-11 01:19:49 +03:00
/**
* uint64_t id = sc_timer_add(&timer, arg, 10);
* sc_timer_cancel(&timer, &id);
*
* @param t timer
* @param id timer id
2020-11-11 01:19:49 +03:00
*/
void sc_timer_cancel(struct sc_timer *t, uint64_t *id);
2020-11-11 01:19:49 +03:00
/**
* Checks timeouts and calls 'callback' function for each timeout.
*
2020-11-11 01:19:49 +03:00
* Logical pattern is :
*
2023-04-21 00:43:26 +03:00
* e.g.,
*
2020-11-11 01:19:49 +03:00
* struct sc_timer timer;
* sc_timer_init(&timer, time_ms());
* sc_timer_add(&timer, data, 100);
*
* while (true) {
* uint64_t timeout = sc_timer_timeout(&timer, time_ms(), arg, callback);
2021-02-03 08:09:50 +03:00
* sleep(timeout); // or select(timeout), epoll_wait(timeout) etc..
2020-11-11 01:19:49 +03:00
* }
*
*
* @param t timer
2021-02-03 08:09:50 +03:00
* @param timestamp current timestamp
* @param arg user data to user callback
2020-11-11 01:19:49 +03:00
* @param callback 'arg' is user data.
* 'timeout' is scheduled timeout for that timer.
* 'type' is what user passed on 'sc_timer_add'.
2020-11-11 01:19:49 +03:00
* 'data' is what user passed on 'sc_timer_add'.
* @return next timeout.
*/
uint64_t sc_timer_timeout(struct sc_timer *t, uint64_t timestamp, void *arg,
void (*callback)(void *arg, uint64_t timeout,
uint64_t type, void *data));
2020-11-11 01:19:49 +03:00
#endif