Example #1
0
/*
 * Create a new thread.
 *
 * This function allocates a new context, and a new Thread
 * structure, and it adds the thread to the Ready queue.
 */
Thread * sthread_create(void (*f)(void *arg), void *arg) {
    Thread *threadp;
    void *memory;

    /* Create a stack for use by the thread */
    memory = (void *) malloc(DEFAULT_STACKSIZE);
    if (memory == NULL) {
        fprintf(stderr, "Can't allocate a stack for the new thread\n");
        exit(1);
    }

    /* Create a thread struct */
    threadp = (Thread *) malloc(sizeof(Thread));
    if (threadp == NULL) {
        fprintf(stderr, "Can't allocate a thread context\n");
        exit(1);
    }

    /* Initialize the thread */
    threadp->state = ThreadReady;
    threadp->memory = memory;
    threadp->context = __sthread_initialize_context(
        (char *) memory + DEFAULT_STACKSIZE, f, arg);
    queue_add(threadp);

    return threadp;
}
Example #2
0
/*
 * Create a new thread.
 *
 * This function allocates a new context, and a new Thread
 * structure, and it adds the thread to the Ready queue.
 */
Thread * sthread_create(void (*f)(void *arg), void *arg) {
    /* Allocate memory for a new thread. */
    Thread * new_thread = (Thread *) malloc(sizeof(Thread));

    /* Allocate memory for a stack for the thread. */
    void * new_stack = (void *) malloc(DEFAULT_STACKSIZE);

    /* Make sure mallocs worked. */
    if (new_thread == NULL) {
        printf("Thread was not allocated.\n");
    }

    if (new_stack == NULL) {
        printf("Stack was not allocated.\n");
    }

    /* Set thread's stack. */
    new_thread->memory = new_stack;

    /* Set thread to ready. */
    new_thread->state = ThreadReady;

    /* Set contect to end of stack (because it grows down). */
    new_thread->context = __sthread_initialize_context((char *) new_stack +
        DEFAULT_STACKSIZE, f, arg);

    /* Add to queue. */
    queue_add(new_thread);

    return new_thread;
}
Example #3
0
/*
 * Create a new thread.
 *
 * This function allocates a new context, and a new Thread
 * structure, and it adds the thread to the Ready queue.
 */
Thread * sthread_create(void (*f)(void *arg), void *arg) {
    // Allocate memory for the thread's stack and the thread itself
    void *memory = malloc(DEFAULT_STACKSIZE);
    Thread *thread = malloc(sizeof(Thread));

    // Set thread's variables
    thread->memory = memory;
    thread->state = ThreadReady;
    thread->context = __sthread_initialize_context(memory + DEFAULT_STACKSIZE,
            f, arg);

    // Add thread to ready queue
    queue_add(thread);

    return thread;
}
Example #4
0
/*
 * Create a new thread.
 *
 * This function allocates a new context, and a new Thread
 * structure, and it adds the thread to the Ready queue.
 */
Thread * sthread_create(void (*f)(void *arg), void *arg) {
    /* Allocate space for the new Thread's stack. */
    void *newStack = malloc(DEFAULT_STACKSIZE);
    
    /* Allocate space for the Thread itself. */
    Thread *thread = (Thread *)malloc(sizeof(Thread));
    
    /* Set the new Thread's pointer to its stack. */
    thread->memory = newStack;
    
    /* Initialize the Thread's context. */
    thread->context = __sthread_initialize_context(
        (char *)newStack + DEFAULT_STACKSIZE, f, arg);
    
    /* Mark the Thread as Ready. */
    thread->state = ThreadReady;
    
    /* Add the Thread to the Ready queue. */
    queue_add(thread);
    
    return thread;
}