Rev 2103 | Rev 2109 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2103 | Rev 2106 | ||
|---|---|---|---|
| Line 50... | Line 50... | ||
| 50 | #define THREAD_NAME_BUFLEN 20 |
50 | #define THREAD_NAME_BUFLEN 20 |
| 51 | 51 | ||
| 52 | extern char *thread_states[]; |
52 | extern char *thread_states[]; |
| 53 | 53 | ||
| 54 | /* Thread flags */ |
54 | /* Thread flags */ |
| - | 55 | ||
| 55 | #define THREAD_FLAG_WIRED (1 << 0) /**< Thread cannot be migrated to another CPU. */ |
56 | /** Thread cannot be migrated to another CPU. */ |
| - | 57 | #define THREAD_FLAG_WIRED (1 << 0) |
|
| 56 | #define THREAD_FLAG_STOLEN (1 << 1) /**< Thread was migrated to another CPU and has not run yet. */ |
58 | /** Thread was migrated to another CPU and has not run yet. */ |
| - | 59 | #define THREAD_FLAG_STOLEN (1 << 1) |
|
| 57 | #define THREAD_FLAG_USPACE (1 << 2) /**< Thread executes in userspace. */ |
60 | /** Thread executes in userspace. */ |
| - | 61 | #define THREAD_FLAG_USPACE (1 << 2) |
|
| 58 | 62 | ||
| 59 | /** Thread states. */ |
63 | /** Thread states. */ |
| 60 | typedef enum { |
64 | typedef enum { |
| 61 | Invalid, /**< It is an error, if thread is found in this state. */ |
65 | /** It is an error, if thread is found in this state. */ |
| - | 66 | Invalid, |
|
| 62 | Running, /**< State of a thread that is currently executing on some CPU. */ |
67 | /** State of a thread that is currently executing on some CPU. */ |
| - | 68 | Running, |
|
| 63 | Sleeping, /**< Thread in this state is waiting for an event. */ |
69 | /** Thread in this state is waiting for an event. */ |
| - | 70 | Sleeping, |
|
| 64 | Ready, /**< State of threads in a run queue. */ |
71 | /** State of threads in a run queue. */ |
| - | 72 | Ready, |
|
| 65 | Entering, /**< Threads are in this state before they are first readied. */ |
73 | /** Threads are in this state before they are first readied. */ |
| - | 74 | Entering, |
|
| 66 | Exiting, /**< After a thread calls thread_exit(), it is put into Exiting state. */ |
75 | /** After a thread calls thread_exit(), it is put into Exiting state. */ |
| - | 76 | Exiting, |
|
| 67 | Undead /**< Threads that were not detached but exited are in the Undead state. */ |
77 | /** Threads that were not detached but exited are in the Undead state. */ |
| - | 78 | Undead |
|
| 68 | } state_t; |
79 | } state_t; |
| 69 | 80 | ||
| 70 | /** Join types. */ |
81 | /** Join types. */ |
| 71 | typedef enum { |
82 | typedef enum { |
| 72 | None, |
83 | None, |
| Line 74... | Line 85... | ||
| 74 | TaskGC /**< The thread will be joined by ktaskgc thread. */ |
85 | TaskGC /**< The thread will be joined by ktaskgc thread. */ |
| 75 | } thread_join_type_t; |
86 | } thread_join_type_t; |
| 76 | 87 | ||
| 77 | /** Thread structure. There is one per thread. */ |
88 | /** Thread structure. There is one per thread. */ |
| 78 | typedef struct thread { |
89 | typedef struct thread { |
| 79 | link_t rq_link; /**< Run queue link. */ |
90 | link_t rq_link; /**< Run queue link. */ |
| 80 | link_t wq_link; /**< Wait queue link. */ |
91 | link_t wq_link; /**< Wait queue link. */ |
| 81 | link_t th_link; /**< Links to threads within containing task. */ |
92 | link_t th_link; /**< Links to threads within containing task. */ |
| 82 | 93 | ||
| 83 | /** Lock protecting thread structure. |
94 | /** Lock protecting thread structure. |
| 84 | * |
95 | * |
| 85 | * Protects the whole thread structure except list links above. |
96 | * Protects the whole thread structure except list links above. |
| 86 | */ |
97 | */ |
| 87 | SPINLOCK_DECLARE(lock); |
98 | SPINLOCK_DECLARE(lock); |
| 88 | 99 | ||
| 89 | char name[THREAD_NAME_BUFLEN]; |
100 | char name[THREAD_NAME_BUFLEN]; |
| 90 | 101 | ||
| 91 | void (* thread_code)(void *); /**< Function implementing the thread. */ |
102 | /** Function implementing the thread. */ |
| - | 103 | void (* thread_code)(void *); |
|
| 92 | void *thread_arg; /**< Argument passed to thread_code() function. */ |
104 | /** Argument passed to thread_code() function. */ |
| - | 105 | void *thread_arg; |
|
| 93 | 106 | ||
| - | 107 | /** |
|
| 94 | /** From here, the stored context is restored when the thread is scheduled. */ |
108 | * From here, the stored context is restored when the thread is |
| - | 109 | * scheduled. |
|
| - | 110 | */ |
|
| 95 | context_t saved_context; |
111 | context_t saved_context; |
| - | 112 | /** |
|
| 96 | /** From here, the stored timeout context is restored when sleep times out. */ |
113 | * From here, the stored timeout context is restored when sleep times |
| - | 114 | * out. |
|
| - | 115 | */ |
|
| 97 | context_t sleep_timeout_context; |
116 | context_t sleep_timeout_context; |
| - | 117 | /** |
|
| 98 | /** From here, the stored interruption context is restored when sleep is interrupted. */ |
118 | * From here, the stored interruption context is restored when sleep is |
| - | 119 | * interrupted. |
|
| - | 120 | */ |
|
| 99 | context_t sleep_interruption_context; |
121 | context_t sleep_interruption_context; |
| 100 | 122 | ||
| 101 | bool sleep_interruptible; /**< If true, the thread can be interrupted from sleep. */ |
123 | /** If true, the thread can be interrupted from sleep. */ |
| - | 124 | bool sleep_interruptible; |
|
| 102 | waitq_t *sleep_queue; /**< Wait queue in which this thread sleeps. */ |
125 | /** Wait queue in which this thread sleeps. */ |
| - | 126 | waitq_t *sleep_queue; |
|
| 103 | timeout_t sleep_timeout; /**< Timeout used for timeoutable sleeping. */ |
127 | /** Timeout used for timeoutable sleeping. */ |
| - | 128 | timeout_t sleep_timeout; |
|
| 104 | volatile int timeout_pending; /**< Flag signalling sleep timeout in progress. */ |
129 | /** Flag signalling sleep timeout in progress. */ |
| - | 130 | volatile int timeout_pending; |
|
| 105 | 131 | ||
| - | 132 | /** |
|
| 106 | /** True if this thread is executing copy_from_uspace(). False otherwise. */ |
133 | * True if this thread is executing copy_from_uspace(). |
| - | 134 | * False otherwise. |
|
| - | 135 | */ |
|
| 107 | bool in_copy_from_uspace; |
136 | bool in_copy_from_uspace; |
| - | 137 | /** |
|
| 108 | /** True if this thread is executing copy_to_uspace(). False otherwise. */ |
138 | * True if this thread is executing copy_to_uspace(). |
| - | 139 | * False otherwise. |
|
| - | 140 | */ |
|
| 109 | bool in_copy_to_uspace; |
141 | bool in_copy_to_uspace; |
| 110 | 142 | ||
| 111 | /** |
143 | /** |
| 112 | * If true, the thread will not go to sleep at all and will |
144 | * If true, the thread will not go to sleep at all and will call |
| 113 | * call thread_exit() before returning to userspace. |
145 | * thread_exit() before returning to userspace. |
| 114 | */ |
146 | */ |
| 115 | bool interrupted; |
147 | bool interrupted; |
| 116 | 148 | ||
| 117 | thread_join_type_t join_type; /**< Who joinins the thread. */ |
149 | /** Who joinins the thread. */ |
| - | 150 | thread_join_type_t join_type; |
|
| 118 | bool detached; /**< If true, thread_join_timeout() cannot be used on this thread. */ |
151 | /** If true, thread_join_timeout() cannot be used on this thread. */ |
| - | 152 | bool detached; |
|
| 119 | waitq_t join_wq; /**< Waitq for thread_join_timeout(). */ |
153 | /** Waitq for thread_join_timeout(). */ |
| - | 154 | waitq_t join_wq; |
|
| 120 | 155 | ||
| 121 | fpu_context_t *saved_fpu_context; |
156 | fpu_context_t *saved_fpu_context; |
| 122 | int fpu_context_exists; |
157 | int fpu_context_exists; |
| 123 | 158 | ||
| 124 | /* |
159 | /* |
| 125 | * Defined only if thread doesn't run. |
160 | * Defined only if thread doesn't run. |
| 126 | * It means that fpu context is in CPU that last time executes this thread. |
161 | * It means that fpu context is in CPU that last time executes this |
| 127 | * This disables migration. |
162 | * thread. This disables migration. |
| 128 | */ |
163 | */ |
| 129 | int fpu_context_engaged; |
164 | int fpu_context_engaged; |
| 130 | 165 | ||
| 131 | rwlock_type_t rwlock_holder_type; |
166 | rwlock_type_t rwlock_holder_type; |
| 132 | 167 | ||
| 133 | void (* call_me)(void *); /**< Funtion to be called in scheduler before the thread is put asleep. */ |
168 | /** Callback fired in scheduler before the thread is put asleep. */ |
| - | 169 | void (* call_me)(void *); |
|
| 134 | void *call_me_with; /**< Argument passed to call_me(). */ |
170 | /** Argument passed to call_me(). */ |
| - | 171 | void *call_me_with; |
|
| 135 | 172 | ||
| 136 | state_t state; /**< Thread's state. */ |
173 | /** Thread's state. */ |
| - | 174 | state_t state; |
|
| 137 | int flags; /**< Thread's flags. */ |
175 | /** Thread's flags. */ |
| - | 176 | int flags; |
|
| 138 | 177 | ||
| 139 | cpu_t *cpu; /**< Thread's CPU. */ |
178 | /** Thread's CPU. */ |
| - | 179 | cpu_t *cpu; |
|
| 140 | task_t *task; /**< Containing task. */ |
180 | /** Containing task. */ |
| - | 181 | task_t *task; |
|
| 141 | 182 | ||
| 142 | uint64_t ticks; /**< Ticks before preemption. */ |
183 | /** Ticks before preemption. */ |
| - | 184 | uint64_t ticks; |
|
| 143 | 185 | ||
| 144 | uint64_t cycles; /**< Thread accounting. */ |
186 | /** Thread accounting. */ |
| - | 187 | uint64_t cycles; |
|
| 145 | uint64_t last_cycle; /**< Last sampled cycle. */ |
188 | /** Last sampled cycle. */ |
| - | 189 | uint64_t last_cycle; |
|
| 146 | bool uncounted; /**< Thread doesn't affect accumulated accounting. */ |
190 | /** Thread doesn't affect accumulated accounting. */ |
| - | 191 | bool uncounted; |
|
| 147 | 192 | ||
| 148 | int priority; /**< Thread's priority. Implemented as index to CPU->rq */ |
193 | /** Thread's priority. Implemented as index to CPU->rq */ |
| - | 194 | int priority; |
|
| 149 | uint32_t tid; /**< Thread ID. */ |
195 | /** Thread ID. */ |
| - | 196 | uint32_t tid; |
|
| 150 | 197 | ||
| 151 | thread_arch_t arch; /**< Architecture-specific data. */ |
198 | /** Architecture-specific data. */ |
| - | 199 | thread_arch_t arch; |
|
| 152 | 200 | ||
| 153 | uint8_t *kstack; /**< Thread's kernel stack. */ |
201 | /** Thread's kernel stack. */ |
| - | 202 | uint8_t *kstack; |
|
| 154 | } thread_t; |
203 | } thread_t; |
| 155 | 204 | ||
| 156 | /** Thread list lock. |
205 | /** Thread list lock. |
| 157 | * |
206 | * |
| 158 | * This lock protects all link_t structures chained in threads_head. |
207 | * This lock protects all link_t structures chained in threads_head. |
| 159 | * Must be acquired before T.lock for each T of type thread_t. |
208 | * Must be acquired before T.lock for each T of type thread_t. |
| 160 | * |
209 | * |
| 161 | */ |
210 | */ |
| 162 | SPINLOCK_EXTERN(threads_lock); |
211 | SPINLOCK_EXTERN(threads_lock); |
| 163 | 212 | ||
| 164 | extern btree_t threads_btree; /**< B+tree containing all threads. */ |
213 | /** B+tree containing all threads. */ |
| - | 214 | extern btree_t threads_btree; |
|
| 165 | 215 | ||
| 166 | extern void thread_init(void); |
216 | extern void thread_init(void); |
| 167 | extern thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name, bool uncounted); |
217 | extern thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, |
| - | 218 | int flags, char *name, bool uncounted); |
|
| 168 | extern void thread_ready(thread_t *t); |
219 | extern void thread_ready(thread_t *t); |
| 169 | extern void thread_exit(void) __attribute__((noreturn)); |
220 | extern void thread_exit(void) __attribute__((noreturn)); |
| 170 | 221 | ||
| 171 | #ifndef thread_create_arch |
222 | #ifndef thread_create_arch |
| 172 | extern void thread_create_arch(thread_t *t); |
223 | extern void thread_create_arch(thread_t *t); |
| Line 179... | Line 230... | ||
| 179 | #endif |
230 | #endif |
| 180 | 231 | ||
| 181 | extern void thread_sleep(uint32_t sec); |
232 | extern void thread_sleep(uint32_t sec); |
| 182 | extern void thread_usleep(uint32_t usec); |
233 | extern void thread_usleep(uint32_t usec); |
| 183 | 234 | ||
| - | 235 | #define thread_join(t) \ |
|
| 184 | #define thread_join(t) thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE) |
236 | thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE) |
| 185 | extern int thread_join_timeout(thread_t *t, uint32_t usec, int flags); |
237 | extern int thread_join_timeout(thread_t *t, uint32_t usec, int flags); |
| 186 | extern void thread_detach(thread_t *t); |
238 | extern void thread_detach(thread_t *t); |
| 187 | 239 | ||
| 188 | extern void thread_register_call_me(void (* call_me)(void *), void *call_me_with); |
240 | extern void thread_register_call_me(void (* call_me)(void *), |
| - | 241 | void *call_me_with); |
|
| 189 | extern void thread_print_list(void); |
242 | extern void thread_print_list(void); |
| 190 | extern void thread_destroy(thread_t *t); |
243 | extern void thread_destroy(thread_t *t); |
| 191 | extern void thread_update_accounting(void); |
244 | extern void thread_update_accounting(void); |
| 192 | extern bool thread_exists(thread_t *t); |
245 | extern bool thread_exists(thread_t *t); |
| 193 | extern void thread_interrupt_sleep(thread_t *t); |
246 | extern void thread_interrupt_sleep(thread_t *t); |
| 194 | 247 | ||
| 195 | /* Fpu context slab cache */ |
248 | /** Fpu context slab cache. */ |
| 196 | extern slab_cache_t *fpu_context_slab; |
249 | extern slab_cache_t *fpu_context_slab; |
| 197 | 250 | ||
| 198 | /** Thread syscall prototypes. */ |
251 | /* Thread syscall prototypes. */ |
| 199 | unative_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name); |
252 | unative_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name); |
| 200 | unative_t sys_thread_exit(int uspace_status); |
253 | unative_t sys_thread_exit(int uspace_status); |
| 201 | 254 | ||
| 202 | #endif |
255 | #endif |
| 203 | 256 | ||