Пример #1
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;
}
Пример #2
0
void *barrierTest() {

    count++;
    mythread_barrier_wait(&barrier);
    if ( count == N ) {
          first_test++;
    }
    
    count1 ++;
    mythread_barrier_wait(&barrier);
    if ( count1 == N ) {
          second_test++;
    }

}
Пример #3
0
void *barrierTest(void* arg){
  mythread_enter_kernel();
  printf("Going to sleep...%ld\n",mythread_self());
  mythread_leave_kernel();
  //sleep(5);
  mythread_barrier_wait(&mybarrier);
  mythread_enter_kernel();
  printf("Moving towards new barrier... %ld\n",mythread_self());
  mythread_leave_kernel();

  mythread_barrier_wait(&mybarrier);
  mythread_enter_kernel();
  printf("%ld exiting... %ld\n",mythread_self());
  mythread_leave_kernel();
  mythread_exit(NULL);
}
Пример #4
0
void *foo4 (void *arg)
{
  int test;
  int val = 0;
 
  while (val < 9999)
    val++;

   if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD)) 
  
    PASS--;
 
  while (val > 0)
    val--;
  
  return NULL; 
}
Пример #5
0
// grab the lock. If count is less than 100, then wait for signal. Once signal is recieved, unlock mutex and wait on barrier. 
void *foo1 (void *arg)
{
    int test;
    mythread_mutex_lock (&mut);
    
    while (count < 100)             
    {
      mythread_cond_wait (&con, &mut);  
    }
        
   if ( mythread_mutex_unlock (&mut) != 0)
     PASS--;
        
   if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD)) 
    PASS--;

    return NULL;
}
Пример #6
0
/* Each thread will increment the count (global) protected by mutex */
void* thread_func(void *arg)
{
	char buffer[1024];
	/*Lock Mutex*/
	int temp = 0;
	mythread_mutex_lock(&mutex);
	while(temp<1000){
		temp++;
	}
	/*Wait to increment until main thread signals*/
	mythread_cond_wait(&condition,&mutex);
	count++;
	mythread_enter_kernel();
	sprintf(buffer, "Incremented Count to : %d\n", count);
	printToConsole(buffer);
	mythread_leave_kernel();
	mythread_mutex_unlock(&mutex);
	/* Threads will wait on the barrier. Main thread will also wait */
	mythread_barrier_wait(&barrier);
	mythread_exit(NULL);	
}
Пример #7
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);
}