void *fnsort(void *arg) {
    struct pthrarg *pargs;
    int *num, swap;
    mypthread_mutex_t *mtx0, *mtx1;

    pargs = (struct pthrarg *) arg;
    num = pargs->num;
    mtx0 = pargs->mtx;
    mtx1 = pargs->mtx + 1;

    while (!quitting) {
        mypthread_mutex_lock(mtx0);
        if (mypthread_mutex_trylock(mtx1) != 0) {
            mypthread_mutex_unlock(mtx0);
            mypthread_yield();
            continue;
        }

        if (num[1] < num[0]) {
            swap = num[0];
            num[0] = num[1];
            num[1] = swap;
        }

        mypthread_mutex_unlock(mtx0);
        mypthread_mutex_unlock(mtx1);

        mypthread_yield();
    }

    mypthread_exit(0);

    // I will never get here
    return 0;
}
Пример #2
0
void mypthread_exit(void *retval)
{
  mypthread_t *curr = ttable[pos];
  curr->retval = retval;
  curr->state = TERMINATED;
  mypthread_yield();
}
void dispatch(threadpool from_me, dispatch_fn dispatch_to_here, void *arg) {
	_threadpool *pool = (_threadpool *) from_me;
	if (pool == NULL || dispatch_to_here == NULL) {
		printf("Threadpool invalid");

	}
	struct task *mytask;
	mytask = (struct task*) malloc(sizeof(struct task));
	if (mytask == NULL) {
		printf("No memory allocation");
	}
	mytask->func = (void*) dispatch_to_here;
	mytask->arg = arg;
	mypthread_mutex_lock(&(pool->mutex));

	if (&(pool->no_of_threads) == &(pool->no_of_tasks_in_Q)) {
		printf("threadpool_queue_full");
	} else {
		push_task(pool, mytask);
		if (mypthread_cond_signal(&(pool->cond))) {
			printf("threadpool_lock_failure");
		}
		mypthread_cond_signal(&(pool->cond));
	}
	if (mypthread_mutex_unlock(&(pool->mutex))) {
		printf("not unlock");
	}
	mypthread_yield();

}
Пример #4
0
void * fncheck( void *arg )
{
    struct pthrarg *pargs;
    int i, j = 0, size, check;
    mypthread_mutex_t *mtx;

    pargs = (struct pthrarg * )arg;
    mtx   = pargs->mtx;
    size  = pargs->size;

    while( !quitting )
    {
        printf( "." );
        if( (j+1) % 80 == 0 )
            printf( "\n" );

        //lock all threads
        for( i = 0; i < size; i++ )
            mypthread_mutex_lock( mtx+i );

        check = 1;
        for( i = 0; i < size-1 && check; i++ )
        {
            if( pargs->num[i] > pargs->num[i+1] )
                check = 0;
        }

        if( check )
            printf("\nQuitting...\n");
        quitting = check;

        //unlock all threads
        for( i = 0; i < size; i++ )
            mypthread_mutex_unlock( mtx+i );

        // j seconds
        j = j+1;
#ifndef MYTHREAD
       // sleep( j );
#endif
        mypthread_yield( );
    }

    mypthread_exit( 0 );

    return 0;
}