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