blob: 6c25712affff528e67f2d3af85e1136577d95759 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
Travis Geiselbrecht9045c062009-06-28 11:20:09 -07002 * Copyright (c) 2008-2009 Travis Geiselbrecht
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#ifndef __KERNEL_THREAD_H
24#define __KERNEL_THREAD_H
25
26#include <sys/types.h>
27#include <list.h>
28#include <compiler.h>
29#include <arch/ops.h>
30#include <arch/thread.h>
31
32enum thread_state {
33 THREAD_SUSPENDED = 0,
34 THREAD_READY,
35 THREAD_RUNNING,
36 THREAD_BLOCKED,
37 THREAD_SLEEPING,
38 THREAD_DEATH,
39};
40
41typedef int (*thread_start_routine)(void *arg);
42
travis geiselbrechtc60a2e62008-12-22 23:46:26 +000043/* thread local storage */
44enum thread_tls_list {
45 MAX_TLS_ENTRY
46};
47
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070048#define THREAD_MAGIC 'thrd'
49
50typedef struct thread {
51 int magic;
52 struct list_node thread_list_node;
53
54 /* active bits */
55 struct list_node queue_node;
56 int priority;
57 enum thread_state state;
58 int saved_critical_section_count;
59 int remaining_quantum;
Travis Geiselbrecht887061f2008-09-05 01:47:07 -070060
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070061 /* if blocked, a pointer to the wait queue */
62 struct wait_queue *blocking_wait_queue;
63 status_t wait_queue_block_ret;
64
65 /* architecture stuff */
66 struct arch_thread arch;
67
68 /* stack stuff */
69 void *stack;
70 size_t stack_size;
71
72 /* entry point */
73 thread_start_routine entry;
74 void *arg;
75
76 /* return code */
77 int retcode;
78
travis geiselbrechtc60a2e62008-12-22 23:46:26 +000079 /* thread local storage */
80 uint32_t tls[MAX_TLS_ENTRY];
81
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070082 char name[32];
83} thread_t;
84
85/* thread priority */
86#define NUM_PRIORITIES 32
87#define LOWEST_PRIORITY 0
88#define HIGHEST_PRIORITY (NUM_PRIORITIES - 1)
89#define DPC_PRIORITY (NUM_PRIORITIES - 2)
90#define IDLE_PRIORITY LOWEST_PRIORITY
91#define LOW_PRIORITY (NUM_PRIORITIES / 4)
92#define DEFAULT_PRIORITY (NUM_PRIORITIES / 2)
93#define HIGH_PRIORITY ((NUM_PRIORITIES / 4) * 3)
94
95/* stack size */
96#define DEFAULT_STACK_SIZE 8192
97
98/* functions */
travis geiselbrecht858b1ad2008-12-23 21:55:41 +000099void thread_init_early(void);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700100void thread_init(void);
101void thread_become_idle(void) __NO_RETURN;
102void thread_set_name(const char *name);
103void thread_set_priority(int priority);
104thread_t *thread_create(const char *name, thread_start_routine entry, void *arg, int priority, size_t stack_size);
105status_t thread_resume(thread_t *);
106void thread_exit(int retcode) __NO_RETURN;
107void thread_sleep(time_t delay);
108
109void dump_thread(thread_t *t);
110void dump_all_threads(void);
111
112/* scheduler routines */
113void thread_yield(void); /* give up the cpu voluntarily */
114void thread_preempt(void); /* get preempted (inserted into head of run queue) */
115void thread_block(void); /* block on something and reschedule */
116
117/* called on every timer tick for the scheduler to do quantum expiration */
118enum handler_return thread_timer_tick(void);
119
120/* the current thread */
121extern thread_t *current_thread;
122
123/* the idle thread */
124extern thread_t *idle_thread;
125
126/* critical sections */
127extern int critical_section_count;
128
Travis Geiselbrecht887061f2008-09-05 01:47:07 -0700129static inline __ALWAYS_INLINE void enter_critical_section(void)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700130{
131 critical_section_count++;
132 if (critical_section_count == 1)
133 arch_disable_ints();
134}
135
Travis Geiselbrecht887061f2008-09-05 01:47:07 -0700136static inline __ALWAYS_INLINE void exit_critical_section(void)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700137{
138 critical_section_count--;
139 if (critical_section_count == 0)
140 arch_enable_ints();
141}
142
Travis Geiselbrecht887061f2008-09-05 01:47:07 -0700143static inline __ALWAYS_INLINE bool in_critical_section(void)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700144{
145 return critical_section_count > 0;
146}
147
148/* only used by interrupt glue */
149static inline void inc_critical_section(void) { critical_section_count++; }
150static inline void dec_critical_section(void) { critical_section_count--; }
151
travis geiselbrechtc60a2e62008-12-22 23:46:26 +0000152/* thread local storage */
153static inline __ALWAYS_INLINE uint32_t tls_get(uint entry)
154{
155 return current_thread->tls[entry];
156}
157
158static inline __ALWAYS_INLINE uint32_t tls_set(uint entry, uint32_t val)
159{
160 uint32_t oldval = current_thread->tls[entry];
161 current_thread->tls[entry] = val;
162 return oldval;
163}
164
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700165/* wait queue stuff */
166#define WAIT_QUEUE_MAGIC 'wait'
167
168typedef struct wait_queue {
169 int magic;
170 struct list_node list;
171 int count;
172} wait_queue_t;
173
174/* wait queue primitive */
175/* NOTE: must be inside critical section when using these */
176void wait_queue_init(wait_queue_t *);
177
178/*
179 * release all the threads on this wait queue with a return code of ERR_OBJECT_DESTROYED.
180 * the caller must assure that no other threads are operating on the wait queue during or
181 * after the call.
182 */
183void wait_queue_destroy(wait_queue_t *, bool reschedule);
184
185/*
186 * block on a wait queue.
187 * return status is whatever the caller of wait_queue_wake_*() specifies.
188 * a timeout other than INFINITE_TIME will set abort after the specified time
189 * and return ERR_TIMED_OUT. a timeout of 0 will immediately return.
190 */
191status_t wait_queue_block(wait_queue_t *, time_t timeout);
192
193/*
194 * release one or more threads from the wait queue.
195 * reschedule = should the system reschedule if any is released.
196 * wait_queue_error = what wait_queue_block() should return for the blocking thread.
197 */
198int wait_queue_wake_one(wait_queue_t *, bool reschedule, status_t wait_queue_error);
199int wait_queue_wake_all(wait_queue_t *, bool reschedule, status_t wait_queue_error);
200
201/*
202 * remove the thread from whatever wait queue it's in.
203 * return an error if the thread is not currently blocked (or is the current thread)
204 */
205status_t thread_unblock_from_wait_queue(thread_t *t, bool reschedule, status_t wait_queue_error);
206
207/* thread level statistics */
Travis Geiselbrecht9045c062009-06-28 11:20:09 -0700208#if DEBUGLEVEL > 1
209#define THREAD_STATS 1
210#else
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700211#define THREAD_STATS 0
Travis Geiselbrecht9045c062009-06-28 11:20:09 -0700212#endif
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700213#if THREAD_STATS
214struct thread_stats {
215 bigtime_t idle_time;
216 bigtime_t last_idle_timestamp;
217 int reschedules;
218 int context_switches;
219 int preempts;
220 int yields;
221 int interrupts; /* platform code increment this */
222 int timer_ints; /* timer code increment this */
223 int timers; /* timer code increment this */
224};
225
226extern struct thread_stats thread_stats;
227
228#endif
229
230#endif
231