~jsvoboda/helenos/dnsr

« back to all changes in this revision

Viewing changes to kernel/generic/src/proc/thread.c

  • Committer: Jiri Svoboda
  • Date: 2012-11-11 21:31:03 UTC
  • mfrom: (1527.1.178 mainline)
  • Revision ID: jiri@wiwaxia-20121111213103-314bmkettwvlwj97
MergeĀ mainlineĀ changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
190
190
         */
191
191
        kmflags |= FRAME_LOWMEM;
192
192
        kmflags &= ~FRAME_HIGHMEM;
193
 
 
 
193
        
194
194
        thread->kstack = (uint8_t *) frame_alloc(STACK_FRAMES, FRAME_KA | kmflags);
195
195
        if (!thread->kstack) {
196
196
#ifdef CONFIG_FPU
235
235
        THREAD = NULL;
236
236
        
237
237
        atomic_set(&nrdy, 0);
238
 
        thread_slab = slab_cache_create("thread_slab", sizeof(thread_t), 0,
 
238
        thread_slab = slab_cache_create("thread_t", sizeof(thread_t), 0,
239
239
            thr_constructor, thr_destructor, 0);
240
240
        
241
241
#ifdef CONFIG_FPU
242
 
        fpu_context_slab = slab_cache_create("fpu_slab", sizeof(fpu_context_t),
243
 
            FPU_CONTEXT_ALIGN, NULL, NULL, 0);
 
242
        fpu_context_slab = slab_cache_create("fpu_context_t",
 
243
            sizeof(fpu_context_t), FPU_CONTEXT_ALIGN, NULL, NULL, 0);
244
244
#endif
245
245
        
246
246
        avltree_create(&threads_tree);
247
247
}
248
248
 
 
249
/** Wire thread to the given CPU
 
250
 *
 
251
 * @param cpu CPU to wire the thread to.
 
252
 *
 
253
 */
 
254
void thread_wire(thread_t *thread, cpu_t *cpu)
 
255
{
 
256
        irq_spinlock_lock(&thread->lock, true);
 
257
        thread->cpu = cpu;
 
258
        thread->wired = true;
 
259
        irq_spinlock_unlock(&thread->lock, true);
 
260
}
 
261
 
249
262
/** Make thread ready
250
263
 *
251
264
 * Switch thread to the ready state.
259
272
        
260
273
        ASSERT(thread->state != Ready);
261
274
        
262
 
        int i = (thread->priority < RQ_COUNT - 1)
263
 
            ? ++thread->priority : thread->priority;
 
275
        int i = (thread->priority < RQ_COUNT - 1) ?
 
276
            ++thread->priority : thread->priority;
264
277
        
265
 
        cpu_t *cpu = CPU;
266
 
        if (thread->flags & THREAD_FLAG_WIRED) {
 
278
        cpu_t *cpu;
 
279
        if (thread->wired || thread->nomigrate || thread->fpu_context_engaged) {
267
280
                ASSERT(thread->cpu != NULL);
268
281
                cpu = thread->cpu;
269
 
        }
 
282
        } else
 
283
                cpu = CPU;
 
284
        
270
285
        thread->state = Ready;
271
286
        
272
287
        irq_spinlock_pass(&thread->lock, &(cpu->rq[i].lock));
297
312
 *                  call. The task's lock may not be held.
298
313
 * @param flags     Thread flags.
299
314
 * @param name      Symbolic name (a copy is made).
300
 
 * @param uncounted Thread's accounting doesn't affect accumulated task
301
 
 *                  accounting.
302
315
 *
303
316
 * @return New thread's structure on success, NULL on failure.
304
317
 *
305
318
 */
306
319
thread_t *thread_create(void (* func)(void *), void *arg, task_t *task,
307
 
    unsigned int flags, const char *name, bool uncounted)
 
320
    thread_flags_t flags, const char *name)
308
321
{
309
322
        thread_t *thread = (thread_t *) slab_alloc(thread_slab, 0);
310
323
        if (!thread)
334
347
        thread->ticks = -1;
335
348
        thread->ucycles = 0;
336
349
        thread->kcycles = 0;
337
 
        thread->uncounted = uncounted;
 
350
        thread->uncounted =
 
351
            ((flags & THREAD_FLAG_UNCOUNTED) == THREAD_FLAG_UNCOUNTED);
338
352
        thread->priority = -1;          /* Start in rq[0] */
339
353
        thread->cpu = NULL;
340
 
        thread->flags = flags;
 
354
        thread->wired = false;
 
355
        thread->stolen = false;
 
356
        thread->uspace =
 
357
            ((flags & THREAD_FLAG_USPACE) == THREAD_FLAG_USPACE);
 
358
        
341
359
        thread->nomigrate = 0;
342
360
        thread->state = Entering;
343
361
        
355
373
        
356
374
        thread->task = task;
357
375
        
358
 
        thread->fpu_context_exists = 0;
359
 
        thread->fpu_context_engaged = 0;
 
376
        thread->fpu_context_exists = false;
 
377
        thread->fpu_context_engaged = false;
360
378
        
361
379
        avltree_node_initialize(&thread->threads_tree_node);
362
380
        thread->threads_tree_node.key = (uintptr_t) thread;
370
388
        /* Might depend on previous initialization */
371
389
        thread_create_arch(thread);
372
390
        
373
 
        if (!(flags & THREAD_FLAG_NOATTACH))
 
391
        if ((flags & THREAD_FLAG_NOATTACH) != THREAD_FLAG_NOATTACH)
374
392
                thread_attach(thread, task);
375
393
        
376
394
        return thread;
436
454
        task_hold(task);
437
455
        
438
456
        /* Must not count kbox thread into lifecount */
439
 
        if (thread->flags & THREAD_FLAG_USPACE)
 
457
        if (thread->uspace)
440
458
                atomic_inc(&task->lifecount);
441
459
        
442
460
        list_append(&thread->th_link, &task->threads);
458
476
 */
459
477
void thread_exit(void)
460
478
{
461
 
        if (THREAD->flags & THREAD_FLAG_USPACE) {
 
479
        if (THREAD->uspace) {
462
480
#ifdef CONFIG_UDEBUG
463
481
                /* Generate udebug THREAD_E event */
464
482
                udebug_thread_e_event();
465
 
 
 
483
                
466
484
                /*
467
485
                 * This thread will not execute any code or system calls from
468
486
                 * now on.
505
523
void thread_migration_disable(void)
506
524
{
507
525
        ASSERT(THREAD);
508
 
 
 
526
        
509
527
        THREAD->nomigrate++;
510
528
}
511
529
 
514
532
{
515
533
        ASSERT(THREAD);
516
534
        ASSERT(THREAD->nomigrate > 0);
517
 
 
518
 
        THREAD->nomigrate--;
 
535
        
 
536
        if (THREAD->nomigrate > 0)
 
537
                THREAD->nomigrate--;
519
538
}
520
539
 
521
540
/** Thread sleep
864
883
        }
865
884
        
866
885
        thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
867
 
            THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf, false);
 
886
            THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf);
868
887
        if (thread) {
869
888
                if (uspace_thread_id != NULL) {
870
889
                        rc = copy_to_uspace(uspace_thread_id, &thread->tid,