2006-06-26 00:48:02 +00:00
|
|
|
/*
|
|
|
|
include/proto/task.h
|
|
|
|
Functions for task management.
|
|
|
|
|
2009-03-07 16:25:21 +00:00
|
|
|
Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation, version 2.1
|
|
|
|
exclusively.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _PROTO_TASK_H
|
|
|
|
#define _PROTO_TASK_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
2006-06-29 16:54:54 +00:00
|
|
|
|
|
|
|
#include <common/config.h>
|
2009-03-21 11:51:40 +00:00
|
|
|
#include <common/eb32tree.h>
|
2006-06-29 15:53:05 +00:00
|
|
|
#include <common/memory.h>
|
2007-04-29 08:41:56 +00:00
|
|
|
#include <common/mini-clist.h>
|
|
|
|
#include <common/standard.h>
|
2009-03-08 14:53:06 +00:00
|
|
|
#include <common/ticks.h>
|
2007-04-29 08:41:56 +00:00
|
|
|
|
2006-06-29 16:54:54 +00:00
|
|
|
#include <types/task.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2009-03-08 14:53:06 +00:00
|
|
|
/* Principle of the wait queue.
|
|
|
|
*
|
|
|
|
* We want to be able to tell whether an expiration date is before of after the
|
|
|
|
* current time <now>. We KNOW that expiration dates are never too far apart,
|
|
|
|
* because they are measured in ticks (milliseconds). We also know that almost
|
|
|
|
* all dates will be in the future, and that a very small part of them will be
|
|
|
|
* in the past, they are the ones which have expired since last time we checked
|
|
|
|
* them. Using ticks, we know if a date is in the future or in the past, but we
|
|
|
|
* cannot use that to store sorted information because that reference changes
|
|
|
|
* all the time.
|
|
|
|
*
|
2009-03-21 09:01:42 +00:00
|
|
|
* We'll use the fact that the time wraps to sort timers. Timers above <now>
|
|
|
|
* are in the future, timers below <now> are in the past. Here, "above" and
|
|
|
|
* "below" are to be considered modulo 2^31.
|
2009-03-08 14:53:06 +00:00
|
|
|
*
|
2009-03-21 09:01:42 +00:00
|
|
|
* Timers are stored sorted in an ebtree. We use the new ability for ebtrees to
|
|
|
|
* lookup values starting from X to only expire tasks between <now> - 2^31 and
|
|
|
|
* <now>. If the end of the tree is reached while walking over it, we simply
|
|
|
|
* loop back to the beginning. That way, we have no problem keeping sorted
|
|
|
|
* wrapping timers in a tree, between (now - 24 days) and (now + 24 days). The
|
|
|
|
* keys in the tree always reflect their real position, none can be infinite.
|
|
|
|
* This reduces the number of checks to be performed.
|
2009-03-08 14:53:06 +00:00
|
|
|
*
|
|
|
|
* Another nice optimisation is to allow a timer to stay at an old place in the
|
|
|
|
* queue as long as it's not further than the real expiration date. That way,
|
|
|
|
* we use the tree as a place holder for a minorant of the real expiration
|
|
|
|
* date. Since we have a very low chance of hitting a timeout anyway, we can
|
|
|
|
* bounce the nodes to their right place when we scan the tree if we encounter
|
|
|
|
* a misplaced node once in a while. This even allows us not to remove the
|
|
|
|
* infinite timers from the wait queue.
|
|
|
|
*
|
|
|
|
* So, to summarize, we have :
|
|
|
|
* - node->key always defines current position in the wait queue
|
|
|
|
* - timer is the real expiration date (possibly infinite)
|
2009-03-21 09:01:42 +00:00
|
|
|
* - node->key is always before or equal to timer
|
2009-03-08 14:53:06 +00:00
|
|
|
*
|
|
|
|
* The run queue works similarly to the wait queue except that the current date
|
|
|
|
* is replaced by an insertion counter which can also wrap without any problem.
|
|
|
|
*/
|
|
|
|
|
2009-03-21 09:01:42 +00:00
|
|
|
/* The farthest we can look back in a timer tree */
|
|
|
|
#define TIMER_LOOK_BACK (1U << 31)
|
2009-03-08 14:53:06 +00:00
|
|
|
|
|
|
|
/* a few exported variables */
|
2009-03-21 17:13:21 +00:00
|
|
|
extern unsigned int nb_tasks; /* total number of tasks */
|
2008-06-30 05:51:00 +00:00
|
|
|
extern unsigned int run_queue; /* run queue size */
|
|
|
|
extern unsigned int niced_tasks; /* number of niced tasks in the run queue */
|
2007-05-13 17:43:47 +00:00
|
|
|
extern struct pool_head *pool2_task;
|
2009-03-21 11:51:40 +00:00
|
|
|
extern struct eb32_node *last_timer; /* optimization: last queued timer */
|
2007-05-13 17:43:47 +00:00
|
|
|
|
2009-03-07 16:25:21 +00:00
|
|
|
/* return 0 if task is in run queue, otherwise non-zero */
|
|
|
|
static inline int task_in_rq(struct task *t)
|
|
|
|
{
|
|
|
|
return t->rq.node.leaf_p != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return 0 if task is in wait queue, otherwise non-zero */
|
|
|
|
static inline int task_in_wq(struct task *t)
|
|
|
|
{
|
|
|
|
return t->wq.node.leaf_p != NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-29 16:19:04 +00:00
|
|
|
/* puts the task <t> in run queue with reason flags <f>, and returns <t> */
|
2008-08-29 13:26:14 +00:00
|
|
|
struct task *__task_wakeup(struct task *t);
|
2008-08-29 16:19:04 +00:00
|
|
|
static inline struct task *task_wakeup(struct task *t, unsigned int f)
|
2008-08-29 13:26:14 +00:00
|
|
|
{
|
2009-03-07 16:25:21 +00:00
|
|
|
if (likely(!task_in_rq(t)))
|
2008-08-29 16:19:04 +00:00
|
|
|
__task_wakeup(t);
|
|
|
|
t->state |= f;
|
|
|
|
return t;
|
2008-08-29 13:26:14 +00:00
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2009-03-07 16:25:21 +00:00
|
|
|
/*
|
|
|
|
* Unlink the task from the wait queue, and possibly update the last_timer
|
|
|
|
* pointer. A pointer to the task itself is returned. The task *must* already
|
|
|
|
* be in the wait queue before calling this function. If unsure, use the safer
|
|
|
|
* task_unlink_wq() function.
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
2009-03-07 16:25:21 +00:00
|
|
|
static inline struct task *__task_unlink_wq(struct task *t)
|
|
|
|
{
|
|
|
|
eb32_delete(&t->wq);
|
2009-03-21 11:51:40 +00:00
|
|
|
if (last_timer == &t->wq)
|
2009-03-07 16:25:21 +00:00
|
|
|
last_timer = NULL;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct task *task_unlink_wq(struct task *t)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
2009-03-07 16:25:21 +00:00
|
|
|
if (likely(task_in_wq(t)))
|
|
|
|
__task_unlink_wq(t);
|
2007-04-29 08:41:56 +00:00
|
|
|
return t;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-03-07 16:25:21 +00:00
|
|
|
* Unlink the task from the run queue. The run_queue size and number of niced
|
|
|
|
* tasks are updated too. A pointer to the task itself is returned. The task
|
|
|
|
* *must* already be in the wait queue before calling this function. If unsure,
|
|
|
|
* use the safer task_unlink_rq() function.
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
2009-03-07 16:25:21 +00:00
|
|
|
static inline struct task *__task_unlink_rq(struct task *t)
|
|
|
|
{
|
|
|
|
eb32_delete(&t->rq);
|
|
|
|
run_queue--;
|
|
|
|
if (likely(t->nice))
|
|
|
|
niced_tasks--;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct task *task_unlink_rq(struct task *t)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
2009-03-07 16:25:21 +00:00
|
|
|
if (likely(task_in_rq(t)))
|
|
|
|
__task_unlink_rq(t);
|
2008-07-05 16:16:19 +00:00
|
|
|
return t;
|
|
|
|
}
|
2008-06-24 06:17:16 +00:00
|
|
|
|
2008-07-05 16:16:19 +00:00
|
|
|
/*
|
|
|
|
* Unlinks the task and adjusts run queue stats.
|
|
|
|
* A pointer to the task itself is returned.
|
|
|
|
*/
|
|
|
|
static inline struct task *task_delete(struct task *t)
|
|
|
|
{
|
2009-03-07 16:25:21 +00:00
|
|
|
task_unlink_wq(t);
|
|
|
|
task_unlink_rq(t);
|
2008-06-24 06:17:16 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-03-21 17:13:21 +00:00
|
|
|
* Initialize a new task. The bare minimum is performed (queue pointers and
|
|
|
|
* state). The task is returned. This function should not be used outside of
|
|
|
|
* task_new().
|
2008-06-24 06:17:16 +00:00
|
|
|
*/
|
|
|
|
static inline struct task *task_init(struct task *t)
|
|
|
|
{
|
2009-03-07 16:25:21 +00:00
|
|
|
t->wq.node.leaf_p = NULL;
|
|
|
|
t->rq.node.leaf_p = NULL;
|
2008-08-29 16:19:04 +00:00
|
|
|
t->state = TASK_SLEEPING;
|
2008-06-30 05:51:00 +00:00
|
|
|
t->nice = 0;
|
2006-06-26 00:48:02 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-03-21 17:13:21 +00:00
|
|
|
* Allocate and initialise a new task. The new task is returned, or NULL in
|
|
|
|
* case of lack of memory. The task count is incremented. Tasks should only
|
|
|
|
* be allocated this way, and must be freed using task_free().
|
|
|
|
*/
|
|
|
|
static inline struct task *task_new(void)
|
|
|
|
{
|
|
|
|
struct task *t = pool_alloc2(pool2_task);
|
|
|
|
if (t) {
|
|
|
|
nb_tasks++;
|
|
|
|
task_init(t);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a task. Its context must have been freed since it will be lost.
|
|
|
|
* The task count is decremented.
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
|
|
|
static inline void task_free(struct task *t)
|
|
|
|
{
|
2007-05-13 17:43:47 +00:00
|
|
|
pool_free2(pool2_task, t);
|
2009-03-21 17:13:21 +00:00
|
|
|
nb_tasks--;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 16:25:21 +00:00
|
|
|
/* Place <task> into the wait queue, where it may already be. If the expiration
|
2009-03-08 15:35:27 +00:00
|
|
|
* timer is infinite, do nothing and rely on wake_expired_task to clean up.
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
2009-03-08 15:35:27 +00:00
|
|
|
void __task_queue(struct task *task);
|
|
|
|
static inline void task_queue(struct task *task)
|
|
|
|
{
|
|
|
|
/* If we already have a place in the wait queue no later than the
|
|
|
|
* timeout we're trying to set, we'll stay there, because it is very
|
|
|
|
* unlikely that we will reach the timeout anyway. If the timeout
|
|
|
|
* has been disabled, it's useless to leave the queue as well. We'll
|
|
|
|
* rely on wake_expired_tasks() to catch the node and move it to the
|
|
|
|
* proper place should it ever happen. Finally we only add the task
|
|
|
|
* to the queue if it was not there or if it was further than what
|
|
|
|
* we want.
|
|
|
|
*/
|
|
|
|
if (!tick_isset(task->expire))
|
|
|
|
return;
|
|
|
|
|
2009-03-21 09:01:42 +00:00
|
|
|
if (!task_in_wq(task) || tick_is_lt(task->expire, task->wq.key))
|
2009-03-08 15:35:27 +00:00
|
|
|
__task_queue(task);
|
|
|
|
}
|
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
|
2007-05-12 20:35:00 +00:00
|
|
|
* - return the date of next event in <next> or eternity.
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
|
|
|
|
2008-07-06 22:09:58 +00:00
|
|
|
void process_runnable_tasks(int *next);
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2008-06-29 20:40:23 +00:00
|
|
|
/*
|
|
|
|
* Extract all expired timers from the timer queue, and wakes up all
|
|
|
|
* associated tasks. Returns the date of next event (or eternity).
|
|
|
|
*/
|
2008-07-06 22:09:58 +00:00
|
|
|
void wake_expired_tasks(int *next);
|
2008-06-29 20:40:23 +00:00
|
|
|
|
2009-03-08 14:53:06 +00:00
|
|
|
/* Perform minimal initializations, report 0 in case of error, 1 if OK. */
|
|
|
|
int init_task();
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
#endif /* _PROTO_TASK_H */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|