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

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

Reimplement waitq using thread_wait/wakeup

This adds a few functions to the thread API which can be
summarized as "stop running until woken up by others".
The ordering and context-switching concerns are thus yeeted
to this abstraction and waitq only deals with maintaining
the queues. Overall, this makes the control flow in waitq
much easier to navigate.

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