2006-06-26 00:48:02 +00:00
|
|
|
/*
|
|
|
|
* Task management functions.
|
|
|
|
*
|
2007-04-29 08:41:56 +00:00
|
|
|
* Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
|
2006-06-26 00:48:02 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#include <common/config.h>
|
|
|
|
#include <common/mini-clist.h>
|
2007-04-29 08:41:56 +00:00
|
|
|
#include <common/standard.h>
|
2007-04-28 20:40:08 +00:00
|
|
|
#include <common/time.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
#include <proto/task.h>
|
2007-04-29 08:41:56 +00:00
|
|
|
#include <types/task.h>
|
|
|
|
|
|
|
|
// FIXME: check 8bitops.c for faster FLS
|
|
|
|
#include <import/bitops.h>
|
|
|
|
#include <import/tree.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* FIXME : this should be removed very quickly ! */
|
|
|
|
extern int maintain_proxies(void);
|
|
|
|
|
|
|
|
void **pool_task= NULL;
|
2007-04-29 08:41:56 +00:00
|
|
|
void **pool_tree64 = NULL;
|
|
|
|
static struct ultree *stack[LLONGBITS];
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2007-04-29 08:41:56 +00:00
|
|
|
UL2TREE_HEAD(timer_wq);
|
|
|
|
void *eternity_queue = NULL;
|
|
|
|
void *run_queue = NULL;
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2007-04-29 08:41:56 +00:00
|
|
|
struct ultree *ul2tree_insert(struct ultree *root, unsigned long h, unsigned long l)
|
2007-01-06 23:38:00 +00:00
|
|
|
{
|
2007-04-29 08:41:56 +00:00
|
|
|
return __ul2tree_insert(root, h, l);
|
2007-01-06 23:38:00 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 08:41:56 +00:00
|
|
|
void *tree_delete(void *node) {
|
|
|
|
return __tree_delete(node);
|
2007-01-06 23:38:00 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 08:41:56 +00:00
|
|
|
/*
|
|
|
|
* task_queue()
|
|
|
|
*
|
|
|
|
* Inserts a task into the wait queue at the position given by its expiration
|
|
|
|
* date.
|
|
|
|
*
|
|
|
|
*/
|
2006-06-26 00:48:02 +00:00
|
|
|
struct task *task_queue(struct task *task)
|
|
|
|
{
|
2007-04-29 08:41:56 +00:00
|
|
|
if (unlikely(task->qlist.p != NULL)) {
|
|
|
|
DLIST_DEL(&task->qlist);
|
|
|
|
task->qlist.p = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(task->wq)) {
|
|
|
|
tree_delete(task->wq);
|
|
|
|
task->wq = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(tv_iseternity(&task->expire))) {
|
|
|
|
task->wq = NULL;
|
|
|
|
DLIST_ADD(eternity_queue, &task->qlist);
|
2007-01-06 23:38:00 +00:00
|
|
|
return task;
|
2007-04-29 08:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
task->wq = ul2tree_insert(&timer_wq, task->expire.tv_sec, task->expire.tv_usec);
|
|
|
|
DLIST_ADD(task->wq->data, &task->qlist);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract all expired timers from the wait queue, and wakes up all
|
|
|
|
* associated tasks.
|
|
|
|
* Returns the time to wait for next task (next_time).
|
|
|
|
*
|
|
|
|
* FIXME: Use an alternative queue for ETERNITY tasks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int wake_expired_tasks()
|
|
|
|
{
|
|
|
|
int slen;
|
|
|
|
struct task *task;
|
|
|
|
void *data;
|
|
|
|
int next_time;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hint: tasks are *rarely* expired. So we can try to optimize
|
|
|
|
* by not scanning the tree at all in most cases.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (likely(timer_wq.data != NULL)) {
|
|
|
|
task = LIST_ELEM(timer_wq.data, struct task *, qlist);
|
2007-04-29 15:43:56 +00:00
|
|
|
if (likely(__tv_isge(&task->expire, &now) > 0))
|
|
|
|
return tv_ms_remain(&now, &task->expire);
|
2007-04-29 08:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* OK we lose. Let's scan the tree then. */
|
|
|
|
next_time = TIME_ETERNITY;
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2007-04-29 08:41:56 +00:00
|
|
|
tree64_foreach(&timer_wq, data, stack, slen) {
|
|
|
|
task = LIST_ELEM(data, struct task *, qlist);
|
|
|
|
|
2007-04-29 15:43:56 +00:00
|
|
|
if (__tv_isgt(&task->expire, &now)) {
|
|
|
|
next_time = tv_ms_remain(&now, &task->expire);
|
2007-04-29 08:41:56 +00:00
|
|
|
break;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 08:41:56 +00:00
|
|
|
/*
|
|
|
|
* OK, all tasks linked to this node will be unlinked, as well
|
|
|
|
* as the node itself, so we do not need to care about correct
|
|
|
|
* unlinking.
|
|
|
|
*/
|
|
|
|
foreach_dlist_item(task, data, struct task *, qlist) {
|
|
|
|
DLIST_DEL(&task->qlist);
|
|
|
|
task->wq = NULL;
|
|
|
|
DLIST_ADD(run_queue, &task->qlist);
|
|
|
|
task->state = TASK_RUNNING;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-29 08:41:56 +00:00
|
|
|
return next_time;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This does 4 things :
|
|
|
|
* - wake up all expired tasks
|
|
|
|
* - call all runnable tasks
|
|
|
|
* - call maintain_proxies() to enable/disable the listeners
|
|
|
|
* - return the delay till next event in ms, -1 = wait indefinitely
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int process_runnable_tasks()
|
|
|
|
{
|
|
|
|
int next_time;
|
|
|
|
int time2;
|
2007-01-06 23:38:00 +00:00
|
|
|
struct task *t;
|
2007-04-29 08:41:56 +00:00
|
|
|
void *queue;
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2007-04-29 08:41:56 +00:00
|
|
|
next_time = wake_expired_tasks();
|
2006-06-26 00:48:02 +00:00
|
|
|
/* process each task in the run queue now. Each task may be deleted
|
|
|
|
* since we only use the run queue's head. Note that any task can be
|
|
|
|
* woken up by any other task and it will be processed immediately
|
2007-01-06 23:38:00 +00:00
|
|
|
* after as it will be queued on the run queue's head !
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
2007-04-29 08:41:56 +00:00
|
|
|
|
|
|
|
queue = run_queue;
|
|
|
|
foreach_dlist_item(t, queue, struct task *, qlist) {
|
2006-06-26 00:48:02 +00:00
|
|
|
int temp_time;
|
|
|
|
|
2007-04-29 08:41:56 +00:00
|
|
|
DLIST_DEL(&t->qlist);
|
|
|
|
t->qlist.p = NULL;
|
|
|
|
|
|
|
|
t->state = TASK_IDLE;
|
2006-06-26 00:48:02 +00:00
|
|
|
temp_time = t->process(t);
|
|
|
|
next_time = MINTIME(temp_time, next_time);
|
|
|
|
}
|
2007-01-06 23:38:00 +00:00
|
|
|
|
|
|
|
/* maintain all proxies in a consistent state. This should quickly
|
|
|
|
* become a task because it becomes expensive when there are huge
|
|
|
|
* numbers of proxies. */
|
2006-06-26 00:48:02 +00:00
|
|
|
time2 = maintain_proxies();
|
|
|
|
return MINTIME(time2, next_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|