Exemplo n.º 1
0
void
gostraight(void *p, unsigned long direction)
{
	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
 	switch (direction) {
		case 0:
			lock_acquire(sp_intr_zero);
			lock_acquire(sp_intr_three);
			
			inQuadrant(0);
			inQuadrant(3);
			lock_release(sp_intr_zero);
			
			leaveIntersection();
			lock_release(sp_intr_three);
			break;
		case 1:
			lock_acquire(sp_intr_zero);
			lock_acquire(sp_intr_one);
			
			inQuadrant(1);
			inQuadrant(0);
			lock_release(sp_intr_one);
			
			leaveIntersection();
			lock_release(sp_intr_zero);
			break;
		case 2:
			lock_acquire(sp_intr_one);
			lock_acquire(sp_intr_two);
			
			inQuadrant(2);
			inQuadrant(1);
			lock_release(sp_intr_two);
			
			leaveIntersection();
			lock_release(sp_intr_one);
			break;
		case 3:
			lock_acquire(sp_intr_two);
			lock_acquire(sp_intr_three);
			
			inQuadrant(3);
			inQuadrant(2);
			lock_release(sp_intr_three);
			
			leaveIntersection();
			lock_release(sp_intr_two);
			break;
		default:
			KASSERT(false);
	}
  
  	// 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  	// stoplight driver can return to the menu cleanly.
  	V(stoplightMenuSemaphore);
 	return;
}
Exemplo n.º 2
0
void
turnright(void *p, unsigned long direction)
{
	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
 // (void)direction;

    // @forkloop
	//
    // this won't have deadlock
    lock_acquire(quad_lock);
	while (quad_status[direction]) {
		cv_wait(quad_cv, quad_lock);
	}
	quad_status[direction] = 1;
	inQuadrant(direction);
    lock_release(quad_lock);

    lock_acquire(quad_lock);
    leaveIntersection();
    quad_status[direction]=0;
    //cv_signal(quad_cv, quad_lock);
    cv_broadcast(quad_cv, quad_lock);
    lock_release(quad_lock);

  // 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  // stoplight driver can return to the menu cleanly.
  V(stoplightMenuSemaphore);
  return;
}
Exemplo n.º 3
0
void
turnleft(void *p, unsigned long direction)
{
  struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;

  //Enter intersection
  P(inter_sem);
  int firstQuad = direction;
  int secondQuad = (direction + 3) % 4;
  int thirdQuad = (direction + 2) % 4;
  //Try to enter X
  lock_acquire(locks[firstQuad]);
  //Enter X
  inQuadrant(firstQuad);
  //Try to enter (X+3) % 4
  lock_acquire(locks[secondQuad]);
  //Leave X, enter (x+3) % 4
  inQuadrant(secondQuad);
  lock_release(locks[firstQuad]);
  //Leave (x+3) % 4, enter (x+2) % 4 
  lock_acquire(locks[thirdQuad]);
  inQuadrant(thirdQuad);
  lock_release(locks[secondQuad]);
  //Leave intersection
  leaveIntersection();
  lock_release(locks[thirdQuad]);
  V(inter_sem);
  
  // 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  // stoplight driver can return to the menu cleanly.
  V(stoplightMenuSemaphore);
  return;
}
Exemplo n.º 4
0
void
gostraight(void *p, unsigned long direction)
{
	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
  //(void)direction;
	int sec_quad=(direction+3)%4;
	lock_acquire(intersect_lock);
	// So while any of the two possible quadrants are already set, It means some 
	//thread is waiting...Hopefully
	while(cur_poss[direction]==1||cur_poss[sec_quad]==1)
		cv_wait(intersect_cv,intersect_lock);
	cur_poss[direction]=1;
	cur_poss[sec_quad]=1;
	inQuadrant(direction);
	lock_release(intersect_lock);
	lock_acquire(intersect_lock);
    cur_poss[direction]=0;
    //while(curr_p)
    //cv_broadcast(intersect_cv);
    inQuadrant(sec_quad);
    cv_broadcast(intersect_cv,intersect_lock);
    lock_release(intersect_lock);
    lock_acquire(intersect_lock);
    cur_poss[sec_quad]=0;
    leaveIntersection();
    cv_broadcast(intersect_cv,intersect_lock);
    lock_release(intersect_lock);
  // 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  // stoplight driver can return to the menu cleanly.
  V(stoplightMenuSemaphore);
  return;
}
void
turnright(void *p, unsigned long direction)
{
	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;

	//int j_id= ++j[direction];
	//while(j_id!=head[direction]);
//  while(!am_i_first(j[direction],curthread->t_name));
//kprintf("\nTurning right: %s, %ld\n",curthread->t_name,direction);
//insertEnd(j[direction],jl[direction],curthread->t_name);
  //Entering the first and final quadrant

	P(mutex);
	P(quadSem[direction]);
	V(mutex);

//kprintf("\nTurning right: %s acquired %ld\n",curthread->t_name,direction);
  inQuadrant(direction);

  //exiting
  leaveIntersection();
  //head[direction]++;
//  removeHead(j[direction],jl[direction]);
  V(quadSem[direction]);

  // 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  // stoplight driver can return to the menu cleanly.
  V(stoplightMenuSemaphore);
  return;
}
Exemplo n.º 6
0
void
turnleft(void *p, unsigned long direction)
{
	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
  //(void)direction;

    // @forkloop
	//
    int next, nnext;
    next = (direction+3)%4;
    nnext = (direction+2)%4;

    lock_acquire(quad_lock);
	while(quad_status[direction]||quad_status[next])
		cv_wait(quad_cv, quad_lock);

	//quad_status[direction]=quad_status[next]=1;
	//quad_status[direction]=1;
	inQuadrant(direction);
	quad_status[next] = 1;
	inQuadrant(next);
	cv_broadcast(quad_cv, quad_lock);
	lock_release(quad_lock);

	// third quad
	lock_acquire(quad_lock);
	while(quad_status[nnext])
		cv_wait(quad_cv, quad_lock);
	//quad_status[direction]=0;
	//quad_status[next] = 0;
	//inQuadrant(next);
	quad_status[next] = 0;
	quad_status[nnext] = 1;
    inQuadrant(nnext);
    cv_broadcast(quad_cv, quad_lock);
	//cv_signal(quad_cv, quad_lock);
	lock_release(quad_lock);

	// leave
	lock_acquire(quad_lock);
	leaveIntersection();
	quad_status[nnext] = 0;
    cv_broadcast(quad_cv, quad_lock);
	//cv_signal(quad_cv, quad_lock);
	lock_release(quad_lock);

  // 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  // stoplight driver can return to the menu cleanly.
  V(stoplightMenuSemaphore);
  return;
}
Exemplo n.º 7
0
void
gostraight(void *p, unsigned long direction)
{
	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
  (void)direction;

  lock_acquire(big_lock);
  lock_quadrants(SL_STRAIGHT,direction);
  lock_release(big_lock);
  move_to_quadrants(SL_STRAIGHT,direction);
  leaveIntersection();
  release_quadrants(SL_STRAIGHT,direction);
  // 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  // stoplight driver can return to the menu cleanly.
  V(stoplightMenuSemaphore);
  return;
}
Exemplo n.º 8
0
void
turnleft(void *p, unsigned long direction)
{
	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
  (void)direction;
  int sec_quad=(direction+3)%4;
  int third_quad=(direction+2)%4;
  lock_acquire(intersect_lock);
  while(cur_poss[direction]==1||cur_poss[sec_quad]==1||cur_poss[third_quad]==1)
	  cv_wait(intersect_cv,intersect_lock);
  cur_poss[direction]=1;
  cur_poss[sec_quad]=1;
  inQuadrant(direction);
  lock_release(intersect_lock);
  lock_acquire(intersect_lock);
  cur_poss[direction]=0;
  
  //while(cur_poss[third_quad]==1||cur_poss[sec_quad]==1)
  //cv_wait(intersect_cv,intersect_lock);
      cur_poss[third_quad]=1;
      //while(curr_p)
      //cv_broadcast(intersect_cv);
      inQuadrant(sec_quad);
      cv_broadcast(intersect_cv,intersect_lock);
      lock_release(intersect_lock);
        lock_acquire(intersect_lock);
            //currposs[direction]=0;
            cur_poss[sec_quad]=0;
            //while(curr_p)
            //cv_broadcast(intersect_cv);
            inQuadrant(third_quad);
            cv_broadcast(intersect_cv,intersect_lock);
                //cur_poss[third_quad]=0;
            lock_release(intersect_lock);
              lock_acquire(intersect_lock);
                leaveIntersection();
                cur_poss[third_quad]=0;
                cv_broadcast(intersect_cv,intersect_lock);
                lock_release(intersect_lock);
  // 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  // stoplight driver can return to the menu cleanly.
  V(stoplightMenuSemaphore);
  return;
}
Exemplo n.º 9
0
void
turnright(void *p, unsigned long direction)
{
	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
  (void)direction;
  // Easy coz the vehicle is in same lane!!
  lock_acquire(intersect_lock);
  while(cur_poss[direction]==1)
	  cv_wait(intersect_cv,intersect_lock);
  cur_poss[direction]=1;
  inQuadrant(direction);
  lock_release(intersect_lock);
    lock_acquire(intersect_lock);
  leaveIntersection();
  cur_poss[direction]=0;
  cv_broadcast(intersect_cv,intersect_lock);
  lock_release(intersect_lock);
  // 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  // stoplight driver can return to the menu cleanly.
  V(stoplightMenuSemaphore);
  return;
}
void
turnleft(void *p, unsigned long direction)
{
	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
//	int j_id= ++j[direction];
//	while(j_id!=head[direction]);
//kprintf("\nTurning left: %s, %ld\n",curthread->t_name,direction);
/*while(1)
{
lock_acquire(jlock);
if(quadSem[direction]->sem_count==1 && quadSem[(direction+3)%4]->sem_count==1 && quadSem[(direction+2)%4]->sem_count==1)
{
	P(quadSem[direction]);
	P(quadSem[(direction+3)%4]);
	P(quadSem[(direction+2)%4]);
	break;
}
lock_release(jlock);
}*/
//insertEnd(j[direction],jl[direction],curthread->t_name);
//int ex_flag=0;
//while(!am_i_first(j[direction],curthread->t_name));

while(1)
{
	P(mutex);
	//Entering the first quadrant
  P(quadSem[direction]);

//kprintf("\nTurning left: %s acquired %ld\n",curthread->t_name,direction);

  //Entering the second quadrant;
if(quadSem[(direction+3)%4]->sem_count>0)
{
  P(quadSem[(direction+3)%4]);
if(quadSem[(direction+2)%4]->sem_count>0)
{
  P(quadSem[(direction+2)%4]);
  	  V(mutex);
	break;
}
else
{
	V(quadSem[(direction+3)%4]);
	V(quadSem[direction]);
}
}
else
	V(quadSem[direction]);
V(mutex);
//kprintf("\nTurning left: %s acquired %ld\n",curthread->t_name,(direction+3)%4);

}
//kprintf("\nTurning left: %s acquired %ld\n",curthread->t_name,(direction+2)%4);

  inQuadrant(direction);
  
  inQuadrant((direction+3)%4);

  inQuadrant((direction+2)%4);

  //exiting
  leaveIntersection();
//  head[direction]++;
//  removeHead(j[direction],jl[direction]);
  V(quadSem[direction]);
  V(quadSem[(direction+3)%4]);
  V(quadSem[(direction+2)%4]);


  // 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  // stoplight driver can return to the menu cleanly.
  V(stoplightMenuSemaphore);
  return;
}
void
gostraight(void *p, unsigned long direction)
{
	struct semaphore * stoplightMenuSemaphore = (struct semaphore *)p;
  (void)direction;
//kprintf("\nGo Straight: %s, %ld\n",curthread->t_name,direction);
/*
while(1)
{
lock_acquire(jlock);
if(quadSem[direction]->sem_count==1 && quadSem[(direction+3)%4]->sem_count==1)
{
	P(quadSem[direction]);
	P(quadSem[(direction+3)%4]);
	break;
}
lock_release(jlock);
}*/
//int j_id= ++j[direction];
//while(j_id!=head[direction]);

while(1)
{
	P(mutex);
  P(quadSem[direction]);
  //ex_flag++;
  //if(ex_flag==2)
	  //break;
if(quadSem[(direction+3)%4]->sem_count!=0)
{
	P(quadSem[(direction+3)%4]);

	V(mutex);
	break;
}
else
{
	V(quadSem[direction]);
	V(mutex);
}

}

  inQuadrant(direction);
  

  //Entering the second quadrant

  inQuadrant((direction+3)%4);


  //exiting
  leaveIntersection();
  //head[direction]++;
//  removeHead(j[direction],jl[direction]);
  V(quadSem[direction]);
  V(quadSem[(direction+3)%4]);

  // 08 Feb 2012 : GWA : Please do not change this code. This is so that your
  // stoplight driver can return to the menu cleanly.
  V(stoplightMenuSemaphore);
  return;
}