mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
reorganized project: prepared to extract common functionality
This commit is contained in:
84
lib/wsfs/adapter/time/timeout_manager.c
Normal file
84
lib/wsfs/adapter/time/timeout_manager.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "wsfs/adapter/time/timeout_manager_intern.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include "wsfs/adapter/time/timer_intern.h"
|
||||
#include "wsfs/adapter/time/timepoint.h"
|
||||
|
||||
void wsfs_timeout_manager_init(
|
||||
struct wsfs_timeout_manager * manager)
|
||||
{
|
||||
manager->timers = NULL;
|
||||
}
|
||||
|
||||
void wsfs_timeout_manager_cleanup(
|
||||
struct wsfs_timeout_manager * manager)
|
||||
{
|
||||
struct wsfs_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wsfs_timer * next = timer->next;
|
||||
|
||||
wsfs_timer_trigger(timer);
|
||||
|
||||
timer = next;
|
||||
}
|
||||
|
||||
manager->timers = NULL;
|
||||
|
||||
}
|
||||
|
||||
void wsfs_timeout_manager_check(
|
||||
struct wsfs_timeout_manager * manager)
|
||||
{
|
||||
struct wsfs_timer * timer = manager->timers;
|
||||
while (NULL != timer)
|
||||
{
|
||||
struct wsfs_timer * next = timer->next;
|
||||
|
||||
if (wsfs_timer_is_timeout(timer))
|
||||
{
|
||||
wsfs_timeout_manager_removetimer(manager, timer);
|
||||
wsfs_timer_trigger(timer);
|
||||
}
|
||||
|
||||
timer = next;
|
||||
}
|
||||
}
|
||||
|
||||
void wsfs_timeout_manager_addtimer(
|
||||
struct wsfs_timeout_manager * manager,
|
||||
struct wsfs_timer * timer)
|
||||
{
|
||||
if (NULL != manager->timers)
|
||||
{
|
||||
manager->timers->prev = timer;
|
||||
}
|
||||
|
||||
timer->next = manager->timers;
|
||||
timer->prev = NULL;
|
||||
manager->timers = timer;
|
||||
}
|
||||
|
||||
void wsfs_timeout_manager_removetimer(
|
||||
struct wsfs_timeout_manager * manager,
|
||||
struct wsfs_timer * timer)
|
||||
{
|
||||
struct wsfs_timer * prev = timer->prev;
|
||||
struct wsfs_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/wsfs/adapter/time/timeout_manager.h
Normal file
29
lib/wsfs/adapter/time/timeout_manager.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef WSFS_TIME_TIMEOUT_MANAGER_H
|
||||
#define WSFS_TIME_TIMEOUT_MANAGER_H
|
||||
|
||||
struct wsfs_timer;
|
||||
struct wsfs_timeout_manager
|
||||
{
|
||||
struct wsfs_timer * timers;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfs_timeout_manager_init(
|
||||
struct wsfs_timeout_manager * manager);
|
||||
|
||||
extern void wsfs_timeout_manager_cleanup(
|
||||
struct wsfs_timeout_manager * manager);
|
||||
|
||||
extern void wsfs_timeout_manager_check(
|
||||
struct wsfs_timeout_manager * manager);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
24
lib/wsfs/adapter/time/timeout_manager_intern.h
Normal file
24
lib/wsfs/adapter/time/timeout_manager_intern.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef WSFS_TIME_TIMEOUT_MANAGER_INTERN_H
|
||||
#define WSFS_TIME_TIMEOUT_MANAGER_INTERN_H
|
||||
|
||||
#include "wsfs/adapter/time/timeout_manager.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfs_timeout_manager_addtimer(
|
||||
struct wsfs_timeout_manager * manager,
|
||||
struct wsfs_timer * timer);
|
||||
|
||||
extern void wsfs_timeout_manager_removetimer(
|
||||
struct wsfs_timeout_manager * manager,
|
||||
struct wsfs_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
31
lib/wsfs/adapter/time/timepoint.c
Normal file
31
lib/wsfs/adapter/time/timepoint.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "wsfs/adapter/time/timepoint.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#define WSFS_MSEC_PER_SEC ((wsfs_timepoint) 1000)
|
||||
#define WSFS_NSEC_PER_MSEC ((wsfs_timepoint) 1000 * 1000)
|
||||
|
||||
wsfs_timepoint wsfs_timepoint_now(void)
|
||||
{
|
||||
struct timespec timepoint;
|
||||
clock_gettime(CLOCK_MONOTONIC, &timepoint);
|
||||
|
||||
wsfs_timepoint const now = (timepoint.tv_sec * WSFS_MSEC_PER_SEC) + (timepoint.tv_nsec / WSFS_NSEC_PER_MSEC);
|
||||
return now;
|
||||
}
|
||||
|
||||
wsfs_timepoint wsfs_timepoint_in_msec(wsfs_timediff value)
|
||||
{
|
||||
wsfs_timepoint const now = wsfs_timepoint_now();
|
||||
wsfs_timepoint result = now + ((wsfs_timepoint) value);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool wsfs_timepoint_is_elapsed(wsfs_timepoint timepoint)
|
||||
{
|
||||
wsfs_timepoint const now = wsfs_timepoint_now();
|
||||
wsfs_timediff const diff = (wsfs_timediff) (timepoint - now);
|
||||
|
||||
return (0 > diff);
|
||||
}
|
||||
31
lib/wsfs/adapter/time/timepoint.h
Normal file
31
lib/wsfs/adapter/time/timepoint.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef WSFS_TIME_TIMEPOINT_H
|
||||
#define WSFS_TIME_TIMEPOINT_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
#else
|
||||
#include <cinttypes>
|
||||
#endif
|
||||
|
||||
typedef uint64_t wsfs_timepoint;
|
||||
typedef int64_t wsfs_timediff;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern wsfs_timepoint wsfs_timepoint_now(void);
|
||||
|
||||
extern wsfs_timepoint wsfs_timepoint_in_msec(
|
||||
wsfs_timediff value);
|
||||
|
||||
extern bool wsfs_timepoint_is_elapsed(
|
||||
wsfs_timepoint timepoint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
65
lib/wsfs/adapter/time/timer.c
Normal file
65
lib/wsfs/adapter/time/timer.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "wsfs/adapter/time/timer_intern.h"
|
||||
#include "wsfs/adapter/time/timeout_manager_intern.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
void wsfs_timer_init(
|
||||
struct wsfs_timer * timer,
|
||||
struct wsfs_timeout_manager * manager)
|
||||
{
|
||||
timer->manager = manager;
|
||||
timer->timeout = 0;
|
||||
timer->timeout_handler = NULL;
|
||||
timer->user_data = NULL;
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
}
|
||||
|
||||
void wsfs_timer_cleanup(
|
||||
struct wsfs_timer * timer)
|
||||
{
|
||||
memset(timer, 0, sizeof(struct wsfs_timer));
|
||||
}
|
||||
|
||||
void wsfs_timer_start(
|
||||
struct wsfs_timer * timer,
|
||||
wsfs_timepoint absolute_timeout,
|
||||
wsfs_timer_timeout_fn * handler,
|
||||
void * user_data)
|
||||
{
|
||||
timer->timeout = absolute_timeout;
|
||||
timer->timeout_handler = handler;
|
||||
timer->user_data = user_data;
|
||||
|
||||
wsfs_timeout_manager_addtimer(timer->manager, timer);
|
||||
}
|
||||
|
||||
void wsfs_timer_cancel(
|
||||
struct wsfs_timer * timer)
|
||||
{
|
||||
wsfs_timeout_manager_removetimer(timer->manager, timer);
|
||||
|
||||
timer->timeout = 0;
|
||||
timer->timeout_handler = NULL;
|
||||
timer->user_data = NULL;
|
||||
}
|
||||
|
||||
bool wsfs_timer_is_timeout(
|
||||
struct wsfs_timer * timer)
|
||||
{
|
||||
return wsfs_timepoint_is_elapsed(timer->timeout);
|
||||
}
|
||||
|
||||
|
||||
void wsfs_timer_trigger(
|
||||
struct wsfs_timer * timer)
|
||||
{
|
||||
if (NULL != timer->timeout_handler)
|
||||
{
|
||||
timer->prev = NULL;
|
||||
timer->next = NULL;
|
||||
|
||||
timer->timeout_handler(timer);
|
||||
}
|
||||
}
|
||||
48
lib/wsfs/adapter/time/timer.h
Normal file
48
lib/wsfs/adapter/time/timer.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef WSFS_TIMER_H
|
||||
#define WSFS_TIMER_H
|
||||
|
||||
#include "wsfs/adapter/time/timepoint.h"
|
||||
|
||||
struct wsfs_timer;
|
||||
struct wsfs_timeout_manager;
|
||||
|
||||
typedef void wsfs_timer_timeout_fn(struct wsfs_timer * timer);
|
||||
|
||||
struct wsfs_timer
|
||||
{
|
||||
struct wsfs_timeout_manager * manager;
|
||||
wsfs_timepoint timeout;
|
||||
wsfs_timer_timeout_fn * timeout_handler;
|
||||
void * user_data;
|
||||
struct wsfs_timer * next;
|
||||
struct wsfs_timer * prev;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfs_timer_init(
|
||||
struct wsfs_timer * timer,
|
||||
struct wsfs_timeout_manager * manager);
|
||||
|
||||
extern void wsfs_timer_cleanup(
|
||||
struct wsfs_timer * timer);
|
||||
|
||||
extern void wsfs_timer_start(
|
||||
struct wsfs_timer * timer,
|
||||
wsfs_timepoint absolute_timeout,
|
||||
wsfs_timer_timeout_fn * handler,
|
||||
void * user_data);
|
||||
|
||||
extern void wsfs_timer_cancel(
|
||||
struct wsfs_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
25
lib/wsfs/adapter/time/timer_intern.h
Normal file
25
lib/wsfs/adapter/time/timer_intern.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef WSFS_TIME_TIMER_INTERN_H
|
||||
#define WSFS_TIME_TIMER_INTERN_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include "wsfs/adapter/time/timer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern bool wsfs_timer_is_timeout(
|
||||
struct wsfs_timer * timer);
|
||||
|
||||
extern void wsfs_timer_trigger(
|
||||
struct wsfs_timer * timer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user