Exemplo n.º 1
0
void *thread1() {

//	printOut(itoa(mythread_self(),10));
//	printOut("Thread1\n");
	int i=0;
	while(i<200) {
  		mythread_mutex_lock(&lock);
		glval++;
		//usleep(100);
                if(glval==100) {
                        printOut("Thread1 sleeping for 10000 microseconds to hit blocking mutex condition in thread2\n");
			mythread_cond_signal(&cond);
			mythread_cond_broadcast(&cond1);

                        usleep(10000);

                }

		mythread_mutex_unlock(&lock);
		printOut("Using IO in thread1 \n");
		i++;
	}

	
}
/*
 * This function is used to block on a conditional variable referenced by cond. The attribute referenced by mutex should already be blocked
 * Pre:A conditional variable , a locked mutex
 * Post:The calling thread will be blocked. Upon return mutex has been locked and owned by calling thread
 * Returns:  1 on failure
 * 	     0 on success
 */
int mythread_cond_wait(mythread_cond_t * cond, mythread_mutex_t * mutex)
{
    mythread_enter_kernel();
    mythread_block_phase1(&ccb_table[*cond]->q, 0);

    // Inorder to ensure there will be no signal in between these operations
    // and therefore prevent signal loss futex has been used.
    if (futex_down(&ccb_table[*cond]->mutex)) {
	perror("futex_down failed\n");
	return 1;
    }

    // release lock after phase 1
    if (mythread_mutex_unlock(mutex)) {
	perror("Failed to release lock\n");
	return 1;
    }
    // in order to ensure signal is not received when the thread is about to suspend we need to perform futex up 
    // and block phase 2 atomically 
    mythread_enter_kernel();
    if (futex_up(&ccb_table[*cond]->mutex)) {
	perror("Failed to futex_up\n");
	return 1;
    }
    mythread_block_phase2();

    // Acquire mutex lock before returning from conditional wait
    if (mythread_mutex_lock(mutex)) {
	perror("Failed to acquire lock");
	return 1;
    }
    return 0;
}
Exemplo n.º 3
0
void *thread5() {
        printOut("Thread5 Waiting on Condition Wait .Will be released by broadcast\n");
        mythread_mutex_lock(&lock1);

        mythread_cond_wait(&cond1,&lock1);  // in thread1/thread2 broadcast signal will be done
        printOut("Thread5 woken up by broadcast signal from Condition wait\n");
        mythread_mutex_unlock(&lock1);
}
Exemplo n.º 4
0
void *thread3() {

        printOut("Thread3 Waiting on Condition Wait\n");
	
	mythread_mutex_lock(&lock);
        mythread_cond_wait(&cond,&lock);

	printOut("Thread3 woken up from Condition wait\n");
	mythread_mutex_unlock(&lock);

}
Exemplo n.º 5
0
void* thread_func(void *args) {
	int t = 0;
	char buf[10];

	mythread_t self = mythread_self();

	sprintf(buf, "%ld", (long) args);
	mythread_mutex_lock(&mutex);
	strcat(actualOutput, buf);
	mythread_mutex_unlock(&mutex);
}
Exemplo n.º 6
0
void *fun(void *arg)
{
	int i;

	mythread_mutex_lock(&mut);
	printf("Will now wait\n"); fflush(stdout);
	mythread_cond_wait(&cond, &mut);
	for(i = 0; i < INT_MAX/10; i++);
	printf("Exiting \n"); fflush(stdout);
	mythread_mutex_unlock(&mut);
	mythread_exit(NULL);
	return NULL;
}
void *runner(void *arg)
{
    int i;

    mythread_mutex_lock(&mutex);
    printf("Thread:Waiting for main thread to broadcast\n");
    fflush(stdout);
    mythread_cond_wait(&okToProceed, &mutex);
    sleep(0.2);
    printf("Thread:Out of conditonal wait and about to exit\n");
    fflush(stdout);
    mythread_mutex_unlock(&mutex);
    mythread_exit(NULL);
    return NULL;
}
Exemplo n.º 8
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;
}
Exemplo n.º 9
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);	
}