Changeset 266294a9 in mainline for generic/src/proc/thread.c


Ignore:
Timestamp:
2006-02-05T17:01:03Z (19 years ago)
Author:
Ondrej Palkovsky <ondrap@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5c9a08b
Parents:
9b9e385
Message:

Added constructor/destructor calls to SLAB.
Changed allocation of thread_t structure to use SLAB.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • generic/src/proc/thread.c

    r9b9e385 r266294a9  
    5353#include <memstr.h>
    5454#include <print.h>
     55#include <mm/slab.h>
     56#include <debug.h>
    5557
    5658char *thread_states[] = {"Invalid", "Running", "Sleeping", "Ready", "Entering", "Exiting"}; /**< Thread states */
     
    6163SPINLOCK_INITIALIZE(tidlock);
    6264__u32 last_tid = 0;
     65
     66static slab_cache_t *thread_slab;
    6367
    6468
     
    8892}
    8993
     94/** Initialization and allocation for thread_t structure */
     95static int thr_constructor(void *obj, int kmflags)
     96{
     97        thread_t *t = (thread_t *)obj;
     98
     99        spinlock_initialize(&t->lock, "thread_t_lock");
     100        link_initialize(&t->rq_link);
     101        link_initialize(&t->wq_link);
     102        link_initialize(&t->th_link);
     103        link_initialize(&t->threads_link);
     104       
     105        t->kstack = (__u8 *)frame_alloc(ONE_FRAME, FRAME_KA | kmflags);
     106        if (!t->kstack)
     107                return -1;
     108
     109        return 0;
     110}
     111
     112/** Destruction of thread_t object */
     113static int thr_destructor(void *obj)
     114{
     115        thread_t *t = (thread_t *)obj;
     116
     117        frame_free((__address) t->kstack);
     118        return 1; /* One page freed */
     119}
    90120
    91121/** Initialize threads
     
    98128        THREAD = NULL;
    99129        atomic_set(&nrdy,0);
     130        thread_slab = slab_cache_create("thread_slab",
     131                                        sizeof(thread_t),0,
     132                                        thr_constructor, thr_destructor, 0);
    100133}
    101134
     
    144177
    145178
     179/** Destroy thread memory structure
     180 *
     181 * Detach thread from all queues, cpus etc. and destroy it.
     182 *
     183 * Assume thread->lock is held!!
     184 */
     185void thread_destroy(thread_t *t)
     186{
     187        ASSERT(t->state == Exiting);
     188        ASSERT(t->task);
     189        ASSERT(t->cpu);
     190
     191        spinlock_lock(&t->cpu->lock);
     192        if(t->cpu->fpu_owner==t)
     193                t->cpu->fpu_owner=NULL;
     194        spinlock_unlock(&t->cpu->lock);
     195
     196        if (t->ustack)
     197                frame_free((__address) t->ustack);
     198       
     199        /*
     200         * Detach from the containing task.
     201         */
     202        spinlock_lock(&t->task->lock);
     203        list_remove(&t->th_link);
     204        spinlock_unlock(&t->task->lock);
     205       
     206        spinlock_unlock(&t->lock);
     207       
     208        spinlock_lock(&threads_lock);
     209        list_remove(&t->threads_link);
     210        spinlock_unlock(&threads_lock);
     211       
     212        slab_free(thread_slab, t);
     213}
     214
     215
    146216/** Create new thread
    147217 *
     
    159229{
    160230        thread_t *t;
    161         __address frame_ks, frame_us = NULL;
    162 
    163         t = (thread_t *) malloc(sizeof(thread_t));
     231        __address frame_us = NULL;
     232
     233        t = (thread_t *) slab_alloc(thread_slab, 0);
    164234        if (t) {
    165235                ipl_t ipl;
    166236       
    167                 spinlock_initialize(&t->lock, "thread_t_lock");
    168        
    169                 frame_ks = frame_alloc(ONE_FRAME, FRAME_KA);
    170237                if (THREAD_USER_STACK & flags) {
    171238                        frame_us = frame_alloc(ONE_FRAME, FRAME_KA);
    172239                }
     240
     241                /* Not needed, but good for debugging */
     242                memsetb((__address)t->kstack, THREAD_STACK_SIZE, 0);
    173243
    174244                ipl = interrupts_disable();
     
    178248                interrupts_restore(ipl);
    179249               
    180                 memsetb(frame_ks, THREAD_STACK_SIZE, 0);
    181                 link_initialize(&t->rq_link);
    182                 link_initialize(&t->wq_link);
    183                 link_initialize(&t->th_link);
    184                 link_initialize(&t->threads_link);
    185                 t->kstack = (__u8 *) frame_ks;
    186250                t->ustack = (__u8 *) frame_us;
    187251               
     
    219283                 * Register this thread in the system-wide list.
    220284                 */
    221                 ipl = interrupts_disable();             
     285                ipl = interrupts_disable();
    222286                spinlock_lock(&threads_lock);
    223287                list_append(&t->threads_link, &threads_head);
Note: See TracChangeset for help on using the changeset viewer.