blob: c594d525ed5fc485b1e32381a3da985a1258bd29 [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 Tarreaue33aece2007-04-30 13:15:14 +020041#define task_wakeup _task_wakeup
42struct task *_task_wakeup(struct task *t);
43static inline struct task *__task_wakeup(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +020044{
45 if (t->state == TASK_RUNNING)
46 return t;
Willy Tarreau96bcfd72007-04-29 10:41:56 +020047
48 if (t->qlist.p != NULL)
49 DLIST_DEL(&t->qlist);
50
51 DLIST_ADD(run_queue, &t->qlist);
52 t->state = TASK_RUNNING;
53
54 if (likely(t->wq)) {
55 tree_delete(t->wq);
56 t->wq = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +020058
59 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +020060}
61
Willy Tarreau96bcfd72007-04-29 10:41:56 +020062/* removes the task <t> from the run queue if it was in it.
63 * returns <t>.
Willy Tarreaubaaee002006-06-26 02:48:02 +020064 */
Willy Tarreau96bcfd72007-04-29 10:41:56 +020065static inline struct task *task_sleep(struct task *t)
Willy Tarreaubaaee002006-06-26 02:48:02 +020066{
67 if (t->state == TASK_RUNNING) {
Willy Tarreau96bcfd72007-04-29 10:41:56 +020068 DLIST_DEL(&t->qlist);
69 t->qlist.p = NULL;
70 t->state = TASK_IDLE;
Willy Tarreaubaaee002006-06-26 02:48:02 +020071 }
Willy Tarreau96bcfd72007-04-29 10:41:56 +020072 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +020073}
74
75/*
Willy Tarreau96bcfd72007-04-29 10:41:56 +020076 * unlinks the task from wherever it is queued :
77 * - eternity_queue, run_queue
78 * - wait queue : wq not null => remove carrier node too
79 * A pointer to the task itself is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +020080 */
81static inline struct task *task_delete(struct task *t)
82{
Willy Tarreau96bcfd72007-04-29 10:41:56 +020083 if (t->qlist.p != NULL) {
84 DLIST_DEL(&t->qlist);
85 t->qlist.p = NULL;
86 }
87
88 if (t->wq) {
89 tree_delete(t->wq);
90 t->wq = NULL;
91 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020092 return t;
93}
94
95/*
96 * frees a task. Its context must have been freed since it will be lost.
97 */
98static inline void task_free(struct task *t)
99{
100 pool_free(task, t);
101}
102
103/* inserts <task> into its assigned wait queue, where it may already be. In this case, it
104 * may be only moved or left where it was, depending on its timing requirements.
105 * <task> is returned.
106 */
107struct task *task_queue(struct task *task);
108
109/*
110 * This does 4 things :
111 * - wake up all expired tasks
112 * - call all runnable tasks
113 * - call maintain_proxies() to enable/disable the listeners
Willy Tarreaud825eef2007-05-12 22:35:00 +0200114 * - return the date of next event in <next> or eternity.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 */
116
Willy Tarreaud825eef2007-05-12 22:35:00 +0200117void process_runnable_tasks(struct timeval *next);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118
119
120#endif /* _PROTO_TASK_H */
121
122/*
123 * Local variables:
124 * c-indent-level: 8
125 * c-basic-offset: 8
126 * End:
127 */