00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00035 #ifndef __THREAD_H__
00036 #define __THREAD_H__
00037
00038 #include <arch/proc/thread.h>
00039 #include <synch/spinlock.h>
00040 #include <arch/context.h>
00041 #include <fpu_context.h>
00042 #include <arch/types.h>
00043 #include <typedefs.h>
00044 #include <time/timeout.h>
00045 #include <synch/rwlock.h>
00046 #include <synch/synch.h>
00047 #include <config.h>
00048 #include <adt/btree.h>
00049 #include <adt/list.h>
00050 #include <mm/slab.h>
00051 #include <proc/uarg.h>
00052
00053 #define THREAD_STACK_SIZE STACK_SIZE
00054
00056 enum state {
00057 Invalid,
00058 Running,
00059 Sleeping,
00060 Ready,
00061 Entering,
00062 Exiting,
00063 Undead
00064 };
00065
00066 extern char *thread_states[];
00067
00069 typedef enum {
00070 None,
00071 TaskClnp,
00072 TaskGC
00073 } thread_join_type_t;
00074
00075 #define X_WIRED (1<<0)
00076 #define X_STOLEN (1<<1)
00077
00078 #define THREAD_NAME_BUFLEN 20
00079
00081 struct thread {
00082 link_t rq_link;
00083 link_t wq_link;
00084 link_t th_link;
00090 SPINLOCK_DECLARE(lock);
00091
00092 char name[THREAD_NAME_BUFLEN];
00093
00094 void (* thread_code)(void *);
00095 void *thread_arg;
00098 context_t saved_context;
00100 context_t sleep_timeout_context;
00102 context_t sleep_interruption_context;
00103
00104 bool sleep_interruptible;
00105 waitq_t *sleep_queue;
00106 timeout_t sleep_timeout;
00107 volatile int timeout_pending;
00110 bool in_copy_from_uspace;
00112 bool in_copy_to_uspace;
00113
00118 bool interrupted;
00119
00120 thread_join_type_t join_type;
00121 bool detached;
00122 waitq_t join_wq;
00124 fpu_context_t *saved_fpu_context;
00125 int fpu_context_exists;
00126
00127
00128
00129
00130
00131
00132 int fpu_context_engaged;
00133
00134 rwlock_type_t rwlock_holder_type;
00135
00136 void (* call_me)(void *);
00137 void *call_me_with;
00139 state_t state;
00140 int flags;
00142 cpu_t *cpu;
00143 task_t *task;
00145 __u64 ticks;
00147 int priority;
00148 __u32 tid;
00150 thread_arch_t arch;
00152 __u8 *kstack;
00153 };
00154
00161 extern spinlock_t threads_lock;
00162
00163 extern btree_t threads_btree;
00165 extern void thread_init(void);
00166 extern thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name);
00167 extern void thread_ready(thread_t *t);
00168 extern void thread_exit(void) __attribute__((noreturn));
00169
00170 #ifndef thread_create_arch
00171 extern void thread_create_arch(thread_t *t);
00172 #endif
00173
00174 extern void thread_sleep(__u32 sec);
00175 extern void thread_usleep(__u32 usec);
00176
00177 #define thread_join(t) thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
00178 extern int thread_join_timeout(thread_t *t, __u32 usec, int flags);
00179 extern void thread_detach(thread_t *t);
00180
00181 extern void thread_register_call_me(void (* call_me)(void *), void *call_me_with);
00182 extern void thread_print_list(void);
00183 extern void thread_destroy(thread_t *t);
00184 extern bool thread_exists(thread_t *t);
00185
00186
00187 extern slab_cache_t *fpu_context_slab;
00188
00190 __native sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name);
00191 __native sys_thread_exit(int uspace_status);
00192
00193 #endif
00194