Пример #1
0
static void
tester(long a0, void *a1)
{
    int	i = 0, ret;
    char pbuffer[SBUFSZ];
    
    while (i < 10)
    {
        sprintf(pbuffer, "thread %i: hello! (%i)\n", uthread_self(), i++);  
        ret = write(STDOUT_FILENO, pbuffer, strlen(pbuffer));
        if (ret < 0) 
        {
            perror("uthreads_test");
            /* XXX: we should really cleanup here */
            exit(1);
        }
	
        uthread_mtx_lock(&mtx);
        uthread_cond_signal(&cond);
        uthread_cond_wait(&cond, &mtx);
        uthread_mtx_unlock(&mtx);
    }

    sprintf(pbuffer, "thread %i exiting.\n", uthread_self());  
    ret = write(STDOUT_FILENO, pbuffer, strlen(pbuffer));
    if (ret < 0) 
    {
        perror("uthreads_test");
        /* XXX: we should really cleanup here */
        exit(1);
    }

    uthread_exit(a0);
}
Пример #2
0
int
main(int ac, char **av)
{
    int	i;


/*
    char buff[50];
    memset(buff,0,50);
    sprintf(buff, "helloworld\n");
    write(STDOUT_FILENO, buff, strlen(buff));
*/
    printf("hello\n");

    uthread_init();
    uthread_mtx_init(&mtx);
    uthread_cond_init(&cond);

    for (i = 0; i < NUM_THREADS; i++)
    {
        uthread_create(&thr[i], tester, i, NULL, 0);
    }
    uthread_setprio(thr[0], 2);


    for (i = 0; i < NUM_THREADS; i++)
    {
        char pbuffer[SBUFSZ];
        int	tmp, ret;

        uthread_join(thr[i], &tmp);
    
        sprintf(pbuffer, "joined with thread %i, exited %i.\n", thr[i], tmp);  
        ret = write(STDOUT_FILENO, pbuffer, strlen(pbuffer));
        if (ret < 0) 
        {
            perror("uthreads_test");
            return EXIT_FAILURE;
        }   

        uthread_mtx_lock(&mtx);
        uthread_cond_signal(&cond);
        uthread_mtx_unlock(&mtx);
    }

    uthread_exit(0);

    return 0;
}
Пример #3
0
/*
 * uthread_cond_wait
 *
 * Should behave just like a stripped down version of pthread_cond_wait.
 * Block on the given condition variable.
 * param cond: the pointer to the conditional variable object
 * param mtx: the pointer to the mutex object
 */
void
uthread_cond_wait(uthread_cond_t *cond, uthread_mtx_t *mtx)
{
	/* Make sure the mtx is locked by the current thread */

	assert(mtx != NULL && cond != NULL);
	assert(mtx->m_owner == ut_curthr);
	

	utqueue_enqueue(&cond->uc_waiters, ut_curthr);
	uthread_mtx_unlock(mtx);

	/* Block on the conditional variable */
	uthread_block();

	/* Now it gets back, try lock the mutex */
	uthread_mtx_lock(mtx);
}
Пример #4
0
static void tester2(long a0, void* a1){
    int	i = 0, ret;
    char pbuffer[SBUFSZ];
    
    //uthread_mtx_lock(&mtx);
    while (i < 1)
    {
        i ++;
        uthread_mtx_lock(&mtx1);
        pprintf("thread2 about to block\n");

        //uthread_mtx_lock(&mtx3);
        uthread_cond_wait(&cond,&mtx1);
        sprintf(pbuffer, "thread 2 out of wait\n");  
        ret = write(STDOUT_FILENO, pbuffer, strlen(pbuffer));
        if (ret < 0) 
        {
            perror("uthreads_test");
            /* XXX: we should really cleanup here */
            exit(1);
        }
        
        uthread_mtx_unlock(&mtx1);
        //uthread_mtx_unlock(&mtx3);
               
    }

    sprintf(pbuffer, "thread 2 exiting.\n");  
    ret = write(STDOUT_FILENO, pbuffer, strlen(pbuffer));
    if (ret < 0) 
    {
        perror("uthreads_test");
        /* XXX: we should really cleanup here */
        exit(1);
    }

    uthread_exit(a0);
}