Ejemplo n.º 1
0
Archivo: test-cond.c Proyecto: kunw/P2
/* Consumer thread - remove items from the "buffer"
 * until they've all been transfered. */
void *thread_start(void *arg) {
	sthread_mutex_lock(mutex);

	while (transfered < max_transfer) {
		/* Wait for main() to put something up for us to take */
		while (transfered < max_transfer && waiting == 0) {
			sthread_cond_wait(avail_cond, mutex);

			/* This isn't part of using cond vars, but
			 * helps the test fail quickly if they aren't
			 * working properly: */
			sthread_mutex_unlock(mutex);
			sthread_yield();
			sthread_mutex_lock(mutex);
		}
		/* Either there is something waiting, or we've finished */

		if (waiting != 0) {
			/* Take it */
			waiting--;
			transfered++;
		}
	}

	sthread_mutex_unlock(mutex);

	return 0;
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
	int checks;
	
	printf("Testing sthread_mutex_*, impl: %s\n",
		   (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	
	sthread_init();
	
	mutex = sthread_mutex_init();
	sthread_mutex_lock(mutex);
	
	if (sthread_create(thread_start, (void*)1, 1) == NULL) {
		printf("sthread_create failed\n");
		exit(1);
	}
	
	/* Wait until the other thread has at least started,
	 * to give it a chance at getting through the mutex incorrectly. */
	while (ran_thread == 0) {
		sthread_yield();
	}

   sthread_dump();
	
	/* The other thread has run, but shouldn't have been
	 * able to affect counter (note that this is not a great test
	 * for preemptive scheduler, since the other thread's sequence
	 * is not atomic). */   
	assert(counter == 0);
	
	/* This should let the other thread run at some point. */
	sthread_mutex_unlock(mutex);
	
	/* Allow up to 100 checks in case the scheduler doesn't
	 * decide to run the other thread for a really long time. */
	checks = 100;
	while (checks > 0) {
		sthread_mutex_lock(mutex);
		if (counter != 0) {
			/* The other thread ran, got the lock,
			 * and incrmented the counter: test passes. */
			checks = -1;
		} else {
			checks--;
		}
		sthread_mutex_unlock(mutex);
		
		/* Nudge the scheduler to run the other thread: */
		sthread_yield();
	}
	
	if (checks == -1) {
		printf("sthread_mutex passed\n");
	} else {
		printf("*** sthread_mutex failed\n");
	}
	sthread_mutex_free(mutex);
	return 0;
}
Ejemplo n.º 3
0
/* Try and get the lock.
 * When acquired, mark a global to let main know. */
void *thread_start(void *arg) {
  /* indicate we got in to this thread. Don't try to printf() here
   * though, or the check in the main thread will fail sometimes. */
  ran_thread = 1;
  /* should wait until main() has released us */
  sthread_mutex_lock(mutex);
  counter++;
  sthread_mutex_unlock(mutex);
  return 0;
}
Ejemplo n.º 4
0
/* Try and get the lock.
 * When acquired, mark a global to let main know. */
void *thread_start(void *arg)
{
	/* indicate we got in to this thread */
	ran_thread = 1;
	/* should wait until main() has released us */
	sthread_mutex_lock(mutex);
	counter++;
	sthread_mutex_unlock(mutex);
	return 0;
}
Ejemplo n.º 5
0
void f3()
{
        int i;
        for(i=0;i<LOOP;i++)
        {
                sthread_mutex_lock(m);
                sum+=1;
                printf("sum3----------- %d\n",sum);
                sthread_mutex_unlock(m);
                usleep(TIMESLOT/10);
        }
        printf("fun3 finished\n");
}
Ejemplo n.º 6
0
int sthread_mutex_unlock(sthread_mutex_t *mutex,int tid)
{
    //enable if you want to test other thread unable to get lock...
   // sleep(1);
    int ret_val;

    if(mutex->lock==1){
        if(mutex->tid==tid){
            //check counter
            if(mutex->counter==0){
                mutex->lock = 0;
                ret_val = 0;

                if(isWaitingEmpty(mutex)){
                    printf("No threads waiting\n");
                }
                else{
                    //wake up the mutex in waiting that is in front
                    sthread_wake(mutex->waiting[mutex->front]);
                    //dequeue the mutex

                    dequeueWait(mutex);
                }
            }
            else if(mutex->counter>0){
                mutex->counter = mutex->counter - 1;
                printf("Thread %d, making recursive call to unlock lock\n",tid);

                sthread_mutex_unlock(mutex,tid);
            }
        }
        mutex->lock=0;
        ret_val = 0;

    }
    else{
        ret_val = -1;
    }

    return ret_val;

}
Ejemplo n.º 7
0
int main(int argc, char **argv) {
  sthread_mutex_t mutex = NULL;
  int i = 0;

  printf("Testing sthread_mutex_*, impl: %s\n",
     (sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
 
  sthread_init();

  mutex = sthread_mutex_init();
  while (i < 10) {
    sthread_mutex_lock(mutex);
    printf("i=%i\n",i);
    i++;
    sthread_yield();
    sthread_mutex_unlock(mutex);
  }
  printf("\nSUCESSO!\n");
  return 1;
}
Ejemplo n.º 8
0
int threadmain(void *arg)
{
    int tid = (int)arg;

    sthread_mutex_lock(mut_ptr,tid);

    sthread_mutex_lock(mut_ptr,tid);

    //I am intentionally putting a delay here to show that threads 2 and 3 become suspended.
    if(tid==1){
        sleep(10);
    }

    //critical region
    printf("Thread %d entered critical region\n",tid);

    sthread_mutex_unlock(mut_ptr,tid);

  return 0;
}
Ejemplo n.º 9
0
Archivo: test-cond.c Proyecto: kunw/P2
int main(int argc, char **argv) {
	int sent, checks, i;
	sthread_t child[MAXTHREADS];


	printf("Testing sthread_cond_*, impl: %s\n",
			(sthread_get_impl() == STHREAD_PTHREAD_IMPL) ? "pthread" : "user");
	assert(num_threads <= MAXTHREADS);

	sthread_init();

	mutex = sthread_mutex_init();
	avail_cond = sthread_cond_init();

	sthread_mutex_lock(mutex);

	for (i = 0; i < num_threads; i++) {
		child[i] = sthread_create(thread_start, NULL, 1);
		if (child[i] == NULL) {
			printf("sthread_create %d failed\n", i);
			exit(1);
		}
	}

	assert(transfered == 0);

	/* This should let the other thread run at some point. */
	sthread_mutex_unlock(mutex);

	/* Send a bunch of things for the other threads to take */
	sent = 0;
	while (sent < max_transfer) {
		sthread_mutex_lock(mutex);
		waiting++;
		sent++;
		sthread_cond_signal(avail_cond);
		sthread_mutex_unlock(mutex);
		sthread_yield();
	}

	printf("Sent %d\n", sent);

	/* Now give the other threads 100 tries to get
	 * them all across. We assume that's enough
	 * for the sake of not running this test forever. */
	checks = 10000;  //arbitrary??
	while (checks > 0) {
		sthread_mutex_lock(mutex);
		if (transfered != max_transfer)
			checks--;
		else {
			/* broadcast to let the consumers know we've
			 * finished, so they can exit
			 * (othrewise, they may still be holding the lock
			 * when we try to free it below) */
			sthread_cond_broadcast(avail_cond);
			checks = -1;
		}
		sthread_mutex_unlock(mutex);
		sthread_yield();
	}

	if (checks == -1) {
		/* Wait for child threads to finish, otherwise we could try to
		 * free the mutex before they've unlocked it! */
		printf("joining on children\n");
		for (i = 0; i < num_threads; i++) {
			sthread_join(child[i]);
			printf("joined with child %d\n", i);
		}
		printf("sthread_cond passed\n");
	} else {
		printf("*** sthread_cond failed\n");
		/* If we failed, don't bother joining on threads. */
	}

	sthread_mutex_free(mutex);
	sthread_cond_free(avail_cond);
	return 0;
}