mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
refactor: merged code structure
This commit is contained in:
93
lib/webfuse_provider/impl/timer/manager.c
Normal file
93
lib/webfuse_provider/impl/timer/manager.c
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "webfuse_provider/impl/timer/manager_intern.h"
|
||||
#include "webfuse_provider/impl/timer/timer_intern.h"
|
||||
#include "webfuse_provider/impl/timer/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct wf_timer_manager
|
||||
{
|
||||
struct wf_timer * timers;
|
||||
};
|
||||
|
||||
struct wf_timer_manager *
|
||||
wf_timer_manager_create(void)
|
||||
{
|
||||
struct wf_timer_manager * manager = malloc(sizeof(struct wf_timer_manager));
|
||||
manager->timers = NULL;
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager)
|
||||
{
|
||||
struct wf_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wf_timer * next = timer->next;
|
||||
|
||||
wf_timer_trigger(timer);
|
||||
timer = next;
|
||||
}
|
||||
|
||||
free(manager);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_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_is_timeout(timer))
|
||||
{
|
||||
wf_timer_manager_removetimer(manager, timer);
|
||||
wf_timer_trigger(timer);
|
||||
}
|
||||
|
||||
timer = next;
|
||||
}
|
||||
}
|
||||
|
||||
void wf_timer_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_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;
|
||||
}
|
||||
}
|
||||
|
||||
27
lib/webfuse_provider/impl/timer/manager.h
Normal file
27
lib/webfuse_provider/impl/timer/manager.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef WF_TIMER_MANAGER_H
|
||||
#define WF_TIMER_MANAGER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern struct wf_timer_manager *
|
||||
wf_timer_manager_create(void);
|
||||
|
||||
extern void
|
||||
wf_timer_manager_dispose(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
extern void
|
||||
wf_timer_manager_check(
|
||||
struct wf_timer_manager * manager);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
26
lib/webfuse_provider/impl/timer/manager_intern.h
Normal file
26
lib/webfuse_provider/impl/timer/manager_intern.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef WF_TIMER_MANAGER_INTERN_H
|
||||
#define WF_TIMER_MANAGER_INTERN_H
|
||||
|
||||
#include "webfuse_provider/impl/timer/manager.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
|
||||
extern void wf_timer_manager_addtimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_manager_removetimer(
|
||||
struct wf_timer_manager * manager,
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
19
lib/webfuse_provider/impl/timer/on_timer_fn.h
Normal file
19
lib/webfuse_provider/impl/timer/on_timer_fn.h
Normal file
@@ -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
|
||||
31
lib/webfuse_provider/impl/timer/timepoint.c
Normal file
31
lib/webfuse_provider/impl/timer/timepoint.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "webfuse_provider/impl/timer/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_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_timepoint_in_msec(wf_timer_timediff value)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_timepoint_now();
|
||||
wf_timer_timepoint result = now + ((wf_timer_timepoint) value);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool wf_timer_timepoint_is_elapsed(wf_timer_timepoint tp)
|
||||
{
|
||||
wf_timer_timepoint const now = wf_timer_timepoint_now();
|
||||
wf_timer_timediff const diff = (wf_timer_timediff) (tp - now);
|
||||
|
||||
return (0 > diff);
|
||||
}
|
||||
31
lib/webfuse_provider/impl/timer/timepoint.h
Normal file
31
lib/webfuse_provider/impl/timer/timepoint.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef WF_TIMER_TIMEPOINT_H
|
||||
#define WF_TIMER_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_timepoint_now(void);
|
||||
|
||||
extern wf_timer_timepoint wf_timer_timepoint_in_msec(
|
||||
wf_timer_timediff value);
|
||||
|
||||
extern bool wf_timer_timepoint_is_elapsed(
|
||||
wf_timer_timepoint timepoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
67
lib/webfuse_provider/impl/timer/timer.c
Normal file
67
lib/webfuse_provider/impl/timer/timer.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "webfuse_provider/impl/timer/timer_intern.h"
|
||||
#include "webfuse_provider/impl/timer/manager_intern.h"
|
||||
#include "webfuse_provider/impl/timer/timepoint.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_timer *
|
||||
wf_timer_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_dispose(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
free(timer);
|
||||
}
|
||||
|
||||
void wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms)
|
||||
{
|
||||
timer->timeout = wf_timer_timepoint_in_msec(timeout_ms);
|
||||
|
||||
wf_timer_manager_addtimer(timer->manager, timer);
|
||||
}
|
||||
|
||||
void wf_timer_cancel(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
wf_timer_manager_removetimer(timer->manager, timer);
|
||||
|
||||
timer->timeout = 0;
|
||||
}
|
||||
|
||||
bool wf_timer_is_timeout(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
return wf_timer_timepoint_is_elapsed(timer->timeout);
|
||||
}
|
||||
|
||||
|
||||
void wf_timer_trigger(
|
||||
struct wf_timer * timer)
|
||||
{
|
||||
if (0 != timer->on_timer)
|
||||
{
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
timer->on_timer(timer, timer->user_data);
|
||||
}
|
||||
}
|
||||
37
lib/webfuse_provider/impl/timer/timer.h
Normal file
37
lib/webfuse_provider/impl/timer/timer.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef WF_TIMER_TIMER_H
|
||||
#define WF_TIMER_TIMER_H
|
||||
|
||||
#include "webfuse_provider/impl/timer/on_timer_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wf_timer;
|
||||
struct wf_timer_manager;
|
||||
|
||||
extern struct wf_timer *
|
||||
wf_timer_create(
|
||||
struct wf_timer_manager * manager,
|
||||
wf_timer_on_timer_fn * on_timer,
|
||||
void * user_data);
|
||||
|
||||
extern void
|
||||
wf_timer_dispose(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void
|
||||
wf_timer_start(
|
||||
struct wf_timer * timer,
|
||||
int timeout_ms);
|
||||
|
||||
extern void
|
||||
wf_timer_cancel(
|
||||
struct wf_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
40
lib/webfuse_provider/impl/timer/timer_intern.h
Normal file
40
lib/webfuse_provider/impl/timer/timer_intern.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef WF_TIMER_TIMER_H
|
||||
#define WF_TIMER_TIMER_H
|
||||
|
||||
#include "webfuse_provider/impl/timer/timer.h"
|
||||
#include "webfuse_provider/impl/timer/on_timer_fn.h"
|
||||
#include "webfuse_provider/impl/timer/timepoint.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
extern bool wf_timer_is_timeout(
|
||||
struct wf_timer * timer);
|
||||
|
||||
extern void wf_timer_trigger(
|
||||
struct wf_timer * timer);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user