blob: d236d9fdc7ec023fe3c1b911cd0281cfe87f2247 [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 Tarreaue33aece2007-04-30 13:15:14 +020046struct task *_task_wakeup(struct task *t)
47{
48 return __task_wakeup(t);
49}
Willy Tarreau96bcfd72007-04-29 10:41:56 +020050/*
51 * task_queue()
52 *
53 * Inserts a task into the wait queue at the position given by its expiration
54 * date.
55 *
56 */
57struct task *task_queue(struct task *task)
Willy Tarreau964c9362007-01-07 00:38:00 +010058{
Willy Tarreau96bcfd72007-04-29 10:41:56 +020059 if (unlikely(task->qlist.p != NULL)) {
60 DLIST_DEL(&task->qlist);
61 task->qlist.p = NULL;
62 }
63
64 if (unlikely(task->wq)) {
65 tree_delete(task->wq);
66 task->wq = NULL;
67 }
68
69 if (unlikely(tv_iseternity(&task->expire))) {
70 task->wq = NULL;
71 DLIST_ADD(eternity_queue, &task->qlist);
72 return task;
73 }
74
75 task->wq = ul2tree_insert(&timer_wq, task->expire.tv_sec, task->expire.tv_usec);
76 DLIST_ADD(task->wq->data, &task->qlist);
77 return task;
Willy Tarreau964c9362007-01-07 00:38:00 +010078}
79
80
Willy Tarreau96bcfd72007-04-29 10:41:56 +020081/*
82 * Extract all expired timers from the wait queue, and wakes up all
83 * associated tasks.
84 * Returns the time to wait for next task (next_time).
85 *
86 * FIXME: Use an alternative queue for ETERNITY tasks.
87 *
88 */
89int wake_expired_tasks()
Willy Tarreaubaaee002006-06-26 02:48:02 +020090{
Willy Tarreau96bcfd72007-04-29 10:41:56 +020091 int slen;
92 struct task *task;
93 void *data;
94 int next_time;
Willy Tarreaubaaee002006-06-26 02:48:02 +020095
Willy Tarreau96bcfd72007-04-29 10:41:56 +020096 /*
97 * Hint: tasks are *rarely* expired. So we can try to optimize
98 * by not scanning the tree at all in most cases.
99 */
100
101 if (likely(timer_wq.data != NULL)) {
102 task = LIST_ELEM(timer_wq.data, struct task *, qlist);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200103 if (likely(__tv_isge(&task->expire, &now) > 0))
104 return tv_ms_remain(&now, &task->expire);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200105 }
106
107 /* OK we lose. Let's scan the tree then. */
108 next_time = TIME_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200110 tree64_foreach(&timer_wq, data, stack, slen) {
111 task = LIST_ELEM(data, struct task *, qlist);
112
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200113 if (__tv_isgt(&task->expire, &now)) {
114 next_time = tv_ms_remain(&now, &task->expire);
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200115 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200118 /*
119 * OK, all tasks linked to this node will be unlinked, as well
120 * as the node itself, so we do not need to care about correct
121 * unlinking.
122 */
123 foreach_dlist_item(task, data, struct task *, qlist) {
124 DLIST_DEL(&task->qlist);
125 task->wq = NULL;
126 DLIST_ADD(run_queue, &task->qlist);
127 task->state = TASK_RUNNING;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200130 return next_time;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131}
132
133/*
134 * This does 4 things :
135 * - wake up all expired tasks
136 * - call all runnable tasks
137 * - call maintain_proxies() to enable/disable the listeners
138 * - return the delay till next event in ms, -1 = wait indefinitely
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139 *
140 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200141int process_runnable_tasks()
142{
143 int next_time;
144 int time2;
Willy Tarreau964c9362007-01-07 00:38:00 +0100145 struct task *t;
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200146 void *queue;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200148 next_time = wake_expired_tasks();
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149 /* process each task in the run queue now. Each task may be deleted
150 * since we only use the run queue's head. Note that any task can be
151 * woken up by any other task and it will be processed immediately
Willy Tarreau964c9362007-01-07 00:38:00 +0100152 * after as it will be queued on the run queue's head !
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153 */
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200154
155 queue = run_queue;
156 foreach_dlist_item(t, queue, struct task *, qlist) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200157 int temp_time;
158
Willy Tarreau96bcfd72007-04-29 10:41:56 +0200159 DLIST_DEL(&t->qlist);
160 t->qlist.p = NULL;
161
162 t->state = TASK_IDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163 temp_time = t->process(t);
164 next_time = MINTIME(temp_time, next_time);
165 }
Willy Tarreau964c9362007-01-07 00:38:00 +0100166
167 /* maintain all proxies in a consistent state. This should quickly
168 * become a task because it becomes expensive when there are huge
169 * numbers of proxies. */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 time2 = maintain_proxies();
171 return MINTIME(time2, next_time);
172}
173
Willy Tarreaubaaee002006-06-26 02:48:02 +0200174/*
175 * Local variables:
176 * c-indent-level: 8
177 * c-basic-offset: 8
178 * End:
179 */