blob: 7753ad4456281328d8e08925baa4f054adae7f59 [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
43#define THREAD_MAGIC 'thrd'
44
45typedef struct thread {
46 int magic;
47 struct list_node thread_list_node;
48
49 /* active bits */
50 struct list_node queue_node;
51 int priority;
52 enum thread_state state;
53 int saved_critical_section_count;
54 int remaining_quantum;
55
56 /* if blocked, a pointer to the wait queue */
57 struct wait_queue *blocking_wait_queue;
58 status_t wait_queue_block_ret;
59
60 /* architecture stuff */
61 struct arch_thread arch;
62
63 /* stack stuff */
64 void *stack;
65 size_t stack_size;
66
67 /* entry point */
68 thread_start_routine entry;
69 void *arg;
70
71 /* return code */
72 int retcode;
73
74 char name[32];
75} thread_t;
76
77/* thread priority */
78#define NUM_PRIORITIES 32
79#define LOWEST_PRIORITY 0
80#define HIGHEST_PRIORITY (NUM_PRIORITIES - 1)
81#define DPC_PRIORITY (NUM_PRIORITIES - 2)
82#define IDLE_PRIORITY LOWEST_PRIORITY
83#define LOW_PRIORITY (NUM_PRIORITIES / 4)
84#define DEFAULT_PRIORITY (NUM_PRIORITIES / 2)
85#define HIGH_PRIORITY ((NUM_PRIORITIES / 4) * 3)
86
87/* stack size */
88#define DEFAULT_STACK_SIZE 8192
89
90/* functions */
91void thread_init(void);
92void thread_become_idle(void) __NO_RETURN;
93void thread_set_name(const char *name);
94void thread_set_priority(int priority);
95thread_t *thread_create(const char *name, thread_start_routine entry, void *arg, int priority, size_t stack_size);
96status_t thread_resume(thread_t *);
97void thread_exit(int retcode) __NO_RETURN;
98void thread_sleep(time_t delay);
99
100void dump_thread(thread_t *t);
101void dump_all_threads(void);
102
103/* scheduler routines */
104void thread_yield(void); /* give up the cpu voluntarily */
105void thread_preempt(void); /* get preempted (inserted into head of run queue) */
106void thread_block(void); /* block on something and reschedule */
107
108/* called on every timer tick for the scheduler to do quantum expiration */
109enum handler_return thread_timer_tick(void);
110
111/* the current thread */
112extern thread_t *current_thread;
113
114/* the idle thread */
115extern thread_t *idle_thread;
116
117/* critical sections */
118extern int critical_section_count;
119
120static inline void enter_critical_section(void)
121{
122 critical_section_count++;
123 if (critical_section_count == 1)
124 arch_disable_ints();
125}
126
127static inline void exit_critical_section(void)
128{
129 critical_section_count--;
130 if (critical_section_count == 0)
131 arch_enable_ints();
132}
133
134static inline bool in_critical_section(void)
135{
136 return critical_section_count > 0;
137}
138
139/* only used by interrupt glue */
140static inline void inc_critical_section(void) { critical_section_count++; }
141static inline void dec_critical_section(void) { critical_section_count--; }
142
143/* wait queue stuff */
144#define WAIT_QUEUE_MAGIC 'wait'
145
146typedef struct wait_queue {
147 int magic;
148 struct list_node list;
149 int count;
150} wait_queue_t;
151
152/* wait queue primitive */
153/* NOTE: must be inside critical section when using these */
154void wait_queue_init(wait_queue_t *);
155
156/*
157 * release all the threads on this wait queue with a return code of ERR_OBJECT_DESTROYED.
158 * the caller must assure that no other threads are operating on the wait queue during or
159 * after the call.
160 */
161void wait_queue_destroy(wait_queue_t *, bool reschedule);
162
163/*
164 * block on a wait queue.
165 * return status is whatever the caller of wait_queue_wake_*() specifies.
166 * a timeout other than INFINITE_TIME will set abort after the specified time
167 * and return ERR_TIMED_OUT. a timeout of 0 will immediately return.
168 */
169status_t wait_queue_block(wait_queue_t *, time_t timeout);
170
171/*
172 * release one or more threads from the wait queue.
173 * reschedule = should the system reschedule if any is released.
174 * wait_queue_error = what wait_queue_block() should return for the blocking thread.
175 */
176int wait_queue_wake_one(wait_queue_t *, bool reschedule, status_t wait_queue_error);
177int wait_queue_wake_all(wait_queue_t *, bool reschedule, status_t wait_queue_error);
178
179/*
180 * remove the thread from whatever wait queue it's in.
181 * return an error if the thread is not currently blocked (or is the current thread)
182 */
183status_t thread_unblock_from_wait_queue(thread_t *t, bool reschedule, status_t wait_queue_error);
184
185/* thread level statistics */
186#define THREAD_STATS 0
187#if THREAD_STATS
188struct thread_stats {
189 bigtime_t idle_time;
190 bigtime_t last_idle_timestamp;
191 int reschedules;
192 int context_switches;
193 int preempts;
194 int yields;
195 int interrupts; /* platform code increment this */
196 int timer_ints; /* timer code increment this */
197 int timers; /* timer code increment this */
198};
199
200extern struct thread_stats thread_stats;
201
202#endif
203
204#endif
205