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
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>
[f761f1eb]42#include <synch/rwlock.h>
[ea7890e7]43#include <synch/spinlock.h>
[5dcee525]44#include <adt/avl.h>
[f76fed4]45#include <mm/slab.h>
[b3f8fb7]46#include <arch/cpu.h>
47#include <mm/tlb.h>
[0f250f9]48#include <proc/uarg.h>
[9a1b20c]49#include <udebug/udebug.h>
[e1b6742]50#include <sysinfo/abi.h>
[f761f1eb]51
[361635c]52#define THREAD_STACK_SIZE STACK_SIZE
[4c60255]53#define THREAD_NAME_BUFLEN 20
[f761f1eb]54
[a000878c]55extern const char *thread_states[];
[f761f1eb]56
[32fffef0]57/* Thread flags */
[80bcaed]58
[4365d10]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 */
[80bcaed]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)
[d8431986]69/** Thread will be attached by the caller. */
70#define THREAD_FLAG_NOATTACH (1 << 3)
[f761f1eb]71
[4c60255]72/** Thread structure. There is one per thread. */
73typedef struct thread {
[80bcaed]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. */
[5dcee525]77
78 /** Threads linkage to the threads_tree. */
79 avltree_node_t threads_tree_node;
[4c60255]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
[80bcaed]89 /** Function implementing the thread. */
90 void (* thread_code)(void *);
91 /** Argument passed to thread_code() function. */
92 void *thread_arg;
[4c60255]93
[80bcaed]94 /**
95 * From here, the stored context is restored when the thread is
96 * scheduled.
97 */
[4c60255]98 context_t saved_context;
[80bcaed]99 /**
100 * From here, the stored timeout context is restored when sleep times
101 * out.
102 */
[4c60255]103 context_t sleep_timeout_context;
[80bcaed]104 /**
105 * From here, the stored interruption context is restored when sleep is
106 * interrupted.
107 */
[4c60255]108 context_t sleep_interruption_context;
109
[80bcaed]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;
[4c60255]118
[80bcaed]119 /**
120 * True if this thread is executing copy_from_uspace().
121 * False otherwise.
122 */
[4c60255]123 bool in_copy_from_uspace;
[80bcaed]124 /**
125 * True if this thread is executing copy_to_uspace().
126 * False otherwise.
127 */
[4c60255]128 bool in_copy_to_uspace;
129
130 /**
[80bcaed]131 * If true, the thread will not go to sleep at all and will call
132 * thread_exit() before returning to userspace.
[4c60255]133 */
134 bool interrupted;
135
[80bcaed]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;
[ea7890e7]140 /** Link used in the joiner_head list. */
141 link_t joiner_link;
[4c60255]142
143 fpu_context_t *saved_fpu_context;
144 int fpu_context_exists;
145
146 /*
147 * Defined only if thread doesn't run.
[80bcaed]148 * It means that fpu context is in CPU that last time executes this
149 * thread. This disables migration.
[4c60255]150 */
151 int fpu_context_engaged;
152
153 rwlock_type_t rwlock_holder_type;
154
[80bcaed]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;
[4c60255]159
[80bcaed]160 /** Thread's state. */
161 state_t state;
162 /** Thread's flags. */
163 int flags;
[4c60255]164
[80bcaed]165 /** Thread's CPU. */
166 cpu_t *cpu;
167 /** Containing task. */
168 task_t *task;
[4c60255]169
[80bcaed]170 /** Ticks before preemption. */
171 uint64_t ticks;
[4c60255]172
[80bcaed]173 /** Thread accounting. */
[a2a00e8]174 uint64_t ucycles;
175 uint64_t kcycles;
[80bcaed]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. */
[201abde]184 thread_id_t tid;
[4c60255]185
[80bcaed]186 /** Architecture-specific data. */
187 thread_arch_t arch;
[4c60255]188
[80bcaed]189 /** Thread's kernel stack. */
190 uint8_t *kstack;
[9a1b20c]191
192#ifdef CONFIG_UDEBUG
193 /** Debugging stuff */
194 udebug_thread_t udebug;
195#endif
196
[4c60255]197} thread_t;
198
[05e2a7ad]199/** Thread list lock.
200 *
[5dcee525]201 * This lock protects the threads_tree.
[05e2a7ad]202 * Must be acquired before T.lock for each T of type thread_t.
203 *
204 */
[8be8cfa]205SPINLOCK_EXTERN(threads_lock);
[05e2a7ad]206
[5dcee525]207/** AVL tree containing all threads. */
208extern avltree_t threads_tree;
[f761f1eb]209
210extern void thread_init(void);
[a000878c]211extern thread_t *thread_create(void (*)(void *), void *, task_t *, int,
212 const char *, bool);
[d52b82ad]213extern void thread_attach(thread_t *, task_t *);
214extern void thread_ready(thread_t *);
[874621f]215extern void thread_exit(void) __attribute__((noreturn));
[f761f1eb]216
[3fa424a9]217#ifndef thread_create_arch
[d52b82ad]218extern void thread_create_arch(thread_t *);
[3fa424a9]219#endif
[32fffef0]220#ifndef thr_constructor_arch
[d52b82ad]221extern void thr_constructor_arch(thread_t *);
[32fffef0]222#endif
223#ifndef thr_destructor_arch
[d52b82ad]224extern void thr_destructor_arch(thread_t *);
[32fffef0]225#endif
[3fa424a9]226
[d52b82ad]227extern void thread_sleep(uint32_t);
228extern void thread_usleep(uint32_t);
[f761f1eb]229
[80bcaed]230#define thread_join(t) \
231 thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
[d52b82ad]232extern int thread_join_timeout(thread_t *, uint32_t, int);
233extern void thread_detach(thread_t *);
[fe19611]234
[d52b82ad]235extern void thread_register_call_me(void (*)(void *), void *);
[55ab0f1]236extern void thread_print_list(void);
[d52b82ad]237extern void thread_destroy(thread_t *);
[e1b6742]238extern thread_t *thread_find_by_id(thread_id_t);
[a2a00e8]239extern void thread_update_accounting(bool);
[d52b82ad]240extern bool thread_exists(thread_t *);
[f761f1eb]241
[80bcaed]242/** Fpu context slab cache. */
[f76fed4]243extern slab_cache_t *fpu_context_slab;
244
[80bcaed]245/* Thread syscall prototypes. */
[d52b82ad]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 *);
[d9ece1cb]250extern unative_t sys_thread_usleep(uint32_t);
[9f52563]251
[f761f1eb]252#endif
[b45c443]253
[764c302]254/** @}
[b45c443]255 */
Note: See TracBrowser for help on using the repository browser.