thread.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2004 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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          * Defined only if thread doesn't run.
00129          * It means that fpu context is in CPU that last time executes this thread.
00130          * This disables migration
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 /* Fpu context slab cache */
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 

Generated on Sun Jun 18 16:26:57 2006 for HelenOS Kernel (amd64) by  doxygen 1.4.6