blob: 2fd6efa2a9a825f26165d07a0387885ed6a09107 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Task management functions.
3 *
Willy Tarreau96bcfd72007-04-29 10:41:56 +02004 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau2dd0d472006-06-29 17:53:05 +020013#include <common/config.h>
14#include <common/mini-clist.h>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020015#include <common/standard.h>
Willy Tarreaua6a6a932007-04-28 22:40:08 +020016#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020017
18#include <proto/task.h>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020019#include <types/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreau96bcfd72007-04-29 10:41:56 +020021// FIXME: check 8bitops.c for faster FLS
22#include <import/bitops.h>
23#include <import/tree.h>
24
Willy Tarreaubaaee002006-06-26 02:48:02 +020025
26/* FIXME : this should be removed very quickly ! */
27extern int maintain_proxies(void);
28
29void **pool_task= NULL;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020030void **pool_tree64 = NULL;
31static struct ultree *stack[LLONGBITS];
Willy Tarreau964c9362007-01-07 00:38:00 +010032
Willy Tarreau96bcfd72007-04-29 10:41:56 +020033UL2TREE_HEAD(timer_wq);
34void *eternity_queue = NULL;
35void *run_queue = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreau96bcfd72007-04-29 10:41:56 +020037struct ultree *ul2tree_insert(struct ultree *root, unsigned long h, unsigned long l)
Willy Tarreau964c9362007-01-07 00:38:00 +010038{
Willy Tarreau96bcfd72007-04-29 10:41:56 +020039 return __ul2tree_insert(root, h, l);
40}
Willy Tarreau964c9362007-01-07 00:38:00 +010041
Willy Tarreau96bcfd72007-04-29 10:41:56 +020042void *tree_delete(void *node) {
43 return __tree_delete(node);
Willy Tarreau964c9362007-01-07 00:38:00 +010044}
45
Willy Tarreau96bcfd72007-04-29 10:41:56 +020046/*
47 * task_queue()
48 *
49 * Inserts a task into the wait queue at the position given by its expiration
50 * date.
51 *
52 */
53struct task *task_queue(struct task *task)
Willy Tarreau964c9362007-01-07 00:38:00 +010054{
Willy Tarreau96bcfd72007-04-29 10:41:56 +020055 if (unlikely(task->qlist.p != NULL)) {
56 DLIST_DEL(&task->qlist);
57 task->qlist.p = NULL;
58 }
59
60 if (unlikely(task->wq)) {
61 tree_delete(task->wq);
62 task->wq = NULL;
63 }
64
65 if (unlikely(tv_iseternity(&task->expire))) {
66 task->wq = NULL;
67 DLIST_ADD(eternity_queue, &task->qlist);
68 return task;
69 }
70
71 task->wq = ul2tree_insert(&timer_wq, task->expire.tv_sec, task->expire.tv_usec);
72 DLIST_ADD(task->wq->data, &task->qlist);
73 return task;
Willy Tarreau964c9362007-01-07 00:38:00 +010074}
75
76
Willy Tarreau96bcfd72007-04-29 10:41:56 +020077/*
78 * Extract all expired timers from the wait queue, and wakes up all
79 * associated tasks.
80 * Returns the time to wait for next task (next_time).
81 *
82 * FIXME: Use an alternative queue for ETERNITY tasks.
83 *
84 */
85int wake_expired_tasks()
Willy Tarreaubaaee002006-06-26 02:48:02 +020086{
Willy Tarreau96bcfd72007-04-29 10:41:56 +020087 int slen;
88 struct task *task;
89 void *data;
90 int next_time;
Willy Tarreaubaaee002006-06-26 02:48:02 +020091
Willy Tarreau96bcfd72007-04-29 10:41:56 +020092 /*
93 * Hint: tasks are *rarely* expired. So we can try to optimize
94 * by not scanning the tree at all in most cases.
95 */
96
97 if (likely(timer_wq.data != NULL)) {
98 task = LIST_ELEM(timer_wq.data, struct task *, qlist);
Willy Tarreau42aae5c2007-04-29 17:43:56 +020099 if (likely(__tv_isge(&task->expire, &now) > 0))
100 return tv_ms_remain(&now, &task->expire);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200101 }
102
103 /* OK we lose. Let's scan the tree then. */
104 next_time = TIME_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200106 tree64_foreach(&timer_wq, data, stack, slen) {
107 task = LIST_ELEM(data, struct task *, qlist);
108
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200109 if (__tv_isgt(&task->expire, &now)) {
110 next_time = tv_ms_remain(&now, &task->expire);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200111 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200114 /*
115 * OK, all tasks linked to this node will be unlinked, as well
116 * as the node itself, so we do not need to care about correct
117 * unlinking.
118 */
119 foreach_dlist_item(task, data, struct task *, qlist) {
120 DLIST_DEL(&task->qlist);
121 task->wq = NULL;
122 DLIST_ADD(run_queue, &task->qlist);
123 task->state = TASK_RUNNING;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200126 return next_time;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127}
128
129/*
130 * This does 4 things :
131 * - wake up all expired tasks
132 * - call all runnable tasks
133 * - call maintain_proxies() to enable/disable the listeners
134 * - return the delay till next event in ms, -1 = wait indefinitely
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135 *
136 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200137int process_runnable_tasks()
138{
139 int next_time;
140 int time2;
Willy Tarreau964c9362007-01-07 00:38:00 +0100141 struct task *t;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200142 void *queue;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200144 next_time = wake_expired_tasks();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145 /* process each task in the run queue now. Each task may be deleted
146 * since we only use the run queue's head. Note that any task can be
147 * woken up by any other task and it will be processed immediately
Willy Tarreau964c9362007-01-07 00:38:00 +0100148 * after as it will be queued on the run queue's head !
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149 */
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200150
151 queue = run_queue;
152 foreach_dlist_item(t, queue, struct task *, qlist) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153 int temp_time;
154
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200155 DLIST_DEL(&t->qlist);
156 t->qlist.p = NULL;
157
158 t->state = TASK_IDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159 temp_time = t->process(t);
160 next_time = MINTIME(temp_time, next_time);
161 }
Willy Tarreau964c9362007-01-07 00:38:00 +0100162
163 /* maintain all proxies in a consistent state. This should quickly
164 * become a task because it becomes expensive when there are huge
165 * numbers of proxies. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 time2 = maintain_proxies();
167 return MINTIME(time2, next_time);
168}
169
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170/*
171 * Local variables:
172 * c-indent-level: 8
173 * c-basic-offset: 8
174 * End:
175 */