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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e1b6742 was e1b6742, checked in by Martin Decky <martin@…>, 15 years ago

export threads to user space
the "tasks" command can now print all threads or threads belonging to a task

  • Property mode set to 100644
File size: 7.0 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/rwlock.h>
43#include <synch/spinlock.h>
44#include <adt/avl.h>
45#include <mm/slab.h>
46#include <arch/cpu.h>
47#include <mm/tlb.h>
48#include <proc/uarg.h>
49#include <udebug/udebug.h>
50#include <sysinfo/abi.h>
51
52#define THREAD_STACK_SIZE STACK_SIZE
53#define THREAD_NAME_BUFLEN 20
54
55extern const char *thread_states[];
56
57/* Thread flags */
58
59/** Thread cannot be migrated to another CPU.
60 *
61 * When using this flag, the caller must set cpu in the thread_t
62 * structure manually before calling thread_ready (even on uniprocessor).
63 */
64#define THREAD_FLAG_WIRED (1 << 0)
65/** Thread was migrated to another CPU and has not run yet. */
66#define THREAD_FLAG_STOLEN (1 << 1)
67/** Thread executes in userspace. */
68#define THREAD_FLAG_USPACE (1 << 2)
69/** Thread will be attached by the caller. */
70#define THREAD_FLAG_NOATTACH (1 << 3)
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 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 when the thread is
96 * scheduled.
97 */
98 context_t saved_context;
99 /**
100 * From here, the stored timeout context is restored when sleep times
101 * out.
102 */
103 context_t sleep_timeout_context;
104 /**
105 * From here, the stored interruption context is restored when sleep is
106 * interrupted.
107 */
108 context_t sleep_interruption_context;
109
110 /** If true, the thread can be interrupted from sleep. */
111 bool sleep_interruptible;
112 /** Wait queue in which this thread sleeps. */
113 waitq_t *sleep_queue;
114 /** Timeout used for timeoutable sleeping. */
115 timeout_t sleep_timeout;
116 /** Flag signalling sleep timeout in progress. */
117 volatile int timeout_pending;
118
119 /**
120 * True if this thread is executing copy_from_uspace().
121 * False otherwise.
122 */
123 bool in_copy_from_uspace;
124 /**
125 * True if this thread is executing copy_to_uspace().
126 * False otherwise.
127 */
128 bool in_copy_to_uspace;
129
130 /**
131 * If true, the thread will not go to sleep at all and will call
132 * thread_exit() before returning to userspace.
133 */
134 bool interrupted;
135
136 /** If true, thread_join_timeout() cannot be used on this thread. */
137 bool detached;
138 /** Waitq for thread_join_timeout(). */
139 waitq_t join_wq;
140 /** Link used in the joiner_head list. */
141 link_t joiner_link;
142
143 fpu_context_t *saved_fpu_context;
144 int fpu_context_exists;
145
146 /*
147 * Defined only if thread doesn't run.
148 * It means that fpu context is in CPU that last time executes this
149 * thread. This disables migration.
150 */
151 int fpu_context_engaged;
152
153 rwlock_type_t rwlock_holder_type;
154
155 /** Callback fired in scheduler before the thread is put asleep. */
156 void (* call_me)(void *);
157 /** Argument passed to call_me(). */
158 void *call_me_with;
159
160 /** Thread's state. */
161 state_t state;
162 /** Thread's flags. */
163 int flags;
164
165 /** Thread's CPU. */
166 cpu_t *cpu;
167 /** Containing task. */
168 task_t *task;
169
170 /** Ticks before preemption. */
171 uint64_t ticks;
172
173 /** Thread accounting. */
174 uint64_t ucycles;
175 uint64_t kcycles;
176 /** Last sampled cycle. */
177 uint64_t last_cycle;
178 /** Thread doesn't affect accumulated accounting. */
179 bool uncounted;
180
181 /** Thread's priority. Implemented as index to CPU->rq */
182 int priority;
183 /** Thread ID. */
184 thread_id_t tid;
185
186 /** Architecture-specific data. */
187 thread_arch_t arch;
188
189 /** Thread's kernel stack. */
190 uint8_t *kstack;
191
192#ifdef CONFIG_UDEBUG
193 /** Debugging stuff */
194 udebug_thread_t udebug;
195#endif
196
197} thread_t;
198
199/** Thread list lock.
200 *
201 * This lock protects the threads_tree.
202 * Must be acquired before T.lock for each T of type thread_t.
203 *
204 */
205SPINLOCK_EXTERN(threads_lock);
206
207/** AVL tree containing all threads. */
208extern avltree_t threads_tree;
209
210extern void thread_init(void);
211extern thread_t *thread_create(void (*)(void *), void *, task_t *, int,
212 const char *, bool);
213extern void thread_attach(thread_t *, task_t *);
214extern void thread_ready(thread_t *);
215extern void thread_exit(void) __attribute__((noreturn));
216
217#ifndef thread_create_arch
218extern void thread_create_arch(thread_t *);
219#endif
220#ifndef thr_constructor_arch
221extern void thr_constructor_arch(thread_t *);
222#endif
223#ifndef thr_destructor_arch
224extern void thr_destructor_arch(thread_t *);
225#endif
226
227extern void thread_sleep(uint32_t);
228extern void thread_usleep(uint32_t);
229
230#define thread_join(t) \
231 thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
232extern int thread_join_timeout(thread_t *, uint32_t, int);
233extern void thread_detach(thread_t *);
234
235extern void thread_register_call_me(void (*)(void *), void *);
236extern void thread_print_list(void);
237extern void thread_destroy(thread_t *);
238extern thread_t *thread_find_by_id(thread_id_t);
239extern void thread_update_accounting(bool);
240extern bool thread_exists(thread_t *);
241
242/** Fpu context slab cache. */
243extern slab_cache_t *fpu_context_slab;
244
245/* Thread syscall prototypes. */
246extern unative_t sys_thread_create(uspace_arg_t *, char *, size_t,
247 thread_id_t *);
248extern unative_t sys_thread_exit(int);
249extern unative_t sys_thread_get_id(thread_id_t *);
250extern unative_t sys_thread_usleep(uint32_t);
251
252#endif
253
254/** @}
255 */
Note: See TracBrowser for help on using the repository browser.