blob: 10f6b42f6aa09ce7655035bf924b05a4d192c158 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 include/proto/task.h
3 Functions for task management.
4
Willy Tarreau9789f7b2008-06-24 08:17:16 +02005 Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
Willy Tarreaubaaee002006-06-26 02:48:02 +02006
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 Tarreaue3ba5f02006-06-29 18:54:54 +020027
28#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/memory.h>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020030#include <common/mini-clist.h>
31#include <common/standard.h>
32
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020033#include <types/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
Willy Tarreau91e99932008-06-30 07:51:00 +020035extern unsigned int run_queue; /* run queue size */
36extern unsigned int niced_tasks; /* number of niced tasks in the run queue */
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020037extern struct pool_head *pool2_task;
Willy Tarreauce44f122008-07-05 18:16:19 +020038extern struct task *last_timer; /* optimization: last queued timer */
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020039
Willy Tarreau9789f7b2008-06-24 08:17:16 +020040/* perform minimal initializations, report 0 in case of error, 1 if OK. */
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020041int init_task();
Willy Tarreau96bcfd72007-04-29 10:41:56 +020042
Willy Tarreaubaaee002006-06-26 02:48:02 +020043/* puts the task <t> in run queue <q>, and returns <t> */
Willy Tarreau58b458d2008-06-29 22:40:23 +020044struct task *task_wakeup(struct task *t);
Willy Tarreaubaaee002006-06-26 02:48:02 +020045
Willy Tarreau96bcfd72007-04-29 10:41:56 +020046/* removes the task <t> from the run queue if it was in it.
47 * returns <t>.
Willy Tarreaubaaee002006-06-26 02:48:02 +020048 */
Willy Tarreau96bcfd72007-04-29 10:41:56 +020049static inline struct task *task_sleep(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +020050{
51 if (t->state == TASK_RUNNING) {
Willy Tarreau96bcfd72007-04-29 10:41:56 +020052 t->state = TASK_IDLE;
Willy Tarreau58b458d2008-06-29 22:40:23 +020053 eb32_delete(&t->eb);
54 run_queue--;
Willy Tarreau91e99932008-06-30 07:51:00 +020055 if (likely(t->nice))
56 niced_tasks--;
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +020058 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +020059}
60
61/*
Willy Tarreau96bcfd72007-04-29 10:41:56 +020062 * unlinks the task from wherever it is queued :
Willy Tarreau58b458d2008-06-29 22:40:23 +020063 * - run_queue
Willy Tarreau9789f7b2008-06-24 08:17:16 +020064 * - wait queue
Willy Tarreau96bcfd72007-04-29 10:41:56 +020065 * A pointer to the task itself is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +020066 */
Willy Tarreauce44f122008-07-05 18:16:19 +020067static inline struct task *task_dequeue(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +020068{
Willy Tarreauce44f122008-07-05 18:16:19 +020069 if (likely(t->eb.node.leaf_p)) {
70 if (last_timer == t)
71 last_timer = NULL;
Willy Tarreau9789f7b2008-06-24 08:17:16 +020072 eb32_delete(&t->eb);
Willy Tarreauce44f122008-07-05 18:16:19 +020073 }
74 return t;
75}
Willy Tarreau9789f7b2008-06-24 08:17:16 +020076
Willy Tarreauce44f122008-07-05 18:16:19 +020077/*
78 * Unlinks the task and adjusts run queue stats.
79 * A pointer to the task itself is returned.
80 */
81static inline struct task *task_delete(struct task *t)
82{
83 task_dequeue(t);
Willy Tarreau91e99932008-06-30 07:51:00 +020084 if (t->state == TASK_RUNNING) {
Willy Tarreau58b458d2008-06-29 22:40:23 +020085 run_queue--;
Willy Tarreau91e99932008-06-30 07:51:00 +020086 if (likely(t->nice))
87 niced_tasks--;
88 }
Willy Tarreau9789f7b2008-06-24 08:17:16 +020089 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 */
96static inline struct task *task_init(struct task *t)
97{
Willy Tarreau9789f7b2008-06-24 08:17:16 +020098 t->eb.node.leaf_p = NULL;
99 t->state = TASK_IDLE;
Willy Tarreau91e99932008-06-30 07:51:00 +0200100 t->nice = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 return t;
102}
103
104/*
105 * frees a task. Its context must have been freed since it will be lost.
106 */
107static inline void task_free(struct task *t)
108{
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200109 pool_free2(pool2_task, t);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110}
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 */
116struct 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 Tarreaud825eef2007-05-12 22:35:00 +0200123 * - return the date of next event in <next> or eternity.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 */
125
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200126void process_runnable_tasks(int *next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200127
Willy Tarreau58b458d2008-06-29 22:40:23 +0200128/*
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 Tarreau0c303ee2008-07-07 00:09:58 +0200132void wake_expired_tasks(int *next);
Willy Tarreau58b458d2008-06-29 22:40:23 +0200133
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134
135#endif /* _PROTO_TASK_H */
136
137/*
138 * Local variables:
139 * c-indent-level: 8
140 * c-basic-offset: 8
141 * End:
142 */