Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Task management functions. |
| 3 | * |
| 4 | * Copyright 2000-2006 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 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 Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 13 | #include <common/config.h> |
| 14 | #include <common/mini-clist.h> |
| 15 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | |
| 17 | #include <proto/task.h> |
| 18 | |
| 19 | |
| 20 | /* FIXME : this should be removed very quickly ! */ |
| 21 | extern int maintain_proxies(void); |
| 22 | |
| 23 | void **pool_task= NULL; |
| 24 | struct task *rq = NULL; /* global run queue */ |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 25 | |
| 26 | struct rb_root wait_queue[2] = { |
| 27 | RB_ROOT, |
| 28 | RB_ROOT, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 32 | static inline void __rb_insert_task_queue(struct task *newtask) |
| 33 | { |
| 34 | struct rb_node **p = &newtask->wq->rb_node; |
| 35 | struct rb_node *parent = NULL; |
| 36 | struct task * task; |
| 37 | |
| 38 | while (*p) |
| 39 | { |
| 40 | parent = *p; |
| 41 | task = rb_entry(parent, struct task, rb_node); |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame^] | 42 | if (tv_cmp_ge2(&task->expire, &newtask->expire)) |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 43 | p = &(*p)->rb_left; |
| 44 | else |
| 45 | p = &(*p)->rb_right; |
| 46 | } |
| 47 | rb_link_node(&newtask->rb_node, parent, p); |
| 48 | } |
| 49 | |
Willy Tarreau | b1b8272 | 2007-02-11 13:52:16 +0100 | [diff] [blame] | 50 | static void rb_insert_task_queue(struct task *newtask) |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 51 | { |
| 52 | __rb_insert_task_queue(newtask); |
| 53 | rb_insert_color(&newtask->rb_node, newtask->wq); |
| 54 | } |
| 55 | |
| 56 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | struct task *task_queue(struct task *task) |
| 58 | { |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 59 | struct rb_node *node; |
| 60 | struct task *next, *prev; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | if (tv_iseternity(&task->expire)) { |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 63 | if (task->wq) { |
| 64 | if (task->wq == &wait_queue[1]) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | return task; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 66 | else |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | task_delete(task); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | } |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 69 | task->wq = &wait_queue[1]; |
| 70 | rb_insert_task_queue(task); |
| 71 | return task; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | } else { |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 73 | if (task->wq != &wait_queue[0]) { |
| 74 | if (task->wq) |
| 75 | task_delete(task); |
| 76 | task->wq = &wait_queue[0]; |
| 77 | rb_insert_task_queue(task); |
| 78 | return task; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 81 | // check whether task should be re insert |
| 82 | node = rb_prev(&task->rb_node); |
| 83 | if (node) { |
| 84 | prev = rb_entry(node, struct task, rb_node); |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame^] | 85 | if (tv_cmp_ge(&prev->expire, &task->expire)) { |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 86 | task_delete(task); |
| 87 | task->wq = &wait_queue[0]; |
| 88 | rb_insert_task_queue(task); |
| 89 | return task; |
| 90 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 93 | node = rb_next(&task->rb_node); |
| 94 | if (node) { |
| 95 | next = rb_entry(node, struct task, rb_node); |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame^] | 96 | if (tv_cmp_ge(&task->expire, &next->expire)) { |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 97 | task_delete(task); |
| 98 | task->wq = &wait_queue[0]; |
| 99 | rb_insert_task_queue(task); |
| 100 | return task; |
| 101 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | } |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 103 | return task; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | * This does 4 things : |
| 109 | * - wake up all expired tasks |
| 110 | * - call all runnable tasks |
| 111 | * - call maintain_proxies() to enable/disable the listeners |
| 112 | * - return the delay till next event in ms, -1 = wait indefinitely |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | * |
| 114 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | int process_runnable_tasks() |
| 116 | { |
| 117 | int next_time; |
| 118 | int time2; |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 119 | struct task *t; |
| 120 | struct rb_node *node; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 121 | |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 122 | next_time = TIME_ETERNITY; |
| 123 | for (node = rb_first(&wait_queue[0]); |
| 124 | node != NULL; node = rb_next(node)) { |
| 125 | t = rb_entry(node, struct task, rb_node); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | if (t->state & TASK_RUNNING) |
| 127 | continue; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 128 | if (tv_iseternity(&t->expire)) |
| 129 | continue; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 130 | if (tv_cmp_ms(&t->expire, &now) <= 0) { |
| 131 | task_wakeup(&rq, t); |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 132 | } else { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 133 | int temp_time = tv_remain(&now, &t->expire); |
| 134 | if (temp_time) |
| 135 | next_time = temp_time; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* process each task in the run queue now. Each task may be deleted |
| 141 | * since we only use the run queue's head. Note that any task can be |
| 142 | * woken up by any other task and it will be processed immediately |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 143 | * after as it will be queued on the run queue's head ! |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 144 | */ |
| 145 | while ((t = rq) != NULL) { |
| 146 | int temp_time; |
| 147 | |
| 148 | task_sleep(&rq, t); |
| 149 | temp_time = t->process(t); |
| 150 | next_time = MINTIME(temp_time, next_time); |
| 151 | } |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 152 | |
| 153 | /* maintain all proxies in a consistent state. This should quickly |
| 154 | * become a task because it becomes expensive when there are huge |
| 155 | * numbers of proxies. */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 156 | time2 = maintain_proxies(); |
| 157 | return MINTIME(time2, next_time); |
| 158 | } |
| 159 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 160 | /* |
| 161 | * Local variables: |
| 162 | * c-indent-level: 8 |
| 163 | * c-basic-offset: 8 |
| 164 | * End: |
| 165 | */ |