Rev 788 | Rev 822 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 788 | Rev 814 | ||
---|---|---|---|
Line 27... | Line 27... | ||
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <proc/scheduler.h> |
29 | #include <proc/scheduler.h> |
30 | #include <proc/thread.h> |
30 | #include <proc/thread.h> |
31 | #include <proc/task.h> |
31 | #include <proc/task.h> |
32 | #include <mm/heap.h> |
- | |
33 | #include <mm/frame.h> |
32 | #include <mm/frame.h> |
34 | #include <mm/page.h> |
33 | #include <mm/page.h> |
35 | #include <arch/asm.h> |
34 | #include <arch/asm.h> |
36 | #include <arch.h> |
35 | #include <arch.h> |
37 | #include <synch/synch.h> |
36 | #include <synch/synch.h> |
Line 93... | Line 92... | ||
93 | 92 | ||
94 | /** Initialization and allocation for thread_t structure */ |
93 | /** Initialization and allocation for thread_t structure */ |
95 | static int thr_constructor(void *obj, int kmflags) |
94 | static int thr_constructor(void *obj, int kmflags) |
96 | { |
95 | { |
97 | thread_t *t = (thread_t *)obj; |
96 | thread_t *t = (thread_t *)obj; |
- | 97 | pfn_t pfn; |
|
98 | 98 | ||
99 | spinlock_initialize(&t->lock, "thread_t_lock"); |
99 | spinlock_initialize(&t->lock, "thread_t_lock"); |
100 | link_initialize(&t->rq_link); |
100 | link_initialize(&t->rq_link); |
101 | link_initialize(&t->wq_link); |
101 | link_initialize(&t->wq_link); |
102 | link_initialize(&t->th_link); |
102 | link_initialize(&t->th_link); |
103 | link_initialize(&t->threads_link); |
103 | link_initialize(&t->threads_link); |
104 | 104 | ||
105 | t->kstack = (__u8 *)frame_alloc(ONE_FRAME, FRAME_KA | kmflags); |
105 | pfn = frame_alloc(ONE_FRAME, FRAME_KA | kmflags); |
- | 106 | t->kstack = (__u8 *)PA2KA(PFN2ADDR(pfn)); |
|
106 | if (!t->kstack) |
107 | if (!t->kstack) |
107 | return -1; |
108 | return -1; |
108 | 109 | ||
109 | return 0; |
110 | return 0; |
110 | } |
111 | } |
Line 112... | Line 113... | ||
112 | /** Destruction of thread_t object */ |
113 | /** Destruction of thread_t object */ |
113 | static int thr_destructor(void *obj) |
114 | static int thr_destructor(void *obj) |
114 | { |
115 | { |
115 | thread_t *t = (thread_t *)obj; |
116 | thread_t *t = (thread_t *)obj; |
116 | 117 | ||
117 | frame_free((__address) t->kstack); |
118 | frame_free(ADDR2PFN(KA2PA(t->kstack))); |
118 | return 1; /* One page freed */ |
119 | return 1; /* One page freed */ |
119 | } |
120 | } |
120 | 121 | ||
121 | /** Initialize threads |
122 | /** Initialize threads |
122 | * |
123 | * |
Line 191... | Line 192... | ||
191 | spinlock_lock(&t->cpu->lock); |
192 | spinlock_lock(&t->cpu->lock); |
192 | if(t->cpu->fpu_owner==t) |
193 | if(t->cpu->fpu_owner==t) |
193 | t->cpu->fpu_owner=NULL; |
194 | t->cpu->fpu_owner=NULL; |
194 | spinlock_unlock(&t->cpu->lock); |
195 | spinlock_unlock(&t->cpu->lock); |
195 | 196 | ||
196 | if (t->ustack) |
- | |
197 | frame_free((__address) t->ustack); |
- | |
198 | - | ||
199 | /* |
197 | /* |
200 | * Detach from the containing task. |
198 | * Detach from the containing task. |
201 | */ |
199 | */ |
202 | spinlock_lock(&t->task->lock); |
200 | spinlock_lock(&t->task->lock); |
203 | list_remove(&t->th_link); |
201 | list_remove(&t->th_link); |
Line 226... | Line 224... | ||
226 | * |
224 | * |
227 | */ |
225 | */ |
228 | thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags) |
226 | thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags) |
229 | { |
227 | { |
230 | thread_t *t; |
228 | thread_t *t; |
231 | __address frame_us = NULL; |
- | |
232 | 229 | ||
233 | t = (thread_t *) slab_alloc(thread_slab, 0); |
230 | t = (thread_t *) slab_alloc(thread_slab, 0); |
234 | if (t) { |
231 | if (t) { |
235 | ipl_t ipl; |
232 | ipl_t ipl; |
236 | 233 | ||
237 | if (THREAD_USER_STACK & flags) { |
- | |
238 | frame_us = frame_alloc(ONE_FRAME, FRAME_KA); |
- | |
239 | } |
- | |
240 | - | ||
241 | /* Not needed, but good for debugging */ |
234 | /* Not needed, but good for debugging */ |
242 | memsetb((__address)t->kstack, THREAD_STACK_SIZE, 0); |
235 | memsetb((__address)t->kstack, THREAD_STACK_SIZE, 0); |
243 | 236 | ||
244 | ipl = interrupts_disable(); |
237 | ipl = interrupts_disable(); |
245 | spinlock_lock(&tidlock); |
238 | spinlock_lock(&tidlock); |
246 | t->tid = ++last_tid; |
239 | t->tid = ++last_tid; |
247 | spinlock_unlock(&tidlock); |
240 | spinlock_unlock(&tidlock); |
248 | interrupts_restore(ipl); |
241 | interrupts_restore(ipl); |
249 | 242 | ||
250 | t->ustack = (__u8 *) frame_us; |
- | |
251 | - | ||
252 | context_save(&t->saved_context); |
243 | context_save(&t->saved_context); |
253 | context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE); |
244 | context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE); |
254 | 245 | ||
255 | the_initialize((the_t *) t->kstack); |
246 | the_initialize((the_t *) t->kstack); |
256 | 247 |