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

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

Make thread_t reference counted

This simplifies interaction between various locks and thread
lifespan, which simplifies things. For example, threads_lock can
now simply be a mutex protecting the global it was made for, and
nothing more.

  • Property mode set to 100644
File size: 7.5 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
[174156fd]29/** @addtogroup kernel_generic_proc
[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>
[ef1eab7]43#include <adt/odict.h>
[f76fed4]44#include <mm/slab.h>
[b3f8fb7]45#include <arch/cpu.h>
46#include <mm/tlb.h>
[c0699467]47#include <abi/proc/uarg.h>
[9a1b20c]48#include <udebug/udebug.h>
[9ba415e]49#include <abi/proc/thread.h>
[c0699467]50#include <abi/sysinfo.h>
[1066041]51#include <arch.h>
52
[a6e55886]53#define THREAD CURRENT->thread
[f761f1eb]54
[da1bafb]55#define THREAD_NAME_BUFLEN 20
[f761f1eb]56
[a000878c]57extern const char *thread_states[];
[f761f1eb]58
[32fffef0]59/* Thread flags */
[6eef3c4]60typedef enum {
61 THREAD_FLAG_NONE = 0,
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)
68} thread_flags_t;
[f761f1eb]69
[4c60255]70/** Thread structure. There is one per thread. */
71typedef struct thread {
[1871118]72 atomic_refcount_t refcount;
73
[da1bafb]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. */
[a35b458]77
[ef1eab7]78 /** Link to @c threads ordered dictionary. */
79 odlink_t lthreads;
[a35b458]80
[1871118]81 /**
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.
85 */
86 volatile bool interrupted;
87
88 /** Waitq for thread_join_timeout(). */
89 waitq_t join_wq;
90
[4c60255]91 /** Lock protecting thread structure.
92 *
[1871118]93 * Protects the whole thread structure except fields listed above.
[4c60255]94 */
[da1bafb]95 IRQ_SPINLOCK_DECLARE(lock);
[a35b458]96
[4c60255]97 char name[THREAD_NAME_BUFLEN];
[a35b458]98
[80bcaed]99 /** Function implementing the thread. */
[df58e44]100 void (*thread_code)(void *);
[80bcaed]101 /** Argument passed to thread_code() function. */
102 void *thread_arg;
[a35b458]103
[80bcaed]104 /**
[df58e44]105 * From here, the stored context is restored
106 * when the thread is scheduled.
[80bcaed]107 */
[4c60255]108 context_t saved_context;
[c030818]109 ipl_t saved_ipl;
[a35b458]110
[80bcaed]111 /**
[df58e44]112 * From here, the stored timeout context
113 * is restored when sleep times out.
[80bcaed]114 */
[4c60255]115 context_t sleep_timeout_context;
[a35b458]116
[80bcaed]117 /**
[df58e44]118 * From here, the stored interruption context
119 * is restored when sleep is interrupted.
[80bcaed]120 */
[4c60255]121 context_t sleep_interruption_context;
[a35b458]122
[80bcaed]123 /** If true, the thread can be interrupted from sleep. */
124 bool sleep_interruptible;
[b59318e]125
126 /**
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.
130 */
131 bool sleep_composable;
132
[80bcaed]133 /** Wait queue in which this thread sleeps. */
134 waitq_t *sleep_queue;
[a35b458]135
[80bcaed]136 /**
137 * True if this thread is executing copy_from_uspace().
138 * False otherwise.
139 */
[4c60255]140 bool in_copy_from_uspace;
[a35b458]141
[80bcaed]142 /**
143 * True if this thread is executing copy_to_uspace().
144 * False otherwise.
145 */
[4c60255]146 bool in_copy_to_uspace;
[a35b458]147
[0366d09d]148#ifdef CONFIG_FPU
149 fpu_context_t fpu_context;
150#endif
[6eef3c4]151 bool fpu_context_exists;
[a35b458]152
[4c60255]153 /*
154 * Defined only if thread doesn't run.
[80bcaed]155 * It means that fpu context is in CPU that last time executes this
156 * thread. This disables migration.
[4c60255]157 */
[6eef3c4]158 bool fpu_context_engaged;
[a35b458]159
[43ac0cc]160 /* The thread will not be migrated if nomigrate is non-zero. */
[6eef3c4]161 unsigned int nomigrate;
[a35b458]162
[6eef3c4]163 /** Thread state. */
[80bcaed]164 state_t state;
[a35b458]165
[6eef3c4]166 /** Thread CPU. */
[80bcaed]167 cpu_t *cpu;
168 /** Containing task. */
169 task_t *task;
[6eef3c4]170 /** Thread is wired to CPU. */
171 bool wired;
172 /** Thread was migrated to another CPU and has not run yet. */
173 bool stolen;
174 /** Thread is executed in user space. */
175 bool uspace;
[a35b458]176
[80bcaed]177 /** Thread accounting. */
[a2a00e8]178 uint64_t ucycles;
179 uint64_t kcycles;
[80bcaed]180 /** Last sampled cycle. */
181 uint64_t last_cycle;
[da1bafb]182 /** Thread doesn't affect accumulated accounting. */
[80bcaed]183 bool uncounted;
[a35b458]184
[80bcaed]185 /** Thread's priority. Implemented as index to CPU->rq */
186 int priority;
187 /** Thread ID. */
[201abde]188 thread_id_t tid;
[8a64e81e]189
[80bcaed]190 /** Architecture-specific data. */
191 thread_arch_t arch;
[a35b458]192
[80bcaed]193 /** Thread's kernel stack. */
194 uint8_t *kstack;
[a35b458]195
[9a1b20c]196#ifdef CONFIG_UDEBUG
[5b7a107]197 /**
198 * If true, the scheduler will print a stack trace
199 * to the kernel console upon scheduling this thread.
200 */
201 bool btrace;
[a35b458]202
[9a1b20c]203 /** Debugging stuff */
204 udebug_thread_t udebug;
[da1bafb]205#endif /* CONFIG_UDEBUG */
[4c60255]206} thread_t;
207
[da1bafb]208IRQ_SPINLOCK_EXTERN(threads_lock);
[ef1eab7]209extern odict_t threads;
[f761f1eb]210
211extern void thread_init(void);
[da1bafb]212extern thread_t *thread_create(void (*)(void *), void *, task_t *,
[6eef3c4]213 thread_flags_t, const char *);
214extern void thread_wire(thread_t *, cpu_t *);
[d52b82ad]215extern void thread_attach(thread_t *, task_t *);
216extern void thread_ready(thread_t *);
[874621f]217extern void thread_exit(void) __attribute__((noreturn));
[78acbc72]218extern void thread_interrupt(thread_t *, bool);
[f761f1eb]219
[1871118]220static inline thread_t *thread_ref(thread_t *thread)
221{
222 refcount_up(&thread->refcount);
223 return thread;
224}
225
226static inline thread_t *thread_try_ref(thread_t *thread)
227{
228 if (refcount_try_up(&thread->refcount))
229 return thread;
230 else
231 return NULL;
232}
233
234extern void thread_put(thread_t *);
235
[3fa424a9]236#ifndef thread_create_arch
[deacd722]237extern errno_t thread_create_arch(thread_t *, thread_flags_t);
[3fa424a9]238#endif
[da1bafb]239
[32fffef0]240#ifndef thr_constructor_arch
[d52b82ad]241extern void thr_constructor_arch(thread_t *);
[32fffef0]242#endif
[da1bafb]243
[32fffef0]244#ifndef thr_destructor_arch
[d52b82ad]245extern void thr_destructor_arch(thread_t *);
[32fffef0]246#endif
[3fa424a9]247
[d52b82ad]248extern void thread_sleep(uint32_t);
249extern void thread_usleep(uint32_t);
[f761f1eb]250
[5110d0a]251extern errno_t thread_join(thread_t *);
[b7fd2a0]252extern errno_t thread_join_timeout(thread_t *, uint32_t, unsigned int);
[fe19611]253
[48dcc69]254extern void thread_print_list(bool);
[e1b6742]255extern thread_t *thread_find_by_id(thread_id_t);
[aab5e46]256extern size_t thread_count(void);
257extern thread_t *thread_first(void);
258extern thread_t *thread_next(thread_t *);
[a2a00e8]259extern void thread_update_accounting(bool);
[1871118]260extern thread_t *thread_try_get(thread_t *);
[f761f1eb]261
[43ac0cc]262extern void thread_migration_disable(void);
263extern void thread_migration_enable(void);
264
[5b7a107]265#ifdef CONFIG_UDEBUG
[df58e44]266extern void thread_stack_trace(thread_id_t);
[5b7a107]267#endif
[f761f1eb]268
[80bcaed]269/** Fpu context slab cache. */
[82d515e9]270extern slab_cache_t *fpu_context_cache;
[f76fed4]271
[80bcaed]272/* Thread syscall prototypes. */
[5a5269d]273extern sys_errno_t sys_thread_create(uspace_ptr_uspace_arg_t, uspace_ptr_char, size_t,
274 uspace_ptr_thread_id_t);
[b7fd2a0]275extern sys_errno_t sys_thread_exit(int);
[5a5269d]276extern sys_errno_t sys_thread_get_id(uspace_ptr_thread_id_t);
[b7fd2a0]277extern sys_errno_t sys_thread_usleep(uint32_t);
278extern sys_errno_t sys_thread_udelay(uint32_t);
[9f52563]279
[f761f1eb]280#endif
[b45c443]281
[764c302]282/** @}
[b45c443]283 */
Note: See TracBrowser for help on using the repository browser.