2 * Copyright (c) 2001-2007 Jakub Jermar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup kernel_generic_proc
35 #ifndef KERN_THREAD_H_
36 #define KERN_THREAD_H_
38 #include <synch/waitq.h>
39 #include <proc/task.h>
40 #include <time/timeout.h>
42 #include <synch/spinlock.h>
43 #include <adt/odict.h>
47 #include <abi/proc/uarg.h>
48 #include <udebug/udebug.h>
49 #include <abi/proc/thread.h>
50 #include <abi/sysinfo.h>
53 #define THREAD CURRENT->thread
55 #define THREAD_NAME_BUFLEN 20
57 extern const char *thread_states
[];
62 /** Thread executes in user space. */
63 THREAD_FLAG_USPACE
= (1 << 0),
64 /** Thread will be attached by the caller. */
65 THREAD_FLAG_NOATTACH
= (1 << 1),
66 /** Thread accounting doesn't affect accumulated task accounting. */
67 THREAD_FLAG_UNCOUNTED
= (1 << 2)
70 /** Thread structure. There is one per thread. */
71 typedef struct thread
{
72 atomic_refcount_t refcount
;
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. */
78 /** Link to @c threads ordered dictionary. */
82 * If true, the thread is terminating.
83 * It will not go to sleep in interruptible synchronization functions
84 * and will call thread_exit() before returning to userspace.
86 volatile bool interrupted
;
88 /** Waitq for thread_join_timeout(). */
91 /** Lock protecting thread structure.
93 * Protects the whole thread structure except fields listed above.
95 IRQ_SPINLOCK_DECLARE(lock
);
97 char name
[THREAD_NAME_BUFLEN
];
99 /** Function implementing the thread. */
100 void (*thread_code
)(void *);
101 /** Argument passed to thread_code() function. */
105 * From here, the stored context is restored
106 * when the thread is scheduled.
108 context_t saved_context
;
112 * From here, the stored timeout context
113 * is restored when sleep times out.
115 context_t sleep_timeout_context
;
118 * From here, the stored interruption context
119 * is restored when sleep is interrupted.
121 context_t sleep_interruption_context
;
123 /** If true, the thread can be interrupted from sleep. */
124 bool sleep_interruptible
;
127 * If true, and this thread's sleep returns without a wakeup
128 * (timed out or interrupted), waitq ignores the next wakeup.
129 * This is necessary for futex to be able to handle those conditions.
131 bool sleep_composable
;
133 /** Wait queue in which this thread sleeps. */
134 waitq_t
*sleep_queue
;
137 * True if this thread is executing copy_from_uspace().
140 bool in_copy_from_uspace
;
143 * True if this thread is executing copy_to_uspace().
146 bool in_copy_to_uspace
;
149 fpu_context_t fpu_context
;
151 bool fpu_context_exists
;
154 * Defined only if thread doesn't run.
155 * It means that fpu context is in CPU that last time executes this
156 * thread. This disables migration.
158 bool fpu_context_engaged
;
160 /* The thread will not be migrated if nomigrate is non-zero. */
161 unsigned int nomigrate
;
168 /** Containing task. */
170 /** Thread is wired to CPU. */
172 /** Thread was migrated to another CPU and has not run yet. */
174 /** Thread is executed in user space. */
177 /** Thread accounting. */
180 /** Last sampled cycle. */
182 /** Thread doesn't affect accumulated accounting. */
185 /** Thread's priority. Implemented as index to CPU->rq */
190 /** Architecture-specific data. */
193 /** Thread's kernel stack. */
198 * If true, the scheduler will print a stack trace
199 * to the kernel console upon scheduling this thread.
203 /** Debugging stuff */
204 udebug_thread_t udebug
;
205 #endif /* CONFIG_UDEBUG */
208 IRQ_SPINLOCK_EXTERN(threads_lock
);
209 extern odict_t threads
;
211 extern void thread_init(void);
212 extern thread_t
*thread_create(void (*)(void *), void *, task_t
*,
213 thread_flags_t
, const char *);
214 extern void thread_wire(thread_t
*, cpu_t
*);
215 extern void thread_attach(thread_t
*, task_t
*);
216 extern void thread_ready(thread_t
*);
217 extern void thread_exit(void) __attribute__((noreturn
));
218 extern void thread_interrupt(thread_t
*, bool);
220 static inline thread_t
*thread_ref(thread_t
*thread
)
222 refcount_up(&thread
->refcount
);
226 static inline thread_t
*thread_try_ref(thread_t
*thread
)
228 if (refcount_try_up(&thread
->refcount
))
234 extern void thread_put(thread_t
*);
236 #ifndef thread_create_arch
237 extern errno_t
thread_create_arch(thread_t
*, thread_flags_t
);
240 #ifndef thr_constructor_arch
241 extern void thr_constructor_arch(thread_t
*);
244 #ifndef thr_destructor_arch
245 extern void thr_destructor_arch(thread_t
*);
248 extern void thread_sleep(uint32_t);
249 extern void thread_usleep(uint32_t);
251 extern errno_t
thread_join(thread_t
*);
252 extern errno_t
thread_join_timeout(thread_t
*, uint32_t, unsigned int);
254 extern void thread_print_list(bool);
255 extern thread_t
*thread_find_by_id(thread_id_t
);
256 extern size_t thread_count(void);
257 extern thread_t
*thread_first(void);
258 extern thread_t
*thread_next(thread_t
*);
259 extern void thread_update_accounting(bool);
260 extern thread_t
*thread_try_get(thread_t
*);
262 extern void thread_migration_disable(void);
263 extern void thread_migration_enable(void);
266 extern void thread_stack_trace(thread_id_t
);
269 /** Fpu context slab cache. */
270 extern slab_cache_t
*fpu_context_cache
;
272 /* Thread syscall prototypes. */
273 extern sys_errno_t
sys_thread_create(uspace_ptr_uspace_arg_t
, uspace_ptr_char
, size_t,
274 uspace_ptr_thread_id_t
);
275 extern sys_errno_t
sys_thread_exit(int);
276 extern sys_errno_t
sys_thread_get_id(uspace_ptr_thread_id_t
);
277 extern sys_errno_t
sys_thread_usleep(uint32_t);
278 extern sys_errno_t
sys_thread_udelay(uint32_t);