Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | include/proto/task.h |
| 3 | Functions for task management. |
| 4 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 5 | Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation, version 2.1 |
| 10 | exclusively. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_TASK_H |
| 23 | #define _PROTO_TASK_H |
| 24 | |
| 25 | |
| 26 | #include <sys/time.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 27 | |
| 28 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 29 | #include <common/memory.h> |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 30 | #include <common/mini-clist.h> |
| 31 | #include <common/standard.h> |
| 32 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 33 | #include <types/task.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 35 | extern void *run_queue; |
| 36 | |
| 37 | /* needed later */ |
| 38 | void *tree_delete(void *node); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | |
| 40 | /* puts the task <t> in run queue <q>, and returns <t> */ |
Willy Tarreau | e33aece | 2007-04-30 13:15:14 +0200 | [diff] [blame] | 41 | #define task_wakeup _task_wakeup |
| 42 | struct task *_task_wakeup(struct task *t); |
| 43 | static inline struct task *__task_wakeup(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | { |
| 45 | if (t->state == TASK_RUNNING) |
| 46 | return t; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 47 | |
| 48 | if (t->qlist.p != NULL) |
| 49 | DLIST_DEL(&t->qlist); |
| 50 | |
| 51 | DLIST_ADD(run_queue, &t->qlist); |
| 52 | t->state = TASK_RUNNING; |
| 53 | |
| 54 | if (likely(t->wq)) { |
| 55 | tree_delete(t->wq); |
| 56 | t->wq = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | } |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 58 | |
| 59 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 62 | /* removes the task <t> from the run queue if it was in it. |
| 63 | * returns <t>. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | */ |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 65 | static inline struct task *task_sleep(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | { |
| 67 | if (t->state == TASK_RUNNING) { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 68 | DLIST_DEL(&t->qlist); |
| 69 | t->qlist.p = NULL; |
| 70 | t->state = TASK_IDLE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 71 | } |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 72 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 76 | * unlinks the task from wherever it is queued : |
| 77 | * - eternity_queue, run_queue |
| 78 | * - wait queue : wq not null => remove carrier node too |
| 79 | * A pointer to the task itself is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | */ |
| 81 | static inline struct task *task_delete(struct task *t) |
| 82 | { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 83 | if (t->qlist.p != NULL) { |
| 84 | DLIST_DEL(&t->qlist); |
| 85 | t->qlist.p = NULL; |
| 86 | } |
| 87 | |
| 88 | if (t->wq) { |
| 89 | tree_delete(t->wq); |
| 90 | t->wq = NULL; |
| 91 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | return t; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * frees a task. Its context must have been freed since it will be lost. |
| 97 | */ |
| 98 | static inline void task_free(struct task *t) |
| 99 | { |
| 100 | pool_free(task, t); |
| 101 | } |
| 102 | |
| 103 | /* inserts <task> into its assigned wait queue, where it may already be. In this case, it |
| 104 | * may be only moved or left where it was, depending on its timing requirements. |
| 105 | * <task> is returned. |
| 106 | */ |
| 107 | struct task *task_queue(struct task *task); |
| 108 | |
| 109 | /* |
| 110 | * This does 4 things : |
| 111 | * - wake up all expired tasks |
| 112 | * - call all runnable tasks |
| 113 | * - call maintain_proxies() to enable/disable the listeners |
| 114 | * - return the delay till next event in ms, -1 = wait indefinitely |
| 115 | * Note: this part should be rewritten with the O(ln(n)) scheduler. |
| 116 | * |
| 117 | */ |
| 118 | |
| 119 | int process_runnable_tasks(); |
| 120 | |
| 121 | |
| 122 | #endif /* _PROTO_TASK_H */ |
| 123 | |
| 124 | /* |
| 125 | * Local variables: |
| 126 | * c-indent-level: 8 |
| 127 | * c-basic-offset: 8 |
| 128 | * End: |
| 129 | */ |