blob: 424a46c6d0b02db4c2b92ee67903f8e4878ef679 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 include/proto/task.h
3 Functions for task management.
4
Willy Tarreau96bcfd72007-04-29 10:41:56 +02005 Copyright (C) 2000-2007 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 Tarreau96bcfd72007-04-29 10:41:56 +020035extern void *run_queue;
36
37/* needed later */
38void *tree_delete(void *node);
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
40/* puts the task <t> in run queue <q>, and returns <t> */
Willy Tarreau96bcfd72007-04-29 10:41:56 +020041static inline struct task *task_wakeup(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +020042{
43 if (t->state == TASK_RUNNING)
44 return t;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020045
46 if (t->qlist.p != NULL)
47 DLIST_DEL(&t->qlist);
48
49 DLIST_ADD(run_queue, &t->qlist);
50 t->state = TASK_RUNNING;
51
52 if (likely(t->wq)) {
53 tree_delete(t->wq);
54 t->wq = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020055 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +020056
57 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +020058}
59
Willy Tarreau96bcfd72007-04-29 10:41:56 +020060/* removes the task <t> from the run queue if it was in it.
61 * returns <t>.
Willy Tarreaubaaee002006-06-26 02:48:02 +020062 */
Willy Tarreau96bcfd72007-04-29 10:41:56 +020063static inline struct task *task_sleep(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +020064{
65 if (t->state == TASK_RUNNING) {
Willy Tarreau96bcfd72007-04-29 10:41:56 +020066 DLIST_DEL(&t->qlist);
67 t->qlist.p = NULL;
68 t->state = TASK_IDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +020069 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +020070 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +020071}
72
73/*
Willy Tarreau96bcfd72007-04-29 10:41:56 +020074 * unlinks the task from wherever it is queued :
75 * - eternity_queue, run_queue
76 * - wait queue : wq not null => remove carrier node too
77 * A pointer to the task itself is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +020078 */
79static inline struct task *task_delete(struct task *t)
80{
Willy Tarreau96bcfd72007-04-29 10:41:56 +020081 if (t->qlist.p != NULL) {
82 DLIST_DEL(&t->qlist);
83 t->qlist.p = NULL;
84 }
85
86 if (t->wq) {
87 tree_delete(t->wq);
88 t->wq = NULL;
89 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020090 return t;
91}
92
93/*
94 * frees a task. Its context must have been freed since it will be lost.
95 */
96static inline void task_free(struct task *t)
97{
98 pool_free(task, t);
99}
100
101/* inserts <task> into its assigned wait queue, where it may already be. In this case, it
102 * may be only moved or left where it was, depending on its timing requirements.
103 * <task> is returned.
104 */
105struct task *task_queue(struct task *task);
106
107/*
108 * This does 4 things :
109 * - wake up all expired tasks
110 * - call all runnable tasks
111 * - call maintain_proxies() to enable/disable the listeners
112 * - return the delay till next event in ms, -1 = wait indefinitely
113 * Note: this part should be rewritten with the O(ln(n)) scheduler.
114 *
115 */
116
117int process_runnable_tasks();
118
119
120#endif /* _PROTO_TASK_H */
121
122/*
123 * Local variables:
124 * c-indent-level: 8
125 * c-basic-offset: 8
126 * End:
127 */