int mypthread_mutex_lock(mypthread_mutex_t *mutex)
{
    sem_wait(&sem_mutex);
    if (mutex->lock == 0)
    {
        /* If I am here, that means i can acquire the mutex */
        mutex->lock == 1;
        sem_post(&sem_mutex);
    }   
    else
    {
        /* If I am here, that means I need to block on the mutex */
        sem_wait(&sem_main);
        mypthread_t *current_thread = search_thread(mythread_q, cur_thread_id);
        current_thread->pthread->thread_state = PS_BLOCKED;
        mypthread_t *next_thread = next_active_thread(mythread_q);
        cur_thread_id = next_thread->pthread->thread_id;
        linear_enqueue(mutex->mutex_block_q, current_thread->pthread);
        makecontext(current_thread->pthread->ucontext,(void (*)()) mypthread_mutex_lock,1, mutex);
        sem_post(&sem_main);
        sem_post(&sem_mutex); 
        swapcontext(current_thread->pthread->ucontext, next_thread->pthread->ucontext ); 
    } 

    return 0;
}
int mypthread_cond_wait(mypthread_cond_t *cond,
                      mypthread_mutex_t *mutex)
{
     sem_wait(&sem_cond);
     mypthread_mutex_unlock(mutex);
     sem_wait(&sem_main);
     mypthread_t *current_thread = search_thread(mythread_q, cur_thread_id);
     current_thread->pthread->thread_state = PS_BLOCKED;
     mypthread_t *next_thread = next_active_thread(mythread_q);
     cur_thread_id = next_thread->pthread->thread_id;
     linear_enqueue(cond->cond_block_q, current_thread->pthread);   
     sem_post(&sem_main);
     sem_post(&sem_mutex);
     swapcontext(current_thread->pthread->ucontext, next_thread->pthread->ucontext );
     mypthread_mutex_lock(mutex);
     return 0; 
}
mypthread_t* next_active_thread(struct thread_queue *q)
{
    int iter = 0;
    mypthread_t* temp;
    mypthread_t *current_thread = search_thread(q, cur_thread_id);
    temp = current_thread->next;
    while ( temp!= NULL && iter <= num_of_nodes)
    {
       if (temp->pthread->thread_state == PS_ACTIVE)
       {
           return temp;
       }
       else
       {
           temp = temp->next;
       }
       iter++;
    }
    return NULL;
}
Exemple #4
0
int main(int argc, char *argv[]){
	printf("generating the array...\n");
	/* The following line is used in order to have a sort of random array
	If we comment this line, */
	srand((unsigned)time(0));
	
	// the array is allocated and then filled with random floats
	float *p;
	p = malloc(nb*sizeof(float));
	int i;

	// Filling the array
	for(i=0; i < nb; i++){
		p[i] = ((float)rand()/(float)RAND_MAX)+(rand()%100);
	}
	
	// The value to look for
	float val = 9.589720;
	
	// The time
	double t0,t1, t2, t3 ;
	
	printf("Searching with a multithread method...\n");
	
	t0=now(); i = search_thread(p, nb, val); t1=now(); 
	if(i==0)
		printf("Not found with multithread method\n");
	printf("Multi-Thread T = %6.4f\n",t1-t0);
	
	printf("Searching with a monothread method...\n");
	
	finished = 0;
	t2 = now();	 i = cyclic_find(p, 1, nb-1, val, 0); t3 = now();
	if(i==0)
		printf("Not found with mono-thread method\n");
	printf("Mono-Thread T = %6.4f\n",t3-t2);
	
	//Free the memory
	free(p);
}
int mythread_scheduler(void *args)
{
   kargs *tempargs;
   tempargs = (kargs *)args;
   int kmode;
   mypthread_t thread;
   kmode = tempargs->kmode;
   thread = tempargs->thread; 
   
   
        if (kmode == 1)
        {
           mypthread_t* cur_thread = search_thread(mythread_q, cur_thread_id);

           cur_thread->pthread->thread_state = PS_DEAD;
           free(cur_thread->pthread->ucontext);

           if (cur_thread->pthread->joined_from_th != 0)
           {
               mypthread_t *join_thread = search_thread(mythread_q,cur_thread->pthread->joined_from_th);
               join_thread->pthread->thread_state = PS_ACTIVE;

           }   
           mypthread_t* next_thread = return_head(mythread_q);
           cur_thread_id = next_thread->pthread->thread_id;
           //sem_post(&sem_main);
           setcontext(next_thread->pthread->ucontext);
       }
       else if(kmode == 2)
       {
            int target_thread_id = thread.pthread->thread_id;
            mypthread_t *current_thread = search_thread(mythread_q,cur_thread_id);
            mypthread_t *target_thread = search_thread(mythread_q,target_thread_id);
            if (target_thread->pthread->thread_state != PS_ACTIVE)
            {
                  //sem_post(&sem_main);
                   return;
            }
            else
            {
                current_thread->pthread->thread_state = PS_BLOCKED;
                target_thread->pthread->joined_from_th = cur_thread_id;
                cur_thread_id = target_thread_id;
                //sem_post(&sem_main);
                swapcontext(current_thread->pthread->ucontext, target_thread->pthread->ucontext);
            }
    

       } 
       else if(kmode == 3)
       {
            mypthread_t *current_thread = search_thread(mythread_q, cur_thread_id);
            mypthread_t *next_thread = next_active_thread(mythread_q);
            //printf("Current thread id = %d , next thread id = %d\n", cur_thread_id, next_thread->pthread->thread_id);
            if (cur_thread_id == next_thread->pthread->thread_id)
            {
                return;
            }
            //printf("Thread %d Yielding\n",cur_thread_id);
            cur_thread_id = next_thread->pthread->thread_id;
           //sem_post(&sem_main);
            swapcontext(current_thread->pthread->ucontext, next_thread->pthread->ucontext);
       }
}