blob: 0bd286a69d122f95f9d72942ca35345cd3bce404 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
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 */
99void thread_init(void);
100void thread_become_idle(void) __NO_RETURN;
101void thread_set_name(const char *name);
102void thread_set_priority(int priority);
103thread_t *thread_create(const char *name, thread_start_routine entry, void *arg, int priority, size_t stack_size);
104status_t thread_resume(thread_t *);
105void thread_exit(int retcode) __NO_RETURN;
106void thread_sleep(time_t delay);
107
108void dump_thread(thread_t *t);
109void dump_all_threads(void);
110
111/* scheduler routines */
112void thread_yield(void); /* give up the cpu voluntarily */
113void thread_preempt(void); /* get preempted (inserted into head of run queue) */
114void thread_block(void); /* block on something and reschedule */
115
116/* called on every timer tick for the scheduler to do quantum expiration */
117enum handler_return thread_timer_tick(void);
118
119/* the current thread */
120extern thread_t *current_thread;
121
122/* the idle thread */
123extern thread_t *idle_thread;
124
125/* critical sections */
126extern int critical_section_count;
127
Travis Geiselbrecht887061f2008-09-05 01:47:07 -0700128static inline __ALWAYS_INLINE void enter_critical_section(void)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700129{
130 critical_section_count++;
131 if (critical_section_count == 1)
132 arch_disable_ints();
133}
134
Travis Geiselbrecht887061f2008-09-05 01:47:07 -0700135static inline __ALWAYS_INLINE void exit_critical_section(void)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700136{
137 critical_section_count--;
138 if (critical_section_count == 0)
139 arch_enable_ints();
140}
141
Travis Geiselbrecht887061f2008-09-05 01:47:07 -0700142static inline __ALWAYS_INLINE bool in_critical_section(void)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700143{
144 return critical_section_count > 0;
145}
146
147/* only used by interrupt glue */
148static inline void inc_critical_section(void) { critical_section_count++; }
149static inline void dec_critical_section(void) { critical_section_count--; }
150
travis geiselbrechtc60a2e62008-12-22 23:46:26 +0000151/* thread local storage */
152static inline __ALWAYS_INLINE uint32_t tls_get(uint entry)
153{
154 return current_thread->tls[entry];
155}
156
157static inline __ALWAYS_INLINE uint32_t tls_set(uint entry, uint32_t val)
158{
159 uint32_t oldval = current_thread->tls[entry];
160 current_thread->tls[entry] = val;
161 return oldval;
162}
163
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700164/* wait queue stuff */
165#define WAIT_QUEUE_MAGIC 'wait'
166
167typedef struct wait_queue {
168 int magic;
169 struct list_node list;
170 int count;
171} wait_queue_t;
172
173/* wait queue primitive */
174/* NOTE: must be inside critical section when using these */
175void wait_queue_init(wait_queue_t *);
176
177/*
178 * release all the threads on this wait queue with a return code of ERR_OBJECT_DESTROYED.
179 * the caller must assure that no other threads are operating on the wait queue during or
180 * after the call.
181 */
182void wait_queue_destroy(wait_queue_t *, bool reschedule);
183
184/*
185 * block on a wait queue.
186 * return status is whatever the caller of wait_queue_wake_*() specifies.
187 * a timeout other than INFINITE_TIME will set abort after the specified time
188 * and return ERR_TIMED_OUT. a timeout of 0 will immediately return.
189 */
190status_t wait_queue_block(wait_queue_t *, time_t timeout);
191
192/*
193 * release one or more threads from the wait queue.
194 * reschedule = should the system reschedule if any is released.
195 * wait_queue_error = what wait_queue_block() should return for the blocking thread.
196 */
197int wait_queue_wake_one(wait_queue_t *, bool reschedule, status_t wait_queue_error);
198int wait_queue_wake_all(wait_queue_t *, bool reschedule, status_t wait_queue_error);
199
200/*
201 * remove the thread from whatever wait queue it's in.
202 * return an error if the thread is not currently blocked (or is the current thread)
203 */
204status_t thread_unblock_from_wait_queue(thread_t *t, bool reschedule, status_t wait_queue_error);
205
206/* thread level statistics */
207#define THREAD_STATS 0
208#if THREAD_STATS
209struct thread_stats {
210 bigtime_t idle_time;
211 bigtime_t last_idle_timestamp;
212 int reschedules;
213 int context_switches;
214 int preempts;
215 int yields;
216 int interrupts; /* platform code increment this */
217 int timer_ints; /* timer code increment this */
218 int timers; /* timer code increment this */
219};
220
221extern struct thread_stats thread_stats;
222
223#endif
224
225#endif
226