int main()
{
    int i;
    mythread_t thread[NR_THREADS];
    mythread_setconcurrency(3);

    mythread_mutex_init(&mutex, NULL);
    mythread_cond_init(&okToProceed, NULL);

    for (i = 0; i < NR_THREADS; i++)
	mythread_create(&thread[i], NULL, runner, NULL);
    sleep(2);
    printf("Main: sending broadcast signal to all threads\n");
    fflush(stdout);
    mythread_cond_broadcast(&okToProceed);
    printf("Main: joining all threads\n");
    fflush(stdout);
    for (i = 0; i < NR_THREADS; i++)
	mythread_join(thread[i], NULL);
    printf("Main: about to exit\n");
    fflush(stdout);

    mythread_exit(NULL);

    mythread_cond_destroy(&okToProceed);
    mythread_mutex_destroy(&mutex);
    return 1;
}
Ejemplo n.º 2
0
int main (void)
{
    int test;
    // initialize mutex, condition variable and barrier.   
    mythread_mutex_init (&mut, NULL);
    mythread_cond_init (&con, NULL);
    // 4 threads will be calling wait. 
    mythread_barrier_init (&barr, NULL, 4);

    mythread_setconcurrency (8);

    // create first 3 threads and assign them functions. 
    mythread_create (&t1, NULL, foo1, NULL);
    mythread_create (&t2, NULL, foo2, NULL);
    mythread_create (&t3, NULL, foo3, NULL);
    
    // stall main until all threads have finished. 
   if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD)) 
    
      PASS--;

   // after this stage, mutex and condition variables are not required, destroy them. 
   if ( mythread_cond_destroy (&con) != 0)
     PASS --;

   if ( mythread_mutex_destroy (&mut) != 0)
     PASS --;
  
    // create next 3 threads and assign functions. This is to test reusability of barriers. 
    mythread_create (&t4, NULL, foo3, NULL);
    mythread_create (&t5, NULL, foo4, NULL);
    mythread_create (&t6, NULL, foo5, NULL);
    
    // let main also wait on the barrier
   if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD)) 
     PASS--;
 
    // at this point, we are sure that all the threads have finished running. Now check value of PASS for any errors. 
    // If PASS is 0, means all function executed successfully. If PASS is less than 0, atleast one function returned error. 
    // print PASS or FAIL accordingly. 

    if (PASS == 0)
      printf ("PASS\n");
    else
      printf ("FAIL\n");

    //destroy the barrier and exit. 
    mythread_barrier_destroy (&barr);      
    mythread_exit(NULL);
    return 0;
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{	
	int i;
	char buffer[1024];
	futex_init(&printFutex, 1);
	mythread_t mythread1[NTHREADS];
	mythread_t signalingThread[NTHREADS/2];
	mythread_setconcurrency(4);
	mythread_mutex_init(&mutex,NULL);
	mythread_cond_init(&condition,NULL);
	mythread_barrier_init(&barrier,NULL,NTHREADS+1);
	
	/* Create Threads */
	for(i=0;i<NTHREADS;i++){
		sprintf(buffer, "Created thread : %d\n", i+1);
		printToConsole(buffer);
		mythread_create(&mythread1[i],NULL,&thread_func, NULL);
	}
	
	/*Signal threads waiting on cond var*/
	while(count<NTHREADS){
		/* Special case for testing broadcast*/
		if(count == NTHREADS/2){
			mythread_cond_broadcast(&condition);
		}else{
			mythread_cond_signal(&condition);
		}
	}
	
	/* Waiting on barrier. Last thread, or this main thread will unblock the barrier depending on the execution sequence */
	mythread_barrier_wait(&barrier);
	sprintf(buffer, "Out of barrier, main thread exiting..\n");
	printToConsole(buffer);
	/* Destroy mutex, barrier and cond*/
	mythread_cond_destroy(&condition);
        mythread_mutex_destroy(&mutex);
	mythread_barrier_destroy(&barrier);
	sprintf(buffer, "Finished Execution\n");
	printToConsole(buffer);
}
Ejemplo n.º 4
0
int main()
{
	int i;
	mythread_t thread[NTHREADS];
	mythread_setconcurrency(NTHREADS+1);
	mythread_mutex_init(&mut, NULL);
	mythread_cond_init(&cond, NULL);

	for(i = 0; i < NTHREADS; i++)
		mythread_create(&thread[i], NULL, fun, NULL);
	for(i = 0; i < INT_MAX/10; i++);

	printf("Broadcast\n"); fflush(stdout);
	mythread_cond_broadcast(&cond);
	printf("Now join\n"); fflush(stdout);
	for(i = 0; i < NTHREADS; i++)
		mythread_join(thread[i], NULL);	
	printf("main exit\n"); fflush(stdout);

	mythread_exit(NULL);
	mythread_cond_destroy(&cond);
	mythread_mutex_destroy(&mut);
	return 1;
}
Ejemplo n.º 5
0
int main() {

	mythread_setconcurrency(4);
	mythread_mutex_init(&lock,NULL);
	mythread_mutex_init(&lock1,NULL);

	mythread_cond_init(&cond,NULL);
	mythread_cond_init(&cond1,NULL);
	glval=0;
	glval1=0;
	mythread_t tid1,tid2,tid3,tid4,tid5;
	mythread_queue_t head;
	

	tid1 = malloc(sizeof(struct mythread));
	tid2 = malloc(sizeof(struct mythread));
	tid3 = malloc(sizeof(struct mythread));
	tid4 = malloc(sizeof(struct mythread));
	tid5 = malloc(sizeof(struct mythread));

/**************** Test cases for testing all functions except destroy functions ********************/
	mythread_create(&tid3,NULL,thread3,NULL); //Thread3 going to call condition wait and will be woken up(condition signal)  
	mythread_create(&tid4,NULL,thread4,NULL); //Thread4 going to call condition wait and will be woken up(condition broadcast signal)  
	mythread_create(&tid5,NULL,thread5,NULL); //Thread5 going to call condition wait and will be woken up(condition broadcast signal)  

	printOut("Sleeping for 1 second so that threads calling condition waits starts before their condition variables are signaled\n");
	sleep(1); // Sleep for 1 second so that tid3,tid4,tid5 waits on condition wait before condition signal/broadcast is sent
	mythread_create(&tid1,NULL,thread1,NULL);
	mythread_create(&tid2,NULL,thread2,NULL);
	mythread_join(tid1,NULL);
	mythread_join(tid2,NULL);

	printOut("Value of global variable incremented 200 times in two threads : ");
	printOut(itoa(glval,10));
	printOut("\n");

/*************************************************************************************************/
	mythread_join(tid3,NULL);
	mythread_join(tid4,NULL);
	mythread_join(tid5,NULL);


	printOut("=================================================================\n");
	printOut("Repeat the entire test after destroying(mythread_mutex_destroy,mythread_cond_destroy) and reinitializing 2 locks and 2 condition variables\n");
	printOut("=================================================================\n");

/********* Destroying all mmutexes and locks and reinitialising again ****************************/
	mythread_mutex_destroy(&lock);
	mythread_mutex_destroy(&lock1);
	mythread_cond_destroy(&cond);
	mythread_cond_destroy(&cond1);

        mythread_mutex_init(&lock,NULL);
        mythread_mutex_init(&lock1,NULL);

        mythread_cond_init(&cond,NULL);
        mythread_cond_init(&cond1,NULL);
/**************************************************************************************************/

/**************** Test cases for testing all functions except destroy functions ********************/
	glval=0;
        mythread_create(&tid3,NULL,thread3,NULL); //Thread3 going to call condition wait and will be woken up(condition signal)  
        mythread_create(&tid4,NULL,thread4,NULL); //Thread4 going to call condition wait and will be woken up(condition broadcast signal)  
        mythread_create(&tid5,NULL,thread5,NULL); //Thread5 going to call condition wait and will be woken up(condition broadcast signal)  

	printOut("Sleeping for 1 second so that threads calling 'condition wait' starts before their condition variables are signaled\n");
	sleep(1); // Sleep for 1 second so that tid3,tid4,tid5 waits on condition wait before condition signal/broadcast is sent
        mythread_create(&tid1,NULL,thread1,NULL);
        mythread_create(&tid2,NULL,thread2,NULL);
        mythread_join(tid1,NULL);
        mythread_join(tid2,NULL);

        printOut("Value of global variable incremented in the repeated test 200 times in two threads : ");
        printOut(itoa(glval,10));
        printOut("\n");

/**************************************************************************************************/


	mythread_exit(NULL);			

}