blob: bc64f43f5820c82e9106926b8afa9cb9b751ae03 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau24f4efa2010-08-27 17:56:48 +02002 * include/types/task.h
3 * Macros, variables and structures for task management.
4 *
5 * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
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 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _TYPES_TASK_H
23#define _TYPES_TASK_H
24
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <sys/time.h>
26
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreau96bcfd72007-04-29 10:41:56 +020028#include <common/mini-clist.h>
Willy Tarreau8d388052017-11-05 13:34:20 +010029#include <eb32sctree.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010030#include <eb32tree.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
32/* values for task->state */
Willy Tarreauf0cea1e2018-07-26 16:13:00 +020033#define TASK_SLEEPING 0x0000 /* task sleeping */
34#define TASK_RUNNING 0x0001 /* the task is currently running */
Olivier Houchard76e45182018-07-26 16:19:58 +020035#define TASK_GLOBAL 0x0002 /* The task is currently in the global runqueue */
Willy Tarreauf0cea1e2018-07-26 16:13:00 +020036
37#define TASK_WOKEN_INIT 0x0100 /* woken up for initialisation purposes */
38#define TASK_WOKEN_TIMER 0x0200 /* woken up because of expired timer */
39#define TASK_WOKEN_IO 0x0400 /* woken up because of completed I/O */
40#define TASK_WOKEN_SIGNAL 0x0800 /* woken up by a system signal */
41#define TASK_WOKEN_MSG 0x1000 /* woken up by another task's message */
42#define TASK_WOKEN_RES 0x2000 /* woken up because of available resource */
43#define TASK_WOKEN_OTHER 0x4000 /* woken up for an unspecified reason */
Willy Tarreaufdccded2008-08-29 18:19:04 +020044
45/* use this to check a task state or to clean it up before queueing */
46#define TASK_WOKEN_ANY (TASK_WOKEN_OTHER|TASK_WOKEN_INIT|TASK_WOKEN_TIMER| \
47 TASK_WOKEN_IO|TASK_WOKEN_SIGNAL|TASK_WOKEN_MSG| \
48 TASK_WOKEN_RES)
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
Thierry FOURNIERd6975962017-07-12 14:31:10 +020050struct notification {
51 struct list purge_me; /* Part of the list of signals to be purged in the
52 case of the LUA execution stack crash. */
53 struct list wake_me; /* Part of list of signals to be targeted if an
54 event occurs. */
55 struct task *task; /* The task to be wake if an event occurs. */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010056 __decl_hathreads(HA_SPINLOCK_T lock);
Thierry FOURNIERd6975962017-07-12 14:31:10 +020057};
58
Olivier Houchardb0bdae72018-05-18 18:45:28 +020059/* This part is common between struct task and struct tasklet so that tasks
60 * can be used as-is as tasklets.
61 */
62#define TASK_COMMON \
63 struct { \
64 unsigned short state; /* task state : bitfield of TASK_ */ \
65 short nice; /* task prio from -1024 to +1024, or -32768 for tasklets */ \
66 unsigned int calls; /* number of times process was called */ \
67 struct task *(*process)(struct task *t, void *ctx, unsigned short state); /* the function which processes the task */ \
68 void *context; /* the task's context */ \
69 }
70
Willy Tarreaubaaee002006-06-26 02:48:02 +020071/* The base for all tasks */
72struct task {
Olivier Houchardb0bdae72018-05-18 18:45:28 +020073 TASK_COMMON; /* must be at the beginning! */
Willy Tarreau8d388052017-11-05 13:34:20 +010074 struct eb32sc_node rq; /* ebtree node used to hold the task in the run queue */
Willy Tarreau05efc0f2013-12-07 01:01:39 +010075 struct eb32_node wq; /* ebtree node used to hold the task in the wait queue */
76 int expire; /* next expiration date for this task, in ticks */
Willy Tarreauf65610a2017-10-31 16:06:06 +010077 unsigned long thread_mask; /* mask of thread IDs authorized to process the task */
Willy Tarreaubaaee002006-06-26 02:48:02 +020078};
79
Olivier Houchardb0bdae72018-05-18 18:45:28 +020080/* lightweight tasks, without priority, mainly used for I/Os */
81struct tasklet {
82 TASK_COMMON; /* must be at the beginning! */
83 struct list list;
84};
85
86#define TASK_IS_TASKLET(t) ((t)->nice == -32768)
87
Willy Tarreau26c25062009-03-08 09:38:41 +010088/*
89 * The task callback (->process) is responsible for updating ->expire. It must
90 * return a pointer to the task itself, except if the task has been deleted, in
91 * which case it returns NULL so that the scheduler knows it must not check the
92 * expire timer. The scheduler will requeue the task at the proper location.
93 */
94
Willy Tarreaubaaee002006-06-26 02:48:02 +020095#endif /* _TYPES_TASK_H */
96
97/*
98 * Local variables:
99 * c-indent-level: 8
100 * c-basic-offset: 8
101 * End:
102 */