parent
b2d6ed8754
commit
79318c47b8
@ -0,0 +1,18 @@
|
||||
# timer
|
||||
|
||||
add_library(wf_timer STATIC
|
||||
lib/wf/timer/src/wf/timer/api.c
|
||||
lib/wf/timer/src/wf/timer/impl/manager.c
|
||||
lib/wf/timer/src/wf/timer/impl/timepoint.c
|
||||
lib/wf/timer/src/wf/timer/impl/timer.c
|
||||
)
|
||||
|
||||
target_include_directories(wf_timer PRIVATE
|
||||
lib/wf/timer/src
|
||||
)
|
||||
|
||||
target_include_directories(wf_timer PUBLIC
|
||||
lib/wf/timer/include
|
||||
)
|
||||
|
||||
set_target_properties(wf_timer PROPERTIES C_VISIBILITY_PRESET hidden)
|
@ -1,84 +0,0 @@
|
||||
#include "webfuse/adapter/impl/time/timeout_manager_intern.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include "webfuse/adapter/impl/time/timer_intern.h"
|
||||
#include "webfuse/adapter/impl/time/timepoint.h"
|
||||
|
||||
void wf_impl_timeout_manager_init(
|
||||
struct wf_impl_timeout_manager * manager)
|
||||
{
|
||||
manager->timers = NULL;
|
||||
}
|
||||
|
||||
void wf_impl_timeout_manager_cleanup(
|
||||
struct wf_impl_timeout_manager * manager)
|
||||
{
|
||||
struct wf_impl_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_impl_timer * next = timer->next;
|
||||
|
||||
wf_impl_timer_trigger(timer);
|
||||
|
||||
timer = next;
|
||||
}
|
||||
|
||||
manager->timers = NULL;
|
||||
|
||||
}
|
||||
|
||||
void wf_impl_timeout_manager_check(
|
||||
struct wf_impl_timeout_manager * manager)
|
||||
{
|
||||
struct wf_impl_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_impl_timer * next = timer->next;
|
||||
|
||||
if (wf_impl_timer_is_timeout(timer))
|
||||
{
|
||||
wf_impl_timeout_manager_removetimer(manager, timer);
|
||||
wf_impl_timer_trigger(timer);
|
||||
}
|
||||
|
||||
timer = next;
|
||||
}
|
||||
}
|
||||
|
||||
void wf_impl_timeout_manager_addtimer(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
if (NULL != manager->timers)
|
||||
{
|
||||
manager->timers->prev = timer;
|
||||
}
|
||||
|
||||
timer->next = manager->timers;
|
||||
timer->prev = NULL;
|
||||
manager->timers = timer;
|
||||
}
|
||||
|
||||
void wf_impl_timeout_manager_removetimer(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
struct wf_impl_timer * prev = timer->prev;
|
||||
struct wf_impl_timer * next = timer->next;
|
||||
|
||||
if (NULL != prev)
|
||||
{
|
||||
prev->next = next;
|
||||
}
|
||||
|
||||
if (NULL != next)
|
||||
{
|
||||
next->prev = prev;
|
||||
}
|
||||
|
||||
if (manager->timers == timer)
|
||||
{
|
||||
manager->timers = next;
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_impl_timer;
|
||||
struct wf_impl_timeout_manager
|
||||
{
|
||||
struct wf_impl_timer * timers;
|
||||
};
|
||||
|
||||
extern void wf_impl_timeout_manager_init(
|
||||
struct wf_impl_timeout_manager * manager);
|
||||
|
||||
extern void wf_impl_timeout_manager_cleanup(
|
||||
struct wf_impl_timeout_manager * manager);
|
||||
|
||||
extern void wf_impl_timeout_manager_check(
|
||||
struct wf_impl_timeout_manager * manager);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,24 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_INTERN_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMEOUT_MANAGER_INTERN_H
|
||||
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wf_impl_timeout_manager_addtimer(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
extern void wf_impl_timeout_manager_removetimer(
|
||||
struct wf_impl_timeout_manager * manager,
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -1,31 +0,0 @@
|
||||
#include "webfuse/adapter/impl/time/timepoint.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define WF_MSEC_PER_SEC ((wf_impl_timepoint) 1000)
|
||||
#define WF_NSEC_PER_MSEC ((wf_impl_timepoint) 1000 * 1000)
|
||||
|
||||
wf_impl_timepoint wf_impl_timepoint_now(void)
|
||||
{
|
||||
struct timespec tp;
|
||||
clock_gettime(CLOCK_MONOTONIC, &tp);
|
||||
|
||||
wf_impl_timepoint const now = (tp.tv_sec * WF_MSEC_PER_SEC) + (tp.tv_nsec / WF_NSEC_PER_MSEC);
|
||||
return now;
|
||||
}
|
||||
|
||||
wf_impl_timepoint wf_impl_timepoint_in_msec(wf_impl_timediff value)
|
||||
{
|
||||
wf_impl_timepoint const now = wf_impl_timepoint_now();
|
||||
wf_impl_timepoint result = now + ((wf_impl_timepoint) value);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool wf_impl_timepoint_is_elapsed(wf_impl_timepoint tp)
|
||||
{
|
||||
wf_impl_timepoint const now = wf_impl_timepoint_now();
|
||||
wf_impl_timediff const diff = (wf_impl_timediff) (tp - now);
|
||||
|
||||
return (0 > diff);
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMEPOINT_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMEPOINT_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
#else
|
||||
#include <cinttypes>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef uint64_t wf_impl_timepoint;
|
||||
typedef int64_t wf_impl_timediff;
|
||||
|
||||
extern wf_impl_timepoint wf_impl_timepoint_now(void);
|
||||
|
||||
extern wf_impl_timepoint wf_impl_timepoint_in_msec(
|
||||
wf_impl_timediff value);
|
||||
|
||||
extern bool wf_impl_timepoint_is_elapsed(
|
||||
wf_impl_timepoint timepoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,65 +0,0 @@
|
||||
#include "webfuse/adapter/impl/time/timer_intern.h"
|
||||
#include "webfuse/adapter/impl/time/timeout_manager_intern.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
void wf_impl_timer_init(
|
||||
struct wf_impl_timer * timer,
|
||||
struct wf_impl_timeout_manager * manager)
|
||||
{
|
||||
timer->manager = manager;
|
||||
timer->timeout = 0;
|
||||
timer->timeout_handler = NULL;
|
||||
timer->user_data = NULL;
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
}
|
||||
|
||||
void wf_impl_timer_cleanup(
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
memset(timer, 0, sizeof(struct wf_impl_timer));
|
||||
}
|
||||
|
||||
void wf_impl_timer_start(
|
||||
struct wf_impl_timer * timer,
|
||||
wf_impl_timepoint absolute_timeout,
|
||||
wf_impl_timer_timeout_fn * handler,
|
||||
void * user_data)
|
||||
{
|
||||
timer->timeout = absolute_timeout;
|
||||
timer->timeout_handler = handler;
|
||||
timer->user_data = user_data;
|
||||
|
||||
wf_impl_timeout_manager_addtimer(timer->manager, timer);
|
||||
}
|
||||
|
||||
void wf_impl_timer_cancel(
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
wf_impl_timeout_manager_removetimer(timer->manager, timer);
|
||||
|
||||
timer->timeout = 0;
|
||||
timer->timeout_handler = NULL;
|
||||
timer->user_data = NULL;
|
||||
}
|
||||
|
||||
bool wf_impl_timer_is_timeout(
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
return wf_impl_timepoint_is_elapsed(timer->timeout);
|
||||
}
|
||||
|
||||
|
||||
void wf_impl_timer_trigger(
|
||||
struct wf_impl_timer * timer)
|
||||
{
|
||||
if (NULL != timer->timeout_handler)
|
||||
{
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
timer->timeout_handler(timer);
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMER_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMER_H
|
||||
|
||||
#include "webfuse/adapter/impl/time/timepoint.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_impl_timer;
|
||||
struct wf_impl_timeout_manager;
|
||||
|
||||
typedef void wf_impl_timer_timeout_fn(struct wf_impl_timer * timer);
|
||||
|
||||
struct wf_impl_timer
|
||||
{
|
||||
struct wf_impl_timeout_manager * manager;
|
||||
wf_impl_timepoint timeout;
|
||||
wf_impl_timer_timeout_fn * timeout_handler;
|
||||
void * user_data;
|
||||
struct wf_impl_timer * next;
|
||||
struct wf_impl_timer * prev;
|
||||
};
|
||||
|
||||
extern void wf_impl_timer_init(
|
||||
struct wf_impl_timer * timer,
|
||||
struct wf_impl_timeout_manager * manager);
|
||||
|
||||
extern void wf_impl_timer_cleanup(
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
extern void wf_impl_timer_start(
|
||||
struct wf_impl_timer * timer,
|
||||
wf_impl_timepoint absolute_timeout,
|
||||
wf_impl_timer_timeout_fn * handler,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_impl_timer_cancel(
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -1,25 +0,0 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMER_INTERN_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMER_INTERN_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include "webfuse/adapter/impl/time/timer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern bool wf_impl_timer_is_timeout(
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
extern void wf_impl_timer_trigger(
|
||||
struct wf_impl_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,9 @@
|
||||
#ifndef WF_TIMER_H
|
||||
#define WF_TIMER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
#include <wf/timer/on_timer_fn.h>
|
||||
#include <wf/timer/timer.h>
|
||||
#include <wf/timer/manager.h>
|
||||
|
||||
#endif
|
@ -0,0 +1,8 @@
|
||||
#ifndef WF_TIMER_API_H
|
||||
#define WF_TIMER_API_H
|
||||
|
||||
#ifndef WF_TIMER_API
|
||||
#define WF_TIMER_API
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,29 @@
|
||||
#ifndef WF_TIMER_MANAGER_H
|
||||
#define WF_TIMER_MANAGER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern WF_TIMER_API struct wf_timer_manager *
|
||||
wf_timer_manager_create(void);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,19 @@
|
||||
#ifndef WF_TIMER_ON_TIMER_FN_H
|
||||
#define WF_TIMER_ON_TIMER_FN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
|
||||
typedef void wf_timer_on_timer_fn(
|
||||
struct wf_timer * timer,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,38 @@
|
||||
#ifndef WF_TIMER_TIMER_H
|
||||
#define WF_TIMER_TIMER_H
|
||||
|
||||
#include <wf/timer/api.h>
|
||||
#include <wf/timer/on_timer_fn.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern WF_TIMER_API struct wf_timer *
|
||||
wf_timer_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms);
|
||||
|
||||
extern WF_TIMER_API void
|
||||
wf_timer_cancel(
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,59 @@
|
||||
#include "wf/timer.h"
|
||||
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timer.h"
|
||||
|
||||
// manager
|
||||
|
||||
struct wf_timer_manager *
|
||||
wf_timer_manager_create(void)
|
||||
{
|
||||
return wf_timer_impl_manager_create();
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
wf_timer_impl_manager_dispose(manager);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
wf_timer_impl_manager_check(manager);
|
||||
}
|
||||
|
||||
// timer
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data)
|
||||
{
|
||||
return wf_timer_impl_create(manager, on_timer, user_data);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_impl_dispose(timer);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms)
|
||||
{
|
||||
wf_timer_impl_start(timer, timeout_ms);
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_cancel(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_impl_cancel(timer);
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timer.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct wf_timer_manager
|
||||
{
|
||||
struct wf_timer * timers;
|
||||
};
|
||||
|
||||
struct wf_timer_manager *
|
||||
wf_timer_impl_manager_create(void)
|
||||
{
|
||||
struct wf_timer_manager * manager = malloc(sizeof(struct wf_timer_manager));
|
||||
if (NULL != manager)
|
||||
{
|
||||
manager->timers = NULL;
|
||||
}
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_impl_manager_dispose(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
wf_timer_impl_trigger(timer);
|
||||
timer = next;
|
||||
}
|
||||
|
||||
free(manager);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_impl_manager_check(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
if (wf_timer_impl_is_timeout(timer))
|
||||
{
|
||||
wf_timer_impl_manager_removetimer(manager, timer);
|
||||
wf_timer_impl_trigger(timer);
|
||||
}
|
||||
|
||||
timer = next;
|
||||
}
|
||||
}
|
||||
|
||||
void wf_timer_impl_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
if (NULL != manager->timers)
|
||||
{
|
||||
manager->timers->prev = timer;
|
||||
}
|
||||
|
||||
timer->next = manager->timers;
|
||||
timer->prev = NULL;
|
||||
manager->timers = timer;
|
||||
}
|
||||
|
||||
void wf_timer_impl_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
struct wf_timer * prev = timer->prev;
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
if (NULL != prev)
|
||||
{
|
||||
prev->next = next;
|
||||
}
|
||||
|
||||
if (NULL != next)
|
||||
{
|
||||
next->prev = prev;
|
||||
}
|
||||
|
||||
if (manager->timers == timer)
|
||||
{
|
||||
manager->timers = next;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
#ifndef WF_TIMER_IMPL_MANAGER_H
|
||||
#define WF_TIMER_IMPL_MANAGER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern struct wf_timer_manager *
|
||||
wf_timer_impl_manager_create(void);
|
||||
|
||||
extern void
|
||||
wf_timer_impl_manager_dispose(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern void
|
||||
wf_timer_impl_manager_check(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern void wf_timer_impl_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_impl_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,31 @@
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define WF_TIMER_MSEC_PER_SEC ((wf_timer_timepoint) 1000)
|
||||
#define WF_TIMER_NSEC_PER_MSEC ((wf_timer_timepoint) 1000 * 1000)
|
||||
|
||||
wf_timer_timepoint wf_timer_impl_timepoint_now(void)
|
||||
{
|
||||
struct timespec tp;
|
||||
clock_gettime(CLOCK_MONOTONIC, &tp);
|
||||
|
||||
wf_timer_timepoint const now = (tp.tv_sec * WF_TIMER_MSEC_PER_SEC) + (tp.tv_nsec / WF_TIMER_NSEC_PER_MSEC);
|
||||
return now;
|
||||
}
|
||||
|
||||
wf_timer_timepoint wf_timer_impl_timepoint_in_msec(wf_timer_timediff value)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timepoint result = now + ((wf_timer_timepoint) value);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool wf_timer_impl_timepoint_is_elapsed(wf_timer_timepoint tp)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timediff const diff = (wf_timer_timediff) (tp - now);
|
||||
|
||||
return (0 > diff);
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#ifndef WF_TIMER_IMPL_TIMEPOINT_H
|
||||
#define WF_TIMER_IMPL_TIMEPOINT_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
#else
|
||||
#include <cinttypes>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef uint64_t wf_timer_timepoint;
|
||||
typedef int64_t wf_timer_timediff;
|
||||
|
||||
extern wf_timer_timepoint wf_timer_impl_timepoint_now(void);
|
||||
|
||||
extern wf_timer_timepoint wf_timer_impl_timepoint_in_msec(
|
||||
wf_timer_timediff value);
|
||||
|
||||
extern bool wf_timer_impl_timepoint_is_elapsed(
|
||||
wf_timer_timepoint timepoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,67 @@
|
||||
#include "wf/timer/impl/timer.h"
|
||||
#include "wf/timer/impl/manager.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_impl_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data)
|
||||
{
|
||||
struct wf_timer * timer = malloc(sizeof(struct wf_timer));
|
||||
timer->manager = manager;
|
||||
timer->timeout = 0;
|
||||
timer->on_timer = on_timer;
|
||||
timer->user_data = user_data;
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
return timer;
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_impl_dispose(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
free(timer);
|
||||
}
|
||||
|
||||
void wf_timer_impl_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms)
|
||||
{
|
||||
timer->timeout = wf_timer_impl_timepoint_in_msec(timeout_ms);
|
||||
|
||||
wf_timer_impl_manager_addtimer(timer->manager, timer);
|
||||
}
|
||||
|
||||
void wf_timer_impl_cancel(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_impl_manager_removetimer(timer->manager, timer);
|
||||
|
||||
timer->timeout = 0;
|
||||
}
|
||||
|
||||
bool wf_timer_impl_is_timeout(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
return wf_timer_impl_timepoint_is_elapsed(timer->timeout);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_impl_trigger(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
if (0 != timer->on_timer)
|
||||
{
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
timer->on_timer(timer, timer->user_data);
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
#ifndef WF_ADAPTER_IMPL_TIME_TIMER_H
|
||||
#define WF_ADAPTER_IMPL_TIME_TIMER_H
|
||||
|
||||
#include "wf/timer/on_timer_fn.h"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer_manager;
|
||||
|
||||
struct wf_timer
|
||||
{
|
||||
struct wf_timer_manager * manager;
|
||||
wf_timer_timepoint timeout;
|
||||
wf_timer_on_timer_fn * on_timer;
|
||||
void * user_data;
|
||||
struct wf_timer * next;
|
||||
struct wf_timer * prev;
|
||||
};
|
||||
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_impl_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data);
|
||||
|
||||
void
|
||||
wf_timer_impl_dispose(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_impl_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms);
|
||||
|
||||
extern void wf_timer_impl_cancel(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern bool wf_timer_impl_is_timeout(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_impl_trigger(
|
||||
struct wf_timer * timer);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -0,0 +1,36 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse/utils/msleep.hpp"
|
||||
#include "wf/timer/impl/timepoint.h"
|
||||
|
||||
using webfuse_test::msleep;
|
||||
|
||||
TEST(wf_timer_timepoint, now)
|
||||
{
|
||||
wf_timer_timepoint start = wf_timer_impl_timepoint_now();
|
||||
msleep(42);
|
||||
wf_timer_timepoint end = wf_timer_impl_timepoint_now();
|
||||
|
||||
ASSERT_LT(start, end);
|
||||
ASSERT_LT(end, start + 500);
|
||||
}
|
||||
|
||||
TEST(wf_timer_timepoint, in_msec)
|
||||
{
|
||||
wf_timer_timepoint now = wf_timer_impl_timepoint_now();
|
||||
wf_timer_timepoint later = wf_timer_impl_timepoint_in_msec(42);
|
||||
|
||||
ASSERT_LT(now, later);
|
||||
ASSERT_LT(later, now + 500);
|
||||
}
|
||||
|
||||
TEST(wf_timer_timepoint, elapsed)
|
||||
{
|
||||
wf_timer_timepoint now;
|
||||
|
||||
now = wf_timer_impl_timepoint_now();
|
||||
ASSERT_TRUE(wf_timer_impl_timepoint_is_elapsed(now - 1));
|
||||
|
||||
now = wf_timer_impl_timepoint_now();
|
||||
ASSERT_FALSE(wf_timer_impl_timepoint_is_elapsed(now + 500));
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "webfuse/utils/msleep.hpp"
|
||||
#include "wf/timer/timer.h"
|
||||
#include "wf/timer/manager.h"
|
||||
|
||||
using std::size_t;
|
||||
using webfuse_test::msleep;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void on_timeout(struct wf_timer * timer, void * user_data)
|
||||
{
|
||||
(void) timer;
|
||||
|
||||
bool * triggered = reinterpret_cast<bool*>(user_data);
|
||||
*triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(wf_timer, init)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, trigger)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, 250);
|
||||
msleep(500);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, trigger_on_dispose)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, (5 * 60 * 1000));
|
||||
|
||||
wf_timer_manager_dispose(manager);
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
}
|
||||
|
||||
TEST(wf_timer, cancel)
|
||||
{
|
||||
bool triggered = false;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_timer_start(timer, 250);
|
||||
msleep(500);
|
||||
wf_timer_cancel(timer);
|
||||
wf_timer_manager_check(manager);
|
||||
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
wf_timer_dispose(timer);
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, cancel_multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer[count];
|
||||
|
||||
bool triggered = false;
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
timer[i] = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
wf_timer_start(timer[i], 0);
|
||||
}
|
||||
|
||||
msleep(10);
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_timer_cancel(timer[i]);
|
||||
}
|
||||
|
||||
wf_timer_manager_check(manager);
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_timer_dispose(timer[i]);
|
||||
}
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
||||
|
||||
TEST(wf_timer, multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_timer_manager * manager = wf_timer_manager_create();
|
||||
struct wf_timer * timer[count];
|
||||
bool triggered[count];
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
timer[i] = wf_timer_create(manager, &on_timeout, reinterpret_cast<void*>(&triggered[i]));
|
||||
triggered[i] = false;
|
||||
wf_timer_start(timer[i], (300 - (50 * i)));
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
msleep(100);
|
||||
wf_timer_manager_check(manager);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
ASSERT_TRUE(triggered[i]);
|
||||
wf_timer_dispose(timer[i]);
|
||||
}
|
||||
|
||||
wf_timer_manager_dispose(manager);
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse/utils/msleep.hpp"
|
||||
#include "webfuse/adapter/impl/time/timepoint.h"
|
||||
|
||||
using webfuse_test::msleep;
|
||||
|
||||
TEST(timepoint, now)
|
||||
{
|
||||
wf_impl_timepoint start = wf_impl_timepoint_now();
|
||||
msleep(42);
|
||||
wf_impl_timepoint end = wf_impl_timepoint_now();
|
||||
|
||||
ASSERT_LT(start, end);
|
||||
ASSERT_LT(end, start + 500);
|
||||
}
|
||||
|
||||
TEST(timepoint, in_msec)
|
||||
{
|
||||
wf_impl_timepoint now = wf_impl_timepoint_now();
|
||||
wf_impl_timepoint later = wf_impl_timepoint_in_msec(42);
|
||||
|
||||
ASSERT_LT(now, later);
|
||||
ASSERT_LT(later, now + 500);
|
||||
}
|
||||
|
||||
TEST(wf_impl_timepoint, elapsed)
|
||||
{
|
||||
wf_impl_timepoint now;
|
||||
|
||||
now = wf_impl_timepoint_now();
|
||||
ASSERT_TRUE(wf_impl_timepoint_is_elapsed(now - 1));
|
||||
|
||||
now =wf_impl_timepoint_now();
|
||||
ASSERT_FALSE(wf_impl_timepoint_is_elapsed(now + 500));
|
||||
}
|
@ -1,149 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "webfuse/utils/msleep.hpp"
|
||||
#include "webfuse/adapter/impl/time/timer.h"
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
|
||||
using std::size_t;
|
||||
using webfuse_test::msleep;
|
||||
|
||||
namespace
|
||||
{
|
||||
void on_timeout(struct wf_impl_timer * timer)
|
||||
{
|
||||
bool * triggered = reinterpret_cast<bool*>(timer->user_data);
|
||||
*triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(timer, init)
|
||||
{
|
||||
struct wf_impl_timeout_manager manager;
|
||||
struct wf_impl_timer timer;
|
||||
|
||||
wf_impl_timeout_manager_init(&manager);
|
||||
wf_impl_timer_init(&timer, &manager);
|
||||
|
||||
wf_impl_timer_cleanup(&timer);
|
||||
wf_impl_timeout_manager_cleanup(&manager);
|
||||
}
|
||||
|
||||
TEST(timer, trigger)
|
||||
{
|
||||
struct wf_impl_timeout_manager manager;
|
||||
struct wf_impl_timer timer;
|
||||
|
||||
wf_impl_timeout_manager_init(&manager);
|
||||
wf_impl_timer_init(&timer, &manager);
|
||||
|
||||
bool triggered = false;
|
||||
wf_impl_timer_start(&timer, wf_impl_timepoint_in_msec(250), &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
msleep(500);
|
||||
wf_impl_timeout_manager_check(&manager);
|
||||
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_impl_timer_cleanup(&timer);
|
||||
wf_impl_timeout_manager_cleanup(&manager);
|
||||
}
|
||||
|
||||
TEST(timer, trigger_on_cleanup)
|
||||
{
|
||||
struct wf_impl_timeout_manager manager;
|
||||
struct wf_impl_timer timer;
|
||||
|
||||
wf_impl_timeout_manager_init(&manager);
|
||||
wf_impl_timer_init(&timer, &manager);
|
||||
|
||||
bool triggered = false;
|
||||
wf_impl_timer_start(&timer, wf_impl_timepoint_in_msec(5 * 60 * 1000), &on_timeout, reinterpret_cast<void*>(&triggered));
|
||||
|
||||
wf_impl_timeout_manager_cleanup(&manager);
|
||||
ASSERT_TRUE(triggered);
|
||||
|
||||
wf_impl_timer_cleanup(&timer);
|
||||
}
|
||||
|
||||
TEST(timer, cancel)
|
||||
{
|
||||
struct wf_impl_timeout_manager manager;
|
||||
struct wf_impl_timer timer;
|
||||
|
||||
wf_impl_timeout_manager_init(&manager);
|
||||
wf_impl_timer_init(&timer, &manager);
|
||||
|
||||
bool triggered = false;
|
||||
wf_impl_timer_start(&timer, wf_impl_timepoint_in_msec(250), &on_timeout, &triggered);
|
||||
msleep(500);
|
||||
wf_impl_timer_cancel(&timer);
|
||||
wf_impl_timeout_manager_check(&manager);
|
||||
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
wf_impl_timer_cleanup(&timer);
|
||||
wf_impl_timeout_manager_cleanup(&manager);
|
||||
}
|
||||
|
||||
TEST(timer, cancel_multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_impl_timeout_manager manager;
|
||||
struct wf_impl_timer timer[count];
|
||||
|
||||
wf_impl_timeout_manager_init(&manager);
|
||||
|
||||
bool triggered = false;
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_impl_timer_init(&timer[i], &manager);
|
||||
wf_impl_timer_start(&timer[i], wf_impl_timepoint_in_msec(0), &on_timeout, &triggered);
|
||||
}
|
||||
|
||||
msleep(10);
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_impl_timer_cancel(&timer[i]);
|
||||
}
|
||||
|
||||
wf_impl_timeout_manager_check(&manager);
|
||||
ASSERT_FALSE(triggered);
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_impl_timer_cleanup(&timer[0]);
|
||||
}
|
||||
wf_impl_timeout_manager_cleanup(&manager);
|
||||
}
|
||||
|
||||
TEST(timer, multiple_timers)
|
||||
{
|
||||
static size_t const count = 5;
|
||||
struct wf_impl_timeout_manager manager;
|
||||
struct wf_impl_timer timer[count];
|
||||
bool triggered[count];
|
||||
|
||||
wf_impl_timeout_manager_init(&manager);
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
wf_impl_timer_init(&timer[i], &manager);
|
||||
triggered[i] = false;
|
||||
wf_impl_timer_start(&timer[i], wf_impl_timepoint_in_msec(300 - (50 * i)), &on_timeout, &triggered[i]);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
msleep(100);
|
||||
wf_impl_timeout_manager_check(&manager);
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < count; i++)
|
||||
{
|
||||
ASSERT_TRUE(triggered[i]);
|
||||
wf_impl_timer_cleanup(&timer[i]);
|
||||
}
|
||||
|
||||
wf_impl_timeout_manager_cleanup(&manager);
|
||||
}
|
Loading…
Reference in new issue