1 | /*
|
---|
2 | * Copyright (c) 2001-2007 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup genericproc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifndef KERN_THREAD_H_
|
---|
36 | #define KERN_THREAD_H_
|
---|
37 |
|
---|
38 | #include <synch/waitq.h>
|
---|
39 | #include <proc/task.h>
|
---|
40 | #include <time/timeout.h>
|
---|
41 | #include <cpu.h>
|
---|
42 | #include <synch/spinlock.h>
|
---|
43 | #include <synch/rcu_types.h>
|
---|
44 | #include <adt/avl.h>
|
---|
45 | #include <mm/slab.h>
|
---|
46 | #include <arch/cpu.h>
|
---|
47 | #include <mm/tlb.h>
|
---|
48 | #include <abi/proc/uarg.h>
|
---|
49 | #include <udebug/udebug.h>
|
---|
50 | #include <abi/proc/thread.h>
|
---|
51 | #include <abi/sysinfo.h>
|
---|
52 | #include <arch.h>
|
---|
53 |
|
---|
54 |
|
---|
55 | #define THREAD THE->thread
|
---|
56 |
|
---|
57 | #define THREAD_NAME_BUFLEN 20
|
---|
58 |
|
---|
59 | extern const char *thread_states[];
|
---|
60 |
|
---|
61 | /* Thread flags */
|
---|
62 | typedef enum {
|
---|
63 | THREAD_FLAG_NONE = 0,
|
---|
64 | /** Thread executes in user space. */
|
---|
65 | THREAD_FLAG_USPACE = (1 << 0),
|
---|
66 | /** Thread will be attached by the caller. */
|
---|
67 | THREAD_FLAG_NOATTACH = (1 << 1),
|
---|
68 | /** Thread accounting doesn't affect accumulated task accounting. */
|
---|
69 | THREAD_FLAG_UNCOUNTED = (1 << 2)
|
---|
70 | } thread_flags_t;
|
---|
71 |
|
---|
72 | /** Thread structure. There is one per thread. */
|
---|
73 | typedef struct thread {
|
---|
74 | link_t rq_link; /**< Run queue link. */
|
---|
75 | link_t wq_link; /**< Wait queue link. */
|
---|
76 | link_t th_link; /**< Links to threads within containing task. */
|
---|
77 |
|
---|
78 | /** Threads linkage to the threads_tree. */
|
---|
79 | avltree_node_t threads_tree_node;
|
---|
80 |
|
---|
81 | /** Lock protecting thread structure.
|
---|
82 | *
|
---|
83 | * Protects the whole thread structure except list links above.
|
---|
84 | */
|
---|
85 | IRQ_SPINLOCK_DECLARE(lock);
|
---|
86 |
|
---|
87 | char name[THREAD_NAME_BUFLEN];
|
---|
88 |
|
---|
89 | /** Function implementing the thread. */
|
---|
90 | void (*thread_code)(void *);
|
---|
91 | /** Argument passed to thread_code() function. */
|
---|
92 | void *thread_arg;
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * From here, the stored context is restored
|
---|
96 | * when the thread is scheduled.
|
---|
97 | */
|
---|
98 | context_t saved_context;
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * From here, the stored timeout context
|
---|
102 | * is restored when sleep times out.
|
---|
103 | */
|
---|
104 | context_t sleep_timeout_context;
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * From here, the stored interruption context
|
---|
108 | * is restored when sleep is interrupted.
|
---|
109 | */
|
---|
110 | context_t sleep_interruption_context;
|
---|
111 |
|
---|
112 | /** If true, the thread can be interrupted from sleep. */
|
---|
113 | bool sleep_interruptible;
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * If true, and this thread's sleep returns without a wakeup
|
---|
117 | * (timed out or interrupted), waitq ignores the next wakeup.
|
---|
118 | * This is necessary for futex to be able to handle those conditions.
|
---|
119 | */
|
---|
120 | bool sleep_composable;
|
---|
121 |
|
---|
122 | /** Wait queue in which this thread sleeps. */
|
---|
123 | waitq_t *sleep_queue;
|
---|
124 | /** Timeout used for timeoutable sleeping. */
|
---|
125 | timeout_t sleep_timeout;
|
---|
126 | /** Flag signalling sleep timeout in progress. */
|
---|
127 | volatile bool timeout_pending;
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * True if this thread is executing copy_from_uspace().
|
---|
131 | * False otherwise.
|
---|
132 | */
|
---|
133 | bool in_copy_from_uspace;
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * True if this thread is executing copy_to_uspace().
|
---|
137 | * False otherwise.
|
---|
138 | */
|
---|
139 | bool in_copy_to_uspace;
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * If true, the thread will not go to sleep at all and will call
|
---|
143 | * thread_exit() before returning to userspace.
|
---|
144 | */
|
---|
145 | bool interrupted;
|
---|
146 |
|
---|
147 | /** If true, thread_join_timeout() cannot be used on this thread. */
|
---|
148 | bool detached;
|
---|
149 | /** Waitq for thread_join_timeout(). */
|
---|
150 | waitq_t join_wq;
|
---|
151 | /** Link used in the joiner_head list. */
|
---|
152 | link_t joiner_link;
|
---|
153 |
|
---|
154 | fpu_context_t *saved_fpu_context;
|
---|
155 | bool fpu_context_exists;
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Defined only if thread doesn't run.
|
---|
159 | * It means that fpu context is in CPU that last time executes this
|
---|
160 | * thread. This disables migration.
|
---|
161 | */
|
---|
162 | bool fpu_context_engaged;
|
---|
163 |
|
---|
164 | /* The thread will not be migrated if nomigrate is non-zero. */
|
---|
165 | unsigned int nomigrate;
|
---|
166 |
|
---|
167 | /** Thread state. */
|
---|
168 | state_t state;
|
---|
169 |
|
---|
170 | /** Thread CPU. */
|
---|
171 | cpu_t *cpu;
|
---|
172 | /** Containing task. */
|
---|
173 | task_t *task;
|
---|
174 | /** Thread is wired to CPU. */
|
---|
175 | bool wired;
|
---|
176 | /** Thread was migrated to another CPU and has not run yet. */
|
---|
177 | bool stolen;
|
---|
178 | /** Thread is executed in user space. */
|
---|
179 | bool uspace;
|
---|
180 |
|
---|
181 | /** Ticks before preemption. */
|
---|
182 | uint64_t ticks;
|
---|
183 |
|
---|
184 | /** Thread accounting. */
|
---|
185 | uint64_t ucycles;
|
---|
186 | uint64_t kcycles;
|
---|
187 | /** Last sampled cycle. */
|
---|
188 | uint64_t last_cycle;
|
---|
189 | /** Thread doesn't affect accumulated accounting. */
|
---|
190 | bool uncounted;
|
---|
191 |
|
---|
192 | /** Thread's priority. Implemented as index to CPU->rq */
|
---|
193 | int priority;
|
---|
194 | /** Thread ID. */
|
---|
195 | thread_id_t tid;
|
---|
196 |
|
---|
197 | /** Work queue this thread belongs to or NULL. Immutable. */
|
---|
198 | struct work_queue *workq;
|
---|
199 | /** Links work queue threads. Protected by workq->lock. */
|
---|
200 | link_t workq_link;
|
---|
201 | /** True if the worker was blocked and is not running. Use thread->lock. */
|
---|
202 | bool workq_blocked;
|
---|
203 | /** True if the worker will block in order to become idle. Use workq->lock. */
|
---|
204 | bool workq_idling;
|
---|
205 |
|
---|
206 | /** RCU thread related data. Protected by its own locks. */
|
---|
207 | rcu_thread_data_t rcu;
|
---|
208 |
|
---|
209 | /** Architecture-specific data. */
|
---|
210 | thread_arch_t arch;
|
---|
211 |
|
---|
212 | /** Thread's kernel stack. */
|
---|
213 | uint8_t *kstack;
|
---|
214 |
|
---|
215 | #ifdef CONFIG_UDEBUG
|
---|
216 | /**
|
---|
217 | * If true, the scheduler will print a stack trace
|
---|
218 | * to the kernel console upon scheduling this thread.
|
---|
219 | */
|
---|
220 | bool btrace;
|
---|
221 |
|
---|
222 | /** Debugging stuff */
|
---|
223 | udebug_thread_t udebug;
|
---|
224 | #endif /* CONFIG_UDEBUG */
|
---|
225 | } thread_t;
|
---|
226 |
|
---|
227 | /** Thread list lock.
|
---|
228 | *
|
---|
229 | * This lock protects the threads_tree.
|
---|
230 | * Must be acquired before T.lock for each T of type thread_t.
|
---|
231 | *
|
---|
232 | */
|
---|
233 | IRQ_SPINLOCK_EXTERN(threads_lock);
|
---|
234 |
|
---|
235 | /** AVL tree containing all threads. */
|
---|
236 | extern avltree_t threads_tree;
|
---|
237 |
|
---|
238 | extern void thread_init(void);
|
---|
239 | extern thread_t *thread_create(void (*)(void *), void *, task_t *,
|
---|
240 | thread_flags_t, const char *);
|
---|
241 | extern void thread_wire(thread_t *, cpu_t *);
|
---|
242 | extern void thread_attach(thread_t *, task_t *);
|
---|
243 | extern void thread_ready(thread_t *);
|
---|
244 | extern void thread_exit(void) __attribute__((noreturn));
|
---|
245 | extern void thread_interrupt(thread_t *);
|
---|
246 | extern bool thread_interrupted(thread_t *);
|
---|
247 |
|
---|
248 | #ifndef thread_create_arch
|
---|
249 | extern void thread_create_arch(thread_t *);
|
---|
250 | #endif
|
---|
251 |
|
---|
252 | #ifndef thr_constructor_arch
|
---|
253 | extern void thr_constructor_arch(thread_t *);
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | #ifndef thr_destructor_arch
|
---|
257 | extern void thr_destructor_arch(thread_t *);
|
---|
258 | #endif
|
---|
259 |
|
---|
260 | extern void thread_sleep(uint32_t);
|
---|
261 | extern void thread_usleep(uint32_t);
|
---|
262 |
|
---|
263 | #define thread_join(t) \
|
---|
264 | thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
|
---|
265 |
|
---|
266 | extern errno_t thread_join_timeout(thread_t *, uint32_t, unsigned int);
|
---|
267 | extern void thread_detach(thread_t *);
|
---|
268 |
|
---|
269 | extern void thread_print_list(bool);
|
---|
270 | extern void thread_destroy(thread_t *, bool);
|
---|
271 | extern thread_t *thread_find_by_id(thread_id_t);
|
---|
272 | extern void thread_update_accounting(bool);
|
---|
273 | extern bool thread_exists(thread_t *);
|
---|
274 |
|
---|
275 | extern void thread_migration_disable(void);
|
---|
276 | extern void thread_migration_enable(void);
|
---|
277 |
|
---|
278 | #ifdef CONFIG_UDEBUG
|
---|
279 | extern void thread_stack_trace(thread_id_t);
|
---|
280 | #endif
|
---|
281 |
|
---|
282 | /** Fpu context slab cache. */
|
---|
283 | extern slab_cache_t *fpu_context_cache;
|
---|
284 |
|
---|
285 | /* Thread syscall prototypes. */
|
---|
286 | extern sys_errno_t sys_thread_create(uspace_arg_t *, char *, size_t,
|
---|
287 | thread_id_t *);
|
---|
288 | extern sys_errno_t sys_thread_exit(int);
|
---|
289 | extern sys_errno_t sys_thread_get_id(thread_id_t *);
|
---|
290 | extern sys_errno_t sys_thread_usleep(uint32_t);
|
---|
291 | extern sys_errno_t sys_thread_udelay(uint32_t);
|
---|
292 |
|
---|
293 | #endif
|
---|
294 |
|
---|
295 | /** @}
|
---|
296 | */
|
---|