source: mainline/kernel/generic/include/proc/thread.h@ b7fd2a0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7fd2a0 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 7.9 KB
Line 
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
59extern const char *thread_states[];
60
61/* Thread flags */
62typedef 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. */
73typedef 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 /** Wait queue in which this thread sleeps. */
115 waitq_t *sleep_queue;
116 /** Timeout used for timeoutable sleeping. */
117 timeout_t sleep_timeout;
118 /** Flag signalling sleep timeout in progress. */
119 volatile bool timeout_pending;
120
121 /**
122 * True if this thread is executing copy_from_uspace().
123 * False otherwise.
124 */
125 bool in_copy_from_uspace;
126
127 /**
128 * True if this thread is executing copy_to_uspace().
129 * False otherwise.
130 */
131 bool in_copy_to_uspace;
132
133 /**
134 * If true, the thread will not go to sleep at all and will call
135 * thread_exit() before returning to userspace.
136 */
137 bool interrupted;
138
139 /** If true, thread_join_timeout() cannot be used on this thread. */
140 bool detached;
141 /** Waitq for thread_join_timeout(). */
142 waitq_t join_wq;
143 /** Link used in the joiner_head list. */
144 link_t joiner_link;
145
146 fpu_context_t *saved_fpu_context;
147 bool fpu_context_exists;
148
149 /*
150 * Defined only if thread doesn't run.
151 * It means that fpu context is in CPU that last time executes this
152 * thread. This disables migration.
153 */
154 bool fpu_context_engaged;
155
156 /* The thread will not be migrated if nomigrate is non-zero. */
157 unsigned int nomigrate;
158
159 /** Thread state. */
160 state_t state;
161
162 /** Thread CPU. */
163 cpu_t *cpu;
164 /** Containing task. */
165 task_t *task;
166 /** Thread is wired to CPU. */
167 bool wired;
168 /** Thread was migrated to another CPU and has not run yet. */
169 bool stolen;
170 /** Thread is executed in user space. */
171 bool uspace;
172
173 /** Ticks before preemption. */
174 uint64_t ticks;
175
176 /** Thread accounting. */
177 uint64_t ucycles;
178 uint64_t kcycles;
179 /** Last sampled cycle. */
180 uint64_t last_cycle;
181 /** Thread doesn't affect accumulated accounting. */
182 bool uncounted;
183
184 /** Thread's priority. Implemented as index to CPU->rq */
185 int priority;
186 /** Thread ID. */
187 thread_id_t tid;
188
189 /** Work queue this thread belongs to or NULL. Immutable. */
190 struct work_queue *workq;
191 /** Links work queue threads. Protected by workq->lock. */
192 link_t workq_link;
193 /** True if the worker was blocked and is not running. Use thread->lock. */
194 bool workq_blocked;
195 /** True if the worker will block in order to become idle. Use workq->lock. */
196 bool workq_idling;
197
198 /** RCU thread related data. Protected by its own locks. */
199 rcu_thread_data_t rcu;
200
201 /** Architecture-specific data. */
202 thread_arch_t arch;
203
204 /** Thread's kernel stack. */
205 uint8_t *kstack;
206
207#ifdef CONFIG_UDEBUG
208 /**
209 * If true, the scheduler will print a stack trace
210 * to the kernel console upon scheduling this thread.
211 */
212 bool btrace;
213
214 /** Debugging stuff */
215 udebug_thread_t udebug;
216#endif /* CONFIG_UDEBUG */
217} thread_t;
218
219/** Thread list lock.
220 *
221 * This lock protects the threads_tree.
222 * Must be acquired before T.lock for each T of type thread_t.
223 *
224 */
225IRQ_SPINLOCK_EXTERN(threads_lock);
226
227/** AVL tree containing all threads. */
228extern avltree_t threads_tree;
229
230extern void thread_init(void);
231extern thread_t *thread_create(void (*)(void *), void *, task_t *,
232 thread_flags_t, const char *);
233extern void thread_wire(thread_t *, cpu_t *);
234extern void thread_attach(thread_t *, task_t *);
235extern void thread_ready(thread_t *);
236extern void thread_exit(void) __attribute__((noreturn));
237extern void thread_interrupt(thread_t *);
238extern bool thread_interrupted(thread_t *);
239
240#ifndef thread_create_arch
241extern void thread_create_arch(thread_t *);
242#endif
243
244#ifndef thr_constructor_arch
245extern void thr_constructor_arch(thread_t *);
246#endif
247
248#ifndef thr_destructor_arch
249extern void thr_destructor_arch(thread_t *);
250#endif
251
252extern void thread_sleep(uint32_t);
253extern void thread_usleep(uint32_t);
254
255#define thread_join(t) \
256 thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
257
258extern errno_t thread_join_timeout(thread_t *, uint32_t, unsigned int);
259extern void thread_detach(thread_t *);
260
261extern void thread_print_list(bool);
262extern void thread_destroy(thread_t *, bool);
263extern thread_t *thread_find_by_id(thread_id_t);
264extern void thread_update_accounting(bool);
265extern bool thread_exists(thread_t *);
266
267extern void thread_migration_disable(void);
268extern void thread_migration_enable(void);
269
270#ifdef CONFIG_UDEBUG
271extern void thread_stack_trace(thread_id_t);
272#endif
273
274/** Fpu context slab cache. */
275extern slab_cache_t *fpu_context_cache;
276
277/* Thread syscall prototypes. */
278extern sys_errno_t sys_thread_create(uspace_arg_t *, char *, size_t,
279 thread_id_t *);
280extern sys_errno_t sys_thread_exit(int);
281extern sys_errno_t sys_thread_get_id(thread_id_t *);
282extern sys_errno_t sys_thread_usleep(uint32_t);
283extern sys_errno_t sys_thread_udelay(uint32_t);
284
285#endif
286
287/** @}
288 */
Note: See TracBrowser for help on using the repository browser.