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 | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 5 | Copyright (C) 2000-2008 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 | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 35 | extern unsigned int run_queue; /* run queue size */ |
| 36 | extern unsigned int niced_tasks; /* number of niced tasks in the run queue */ |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 37 | extern struct pool_head *pool2_task; |
Willy Tarreau | ce44f12 | 2008-07-05 18:16:19 +0200 | [diff] [blame] | 38 | extern struct task *last_timer; /* optimization: last queued timer */ |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 40 | /* perform minimal initializations, report 0 in case of error, 1 if OK. */ |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 41 | int init_task(); |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 42 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | /* puts the task <t> in run queue <q>, and returns <t> */ |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 44 | struct task *task_wakeup(struct task *t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 46 | /* removes the task <t> from the run queue if it was in it. |
| 47 | * returns <t>. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | */ |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 49 | static inline struct task *task_sleep(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | { |
| 51 | if (t->state == TASK_RUNNING) { |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 52 | t->state = TASK_IDLE; |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 53 | eb32_delete(&t->eb); |
| 54 | run_queue--; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 55 | if (likely(t->nice)) |
| 56 | niced_tasks--; |
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 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 62 | * unlinks the task from wherever it is queued : |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 63 | * - run_queue |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 64 | * - wait queue |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 65 | * A pointer to the task itself is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | */ |
Willy Tarreau | ce44f12 | 2008-07-05 18:16:19 +0200 | [diff] [blame] | 67 | static inline struct task *task_dequeue(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | { |
Willy Tarreau | ce44f12 | 2008-07-05 18:16:19 +0200 | [diff] [blame] | 69 | if (likely(t->eb.node.leaf_p)) { |
| 70 | if (last_timer == t) |
| 71 | last_timer = NULL; |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 72 | eb32_delete(&t->eb); |
Willy Tarreau | ce44f12 | 2008-07-05 18:16:19 +0200 | [diff] [blame] | 73 | } |
| 74 | return t; |
| 75 | } |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | ce44f12 | 2008-07-05 18:16:19 +0200 | [diff] [blame] | 77 | /* |
| 78 | * Unlinks the task and adjusts run queue stats. |
| 79 | * A pointer to the task itself is returned. |
| 80 | */ |
| 81 | static inline struct task *task_delete(struct task *t) |
| 82 | { |
| 83 | task_dequeue(t); |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 84 | if (t->state == TASK_RUNNING) { |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 85 | run_queue--; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 86 | if (likely(t->nice)) |
| 87 | niced_tasks--; |
| 88 | } |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 89 | return t; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Initialize a new task. The bare minimum is performed (queue pointers and state). |
| 94 | * The task is returned. |
| 95 | */ |
| 96 | static inline struct task *task_init(struct task *t) |
| 97 | { |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 98 | t->eb.node.leaf_p = NULL; |
| 99 | t->state = TASK_IDLE; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 100 | t->nice = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | return t; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * frees a task. Its context must have been freed since it will be lost. |
| 106 | */ |
| 107 | static inline void task_free(struct task *t) |
| 108 | { |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 109 | pool_free2(pool2_task, t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* inserts <task> into its assigned wait queue, where it may already be. In this case, it |
| 113 | * may be only moved or left where it was, depending on its timing requirements. |
| 114 | * <task> is returned. |
| 115 | */ |
| 116 | struct task *task_queue(struct task *task); |
| 117 | |
| 118 | /* |
| 119 | * This does 4 things : |
| 120 | * - wake up all expired tasks |
| 121 | * - call all runnable tasks |
| 122 | * - call maintain_proxies() to enable/disable the listeners |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 123 | * - return the date of next event in <next> or eternity. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | */ |
| 125 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 126 | void process_runnable_tasks(int *next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 127 | |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 128 | /* |
| 129 | * Extract all expired timers from the timer queue, and wakes up all |
| 130 | * associated tasks. Returns the date of next event (or eternity). |
| 131 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 132 | void wake_expired_tasks(int *next); |
Willy Tarreau | 58b458d | 2008-06-29 22:40:23 +0200 | [diff] [blame] | 133 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 134 | |
| 135 | #endif /* _PROTO_TASK_H */ |
| 136 | |
| 137 | /* |
| 138 | * Local variables: |
| 139 | * c-indent-level: 8 |
| 140 | * c-basic-offset: 8 |
| 141 | * End: |
| 142 | */ |