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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b68ae24 was b23c88e, checked in by Adam Hraska <adam.hraska+hos@…>, 14 years ago

preemption_disable: Replaced memory barriers with compiler barriers. Added checks if reschedule is needed once preemption is enabled.

  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[f761f1eb]1/*
[ea7890e7]2 * Copyright (c) 2001-2007 Jakub Jermar
[f761f1eb]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
[764c302]29/** @addtogroup genericproc
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[06e1e95]35#ifndef KERN_THREAD_H_
36#define KERN_THREAD_H_
[f761f1eb]37
[b3f8fb7]38#include <synch/waitq.h>
39#include <proc/task.h>
[831a04d0]40#include <time/timeout.h>
[b3f8fb7]41#include <cpu.h>
[ea7890e7]42#include <synch/spinlock.h>
[181a746]43#include <synch/rcu.h>
[5dcee525]44#include <adt/avl.h>
[f76fed4]45#include <mm/slab.h>
[b3f8fb7]46#include <arch/cpu.h>
47#include <mm/tlb.h>
[c0699467]48#include <abi/proc/uarg.h>
[9a1b20c]49#include <udebug/udebug.h>
[c0699467]50#include <abi/sysinfo.h>
[f761f1eb]51
[da1bafb]52#define THREAD_NAME_BUFLEN 20
[f761f1eb]53
[a000878c]54extern const char *thread_states[];
[f761f1eb]55
[32fffef0]56/* Thread flags */
[6eef3c4]57typedef enum {
58 THREAD_FLAG_NONE = 0,
59 /** Thread executes in user space. */
60 THREAD_FLAG_USPACE = (1 << 0),
61 /** Thread will be attached by the caller. */
62 THREAD_FLAG_NOATTACH = (1 << 1),
63 /** Thread accounting doesn't affect accumulated task accounting. */
64 THREAD_FLAG_UNCOUNTED = (1 << 2)
65} thread_flags_t;
[f761f1eb]66
[4c60255]67/** Thread structure. There is one per thread. */
68typedef struct thread {
[da1bafb]69 link_t rq_link; /**< Run queue link. */
70 link_t wq_link; /**< Wait queue link. */
71 link_t th_link; /**< Links to threads within containing task. */
72
[5dcee525]73 /** Threads linkage to the threads_tree. */
74 avltree_node_t threads_tree_node;
[4c60255]75
76 /** Lock protecting thread structure.
77 *
78 * Protects the whole thread structure except list links above.
79 */
[da1bafb]80 IRQ_SPINLOCK_DECLARE(lock);
81
[4c60255]82 char name[THREAD_NAME_BUFLEN];
[da1bafb]83
[80bcaed]84 /** Function implementing the thread. */
[df58e44]85 void (*thread_code)(void *);
[80bcaed]86 /** Argument passed to thread_code() function. */
87 void *thread_arg;
[da1bafb]88
[80bcaed]89 /**
[df58e44]90 * From here, the stored context is restored
91 * when the thread is scheduled.
[80bcaed]92 */
[4c60255]93 context_t saved_context;
[df58e44]94
[80bcaed]95 /**
[df58e44]96 * From here, the stored timeout context
97 * is restored when sleep times out.
[80bcaed]98 */
[4c60255]99 context_t sleep_timeout_context;
[df58e44]100
[80bcaed]101 /**
[df58e44]102 * From here, the stored interruption context
103 * is restored when sleep is interrupted.
[80bcaed]104 */
[4c60255]105 context_t sleep_interruption_context;
[da1bafb]106
[80bcaed]107 /** If true, the thread can be interrupted from sleep. */
108 bool sleep_interruptible;
109 /** Wait queue in which this thread sleeps. */
110 waitq_t *sleep_queue;
111 /** Timeout used for timeoutable sleeping. */
112 timeout_t sleep_timeout;
113 /** Flag signalling sleep timeout in progress. */
[da1bafb]114 volatile bool timeout_pending;
115
[80bcaed]116 /**
117 * True if this thread is executing copy_from_uspace().
118 * False otherwise.
119 */
[4c60255]120 bool in_copy_from_uspace;
[df58e44]121
[80bcaed]122 /**
123 * True if this thread is executing copy_to_uspace().
124 * False otherwise.
125 */
[4c60255]126 bool in_copy_to_uspace;
127
128 /**
[80bcaed]129 * If true, the thread will not go to sleep at all and will call
130 * thread_exit() before returning to userspace.
[4c60255]131 */
[da1bafb]132 bool interrupted;
[4c60255]133
[80bcaed]134 /** If true, thread_join_timeout() cannot be used on this thread. */
135 bool detached;
136 /** Waitq for thread_join_timeout(). */
137 waitq_t join_wq;
[ea7890e7]138 /** Link used in the joiner_head list. */
139 link_t joiner_link;
[da1bafb]140
[4c60255]141 fpu_context_t *saved_fpu_context;
[6eef3c4]142 bool fpu_context_exists;
[da1bafb]143
[4c60255]144 /*
145 * Defined only if thread doesn't run.
[80bcaed]146 * It means that fpu context is in CPU that last time executes this
147 * thread. This disables migration.
[4c60255]148 */
[6eef3c4]149 bool fpu_context_engaged;
[da1bafb]150
[43ac0cc]151 /* The thread will not be migrated if nomigrate is non-zero. */
[6eef3c4]152 unsigned int nomigrate;
[43ac0cc]153
[6eef3c4]154 /** Thread state. */
[80bcaed]155 state_t state;
[4c60255]156
[b23c88e]157 /** The thread would have been rescheduled had it not disabled preemption.*/
158 bool need_resched;
159
[6eef3c4]160 /** Thread CPU. */
[80bcaed]161 cpu_t *cpu;
162 /** Containing task. */
163 task_t *task;
[6eef3c4]164 /** Thread is wired to CPU. */
165 bool wired;
166 /** Thread was migrated to another CPU and has not run yet. */
167 bool stolen;
168 /** Thread is executed in user space. */
169 bool uspace;
[da1bafb]170
[80bcaed]171 /** Ticks before preemption. */
172 uint64_t ticks;
[4c60255]173
[80bcaed]174 /** Thread accounting. */
[a2a00e8]175 uint64_t ucycles;
176 uint64_t kcycles;
[80bcaed]177 /** Last sampled cycle. */
178 uint64_t last_cycle;
[da1bafb]179 /** Thread doesn't affect accumulated accounting. */
[80bcaed]180 bool uncounted;
[da1bafb]181
[80bcaed]182 /** Thread's priority. Implemented as index to CPU->rq */
183 int priority;
184 /** Thread ID. */
[201abde]185 thread_id_t tid;
[8a64e81e]186
187 /** Work queue this thread belongs to or NULL. Immutable. */
188 struct work_queue *workq;
189 /** Links work queue threads. Protected by workq->lock. */
190 link_t workq_link;
[181a746]191 /** True if the worker was blocked and is not running. Use thread->lock. */
[8a64e81e]192 bool workq_blocked;
[181a746]193 /** True if the worker will block in order to become idle. Use workq->lock. */
[8a64e81e]194 bool workq_idling;
[4c60255]195
[181a746]196 /** RCU thread related data. Protected by its own locks. */
197 rcu_thread_data_t rcu;
198
[80bcaed]199 /** Architecture-specific data. */
200 thread_arch_t arch;
[da1bafb]201
[80bcaed]202 /** Thread's kernel stack. */
203 uint8_t *kstack;
[da1bafb]204
[9a1b20c]205#ifdef CONFIG_UDEBUG
[5b7a107]206 /**
207 * If true, the scheduler will print a stack trace
208 * to the kernel console upon scheduling this thread.
209 */
210 bool btrace;
211
[9a1b20c]212 /** Debugging stuff */
213 udebug_thread_t udebug;
[da1bafb]214#endif /* CONFIG_UDEBUG */
[4c60255]215} thread_t;
216
[05e2a7ad]217/** Thread list lock.
218 *
[5dcee525]219 * This lock protects the threads_tree.
[05e2a7ad]220 * Must be acquired before T.lock for each T of type thread_t.
221 *
222 */
[da1bafb]223IRQ_SPINLOCK_EXTERN(threads_lock);
[05e2a7ad]224
[5dcee525]225/** AVL tree containing all threads. */
226extern avltree_t threads_tree;
[f761f1eb]227
228extern void thread_init(void);
[da1bafb]229extern thread_t *thread_create(void (*)(void *), void *, task_t *,
[6eef3c4]230 thread_flags_t, const char *);
231extern void thread_wire(thread_t *, cpu_t *);
[d52b82ad]232extern void thread_attach(thread_t *, task_t *);
233extern void thread_ready(thread_t *);
[874621f]234extern void thread_exit(void) __attribute__((noreturn));
[518dd43]235extern void thread_interrupt(thread_t *);
236extern bool thread_interrupted(thread_t *);
[f761f1eb]237
[3fa424a9]238#ifndef thread_create_arch
[d52b82ad]239extern void thread_create_arch(thread_t *);
[3fa424a9]240#endif
[da1bafb]241
[32fffef0]242#ifndef thr_constructor_arch
[d52b82ad]243extern void thr_constructor_arch(thread_t *);
[32fffef0]244#endif
[da1bafb]245
[32fffef0]246#ifndef thr_destructor_arch
[d52b82ad]247extern void thr_destructor_arch(thread_t *);
[32fffef0]248#endif
[3fa424a9]249
[d52b82ad]250extern void thread_sleep(uint32_t);
251extern void thread_usleep(uint32_t);
[f761f1eb]252
[80bcaed]253#define thread_join(t) \
254 thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
[da1bafb]255
256extern int thread_join_timeout(thread_t *, uint32_t, unsigned int);
[d52b82ad]257extern void thread_detach(thread_t *);
[fe19611]258
[48dcc69]259extern void thread_print_list(bool);
[da1bafb]260extern void thread_destroy(thread_t *, bool);
[e1b6742]261extern thread_t *thread_find_by_id(thread_id_t);
[a2a00e8]262extern void thread_update_accounting(bool);
[d52b82ad]263extern bool thread_exists(thread_t *);
[f761f1eb]264
[43ac0cc]265extern void thread_migration_disable(void);
266extern void thread_migration_enable(void);
267
[5b7a107]268#ifdef CONFIG_UDEBUG
[df58e44]269extern void thread_stack_trace(thread_id_t);
[5b7a107]270#endif
[f761f1eb]271
[80bcaed]272/** Fpu context slab cache. */
[f76fed4]273extern slab_cache_t *fpu_context_slab;
274
[80bcaed]275/* Thread syscall prototypes. */
[96b02eb9]276extern sysarg_t sys_thread_create(uspace_arg_t *, char *, size_t,
[d52b82ad]277 thread_id_t *);
[96b02eb9]278extern sysarg_t sys_thread_exit(int);
279extern sysarg_t sys_thread_get_id(thread_id_t *);
280extern sysarg_t sys_thread_usleep(uint32_t);
[7e7b791]281extern sysarg_t sys_thread_udelay(uint32_t);
[9f52563]282
[f761f1eb]283#endif
[b45c443]284
[764c302]285/** @}
[b45c443]286 */
Note: See TracBrowser for help on using the repository browser.