1 /*
2  * 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  */
21 
22 #ifndef _TYPES_TASK_H
23 #define _TYPES_TASK_H
24 
25 #include <sys/time.h>
26 
27 #include <common/config.h>
28 #include <common/mini-clist.h>
29 #include <eb32sctree.h>
30 #include <eb32tree.h>
31 
32 /* values for task->state */
33 #define TASK_SLEEPING     0x0000  /* task sleeping */
34 #define TASK_RUNNING      0x0001  /* the task is currently running */
35 #define TASK_GLOBAL       0x0002  /* The task is currently in the global runqueue */
36 #define TASK_QUEUED       0x0004  /* The task has been (re-)added to the run queue */
37 #define TASK_SHARED_WQ    0x0008  /* The task's expiration may be updated by other
38                                    * threads, must be set before first queue/wakeup */
39 
40 #define TASK_WOKEN_INIT   0x0100  /* woken up for initialisation purposes */
41 #define TASK_WOKEN_TIMER  0x0200  /* woken up because of expired timer */
42 #define TASK_WOKEN_IO     0x0400  /* woken up because of completed I/O */
43 #define TASK_WOKEN_SIGNAL 0x0800  /* woken up by a system signal */
44 #define TASK_WOKEN_MSG    0x1000  /* woken up by another task's message */
45 #define TASK_WOKEN_RES    0x2000  /* woken up because of available resource */
46 #define TASK_WOKEN_OTHER  0x4000  /* woken up for an unspecified reason */
47 
48 /* use this to check a task state or to clean it up before queueing */
49 #define TASK_WOKEN_ANY    (TASK_WOKEN_OTHER|TASK_WOKEN_INIT|TASK_WOKEN_TIMER| \
50                            TASK_WOKEN_IO|TASK_WOKEN_SIGNAL|TASK_WOKEN_MSG| \
51                            TASK_WOKEN_RES)
52 
53 struct notification {
54 	struct list purge_me; /* Part of the list of signals to be purged in the
55 	                         case of the LUA execution stack crash. */
56 	struct list wake_me; /* Part of list of signals to be targeted if an
57 	                        event occurs. */
58 	struct task *task; /* The task to be wake if an event occurs. */
59 	__decl_hathreads(HA_SPINLOCK_T lock);
60 };
61 
62 /* force to split per-thread stuff into separate cache lines */
63 struct task_per_thread {
64 	struct eb_root timers;  /* tree constituting the per-thread wait queue */
65 	struct eb_root rqueue;  /* tree constituting the per-thread run queue */
66 	struct list task_list;  /* List of tasks to be run, mixing tasks and tasklets */
67 	int task_list_size;     /* Number of tasks in the task_list */
68 	int rqueue_size;        /* Number of elements in the per-thread run queue */
69 	__attribute__((aligned(64))) char end[0];
70 };
71 
72 /* This part is common between struct task and struct tasklet so that tasks
73  * can be used as-is as tasklets.
74  */
75 #define TASK_COMMON							\
76 	struct {							\
77 		unsigned short state; /* task state : bitfield of TASK_	*/ \
78 		short nice; /* task prio from -1024 to +1024, or -32768 for tasklets */ \
79 		unsigned int calls; /* number of times process was called */ \
80 		uint64_t cpu_time; /* total CPU time consumed */            \
81 		struct task *(*process)(struct task *t, void *ctx, unsigned short state); /* the function which processes the task */ \
82 		void *context; /* the task's context */			\
83 	}
84 
85 /* The base for all tasks */
86 struct task {
87 	TASK_COMMON;			/* must be at the beginning! */
88 	struct eb32sc_node rq;		/* ebtree node used to hold the task in the run queue */
89 	struct eb32_node wq;		/* ebtree node used to hold the task in the wait queue */
90 	int expire;			/* next expiration date for this task, in ticks */
91 	unsigned long thread_mask;	/* mask of thread IDs authorized to process the task */
92 	uint64_t call_date;		/* date of the last task wakeup or call */
93 	uint64_t lat_time;		/* total latency time experienced */
94 };
95 
96 /* lightweight tasks, without priority, mainly used for I/Os */
97 struct tasklet {
98 	TASK_COMMON;			/* must be at the beginning! */
99 	struct list list;
100 };
101 
102 #define TASK_IS_TASKLET(t) ((t)->nice == -32768)
103 
104 /*
105  * The task callback (->process) is responsible for updating ->expire. It must
106  * return a pointer to the task itself, except if the task has been deleted, in
107  * which case it returns NULL so that the scheduler knows it must not check the
108  * expire timer. The scheduler will requeue the task at the proper location.
109  */
110 
111 
112 /* A work_list is a thread-safe way to enqueue some work to be run on another
113  * thread. It consists of a list, a task and a general-purpose argument.
114  * A work is appended to the list by atomically adding a list element to the
115  * list and waking up the associated task, which is done using work_add(). The
116  * caller must be careful about how operations are run as it will definitely
117  * happen that the element being enqueued is processed by the other thread
118  * before the call returns. Some locking conventions between the caller and the
119  * callee might sometimes be necessary. The task is always woken up with reason
120  * TASK_WOKEN_OTHER and a context pointing to the work_list entry.
121  */
122 struct work_list {
123 	struct list head;
124 	struct task *task;
125 	void *arg;
126 };
127 
128 #endif /* _TYPES_TASK_H */
129 
130 /*
131  * Local variables:
132  *  c-indent-level: 8
133  *  c-basic-offset: 8
134  * End:
135  */
136