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; |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 36 | extern struct pool_head *pool2_task; |
| 37 | |
| 38 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 39 | int init_task(); |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 40 | |
| 41 | /* needed later */ |
| 42 | void *tree_delete(void *node); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | |
| 44 | /* puts the task <t> in run queue <q>, and returns <t> */ |
Willy Tarreau | e33aece | 2007-04-30 13:15:14 +0200 | [diff] [blame] | 45 | #define task_wakeup _task_wakeup |
| 46 | struct task *_task_wakeup(struct task *t); |
| 47 | static inline struct task *__task_wakeup(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | { |
| 49 | if (t->state == TASK_RUNNING) |
| 50 | return t; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 51 | |
| 52 | if (t->qlist.p != NULL) |
| 53 | DLIST_DEL(&t->qlist); |
| 54 | |
| 55 | DLIST_ADD(run_queue, &t->qlist); |
| 56 | t->state = TASK_RUNNING; |
| 57 | |
Willy Tarreau | 70bcfb7 | 2008-01-27 02:21:53 +0100 | [diff] [blame] | 58 | if (likely(t->wq != NULL)) { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 59 | tree_delete(t->wq); |
| 60 | t->wq = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | } |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 62 | |
| 63 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 66 | /* removes the task <t> from the run queue if it was in it. |
| 67 | * returns <t>. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | */ |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 69 | static inline struct task *task_sleep(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | { |
| 71 | if (t->state == TASK_RUNNING) { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 72 | DLIST_DEL(&t->qlist); |
| 73 | t->qlist.p = NULL; |
| 74 | t->state = TASK_IDLE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | } |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 76 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 80 | * unlinks the task from wherever it is queued : |
| 81 | * - eternity_queue, run_queue |
| 82 | * - wait queue : wq not null => remove carrier node too |
| 83 | * A pointer to the task itself is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | */ |
| 85 | static inline struct task *task_delete(struct task *t) |
| 86 | { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 87 | if (t->qlist.p != NULL) { |
| 88 | DLIST_DEL(&t->qlist); |
| 89 | t->qlist.p = NULL; |
| 90 | } |
| 91 | |
| 92 | if (t->wq) { |
| 93 | tree_delete(t->wq); |
| 94 | t->wq = NULL; |
| 95 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | return t; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * frees a task. Its context must have been freed since it will be lost. |
| 101 | */ |
| 102 | static inline void task_free(struct task *t) |
| 103 | { |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 104 | pool_free2(pool2_task, t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* inserts <task> into its assigned wait queue, where it may already be. In this case, it |
| 108 | * may be only moved or left where it was, depending on its timing requirements. |
| 109 | * <task> is returned. |
| 110 | */ |
| 111 | struct task *task_queue(struct task *task); |
| 112 | |
| 113 | /* |
| 114 | * This does 4 things : |
| 115 | * - wake up all expired tasks |
| 116 | * - call all runnable tasks |
| 117 | * - call maintain_proxies() to enable/disable the listeners |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 118 | * - return the date of next event in <next> or eternity. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | */ |
| 120 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 121 | void process_runnable_tasks(struct timeval *next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | |
| 123 | |
| 124 | #endif /* _PROTO_TASK_H */ |
| 125 | |
| 126 | /* |
| 127 | * Local variables: |
| 128 | * c-indent-level: 8 |
| 129 | * c-basic-offset: 8 |
| 130 | * End: |
| 131 | */ |