Reindented server/core/housekeeper.c

This commit is contained in:
Johan Wikman
2015-11-30 13:53:06 +02:00
parent 486f724dc1
commit 3189a47fc5
2 changed files with 215 additions and 198 deletions

View File

@ -80,7 +80,8 @@ hkinit()
* @param taskfn The function to call for the task * @param taskfn The function to call for the task
* @param data Data to pass to the task function * @param data Data to pass to the task function
* @param frequency How often to run the task, expressed in seconds * @param frequency How often to run the task, expressed in seconds
* @return Return the time in seconds when the task will be first run if the task was added, otherwise 0 * @return Return the time in seconds when the task will be first run
* if the task was added, otherwise 0
*/ */
int int
hktask_add(char *name, void (*taskfn)(void *), void *data, int frequency) hktask_add(char *name, void (*taskfn)(void *), void *data, int frequency)
@ -144,7 +145,8 @@ HKTASK *task, *ptr;
* @param taskfn The function to call for the task * @param taskfn The function to call for the task
* @param data Data to pass to the task function * @param data Data to pass to the task function
* @param when How many second until the task is executed * @param when How many second until the task is executed
* @return Return the time in seconds when the task will be first run if the task was added, otherwise 0 * @return Return the time in seconds when the task will be first run
* if the task was added, otherwise 0
* *
*/ */
int int
@ -181,9 +183,13 @@ HKTASK *task, *ptr;
ptr = ptr->next; ptr = ptr->next;
} }
if (ptr) if (ptr)
{
ptr->next = task; ptr->next = task;
}
else else
{
tasks = task; tasks = task;
}
spinlock_release(&tasklock); spinlock_release(&tasklock);
return task->nextdue; return task->nextdue;
@ -209,9 +215,13 @@ HKTASK *ptr, *lptr = NULL;
ptr = ptr->next; ptr = ptr->next;
} }
if (ptr && lptr) if (ptr && lptr)
{
lptr->next = ptr->next; lptr->next = ptr->next;
}
else if (ptr) else if (ptr)
{
tasks = ptr->next; tasks = ptr->next;
}
spinlock_release(&tasklock); spinlock_release(&tasklock);
if (ptr) if (ptr)
@ -256,7 +266,9 @@ int i;
for (i = 0; i < 10; i++) for (i = 0; i < 10; i++)
{ {
if (do_shutdown) if (do_shutdown)
{
return; return;
}
thread_millisleep(100); thread_millisleep(100);
hkheartbeat++; hkheartbeat++;
} }
@ -273,13 +285,17 @@ int i;
spinlock_release(&tasklock); spinlock_release(&tasklock);
(*taskfn)(taskdata); (*taskfn)(taskdata);
if (ptr->type == HK_ONESHOT) if (ptr->type == HK_ONESHOT)
{
hktask_remove(ptr->name); hktask_remove(ptr->name);
}
spinlock_acquire(&tasklock); spinlock_acquire(&tasklock);
ptr = tasks; ptr = tasks;
} }
else else
{
ptr = ptr->next; ptr = ptr->next;
} }
}
spinlock_release(&tasklock); spinlock_release(&tasklock);
} }
} }

View File

@ -32,7 +32,8 @@
* @endverbatim * @endverbatim
*/ */
typedef enum { typedef enum
{
HK_REPEATED = 1, HK_REPEATED = 1,
HK_ONESHOT HK_ONESHOT
} HKTASK_TYPE; } HKTASK_TYPE;
@ -40,16 +41,15 @@ typedef enum {
/** /**
* The housekeeper task list * The housekeeper task list
*/ */
typedef struct hktask { typedef struct hktask
{
char *name; /*< A simple task name */ char *name; /*< A simple task name */
void (*task)(void *data); /*< The task to call */ void (*task)(void *data); /*< The task to call */
void *data; /*< Data to pass the task */ void *data; /*< Data to pass the task */
int frequency; /*< How often to call the tasks (seconds) */ int frequency; /*< How often to call the tasks (seconds) */
time_t nextdue; /*< When the task should be next run */ time_t nextdue; /*< When the task should be next run */
HKTASK_TYPE HKTASK_TYPE type; /*< The task type */
type; /*< The task type */ struct hktask *next; /*< Next task in the list */
struct hktask
*next; /*< Next task in the list */
} HKTASK; } HKTASK;
extern void hkinit(); extern void hkinit();
@ -58,4 +58,5 @@ extern int hktask_oneshot(char *name, void (*task)(void *), void *data, int whe
extern int hktask_remove(char *name); extern int hktask_remove(char *name);
extern void hkshutdown(); extern void hkshutdown();
extern void hkshow_tasks(DCB *pdcb); extern void hkshow_tasks(DCB *pdcb);
#endif #endif