mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
renamed to webfuse
This commit is contained in:
84
lib/webfuse/adapter/impl/time/timeout_manager.c
Normal file
84
lib/webfuse/adapter/impl/time/timeout_manager.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#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;
|
||||
}
|
||||
}
|
||||
|
||||
29
lib/webfuse/adapter/impl/time/timeout_manager.h
Normal file
29
lib/webfuse/adapter/impl/time/timeout_manager.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#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
|
||||
24
lib/webfuse/adapter/impl/time/timeout_manager_intern.h
Normal file
24
lib/webfuse/adapter/impl/time/timeout_manager_intern.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#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
|
||||
31
lib/webfuse/adapter/impl/time/timepoint.c
Normal file
31
lib/webfuse/adapter/impl/time/timepoint.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#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);
|
||||
}
|
||||
31
lib/webfuse/adapter/impl/time/timepoint.h
Normal file
31
lib/webfuse/adapter/impl/time/timepoint.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#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
|
||||
65
lib/webfuse/adapter/impl/time/timer.c
Normal file
65
lib/webfuse/adapter/impl/time/timer.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
48
lib/webfuse/adapter/impl/time/timer.h
Normal file
48
lib/webfuse/adapter/impl/time/timer.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#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
|
||||
25
lib/webfuse/adapter/impl/time/timer_intern.h
Normal file
25
lib/webfuse/adapter/impl/time/timer_intern.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user