Ejemplo n.º 1
0
void put_forks(int id) {
   /* Enter Critical Region */ 
   if (pthread_mutex_lock(&putForkLock) != 0) {
      fprintf(stderr, "P thread error entering put_forks \n");
      exit(-1);
   }

   /* Philosopher wants to think */ 
   state[id] = CHANGING;

   printStatusLine();

   /* Unlock the forks for other philosophers */ 
   printForks[LEFT_FORK] = FORK_DOWN;
   if (pthread_mutex_unlock(&(forks[LEFT_FORK])) != 0) {
      fprintf(stderr, "P thread error unlocking left fork \n");
      exit(-1);
   }
 
   printStatusLine();

   printForks[RIGHT_FORK] = FORK_DOWN;
   if (pthread_mutex_unlock(&(forks[RIGHT_FORK])) != 0) {
      fprintf(stderr, "P thread error unlocking right fork \n");
      exit(-1);
   }

   printStatusLine();
   
   /* Exit critical region */ 
   if (pthread_mutex_unlock(&putForkLock) != 0) {
      fprintf(stderr, "P thread error leaving put_forks \n");
      exit(-1);
   }
}
Ejemplo n.º 2
0
void take_forks(int id) {
   /* Enter critical section */ 
   if (pthread_mutex_lock(&takeForkLock) != 0) {
      fprintf(stderr, "P thread error entering take_forks \n");
      exit(-1);
   }

   /* Philosopher is hungry */  
   state[id] = CHANGING;

   printStatusLine();
   
   if (id % 2 == 0) {
      /* Even philosopher, take right fork first */ 
      takeRightFork(id);
      takeLeftFork(id);
   }
   else {
      /* Odd philosopher, take left fork first */ 
      takeLeftFork(id);
      takeRightFork(id);
   }      

   /* Leave Critical section */ 
   if (pthread_mutex_unlock(&takeForkLock) != 0) {
      fprintf(stderr, "P thread error leaving take_fork \n");
      exit(-1);
   }
}
Ejemplo n.º 3
0
void think(int id) {
   state[id] = THINKING;

   printStatusLine();

   /* Sleep for a random amount of time */ 
   dawdle();
}
Ejemplo n.º 4
0
void eat(int id) {
   state[id] = EATING;

   printStatusLine();

   /* Sleep for a random amount of time */ 
   dawdle();
}
Ejemplo n.º 5
0
void takeRightFork(int id) {
   /* Try to lock the right fork, if not - block */ 
   if (pthread_mutex_lock(&(forks[RIGHT_FORK])) != 0) {
      fprintf(stderr, "P thread error locking right fork \n");
      exit(-1);
   }

   printForks[RIGHT_FORK] = id;

   printStatusLine();
}
Ejemplo n.º 6
0
 void redraw(const uavcan::TimerEvent&)
 {
     std::cout << "\x1b\x5b\x48" << "\x1b\x5b\x32\x4a"
               << " NID | Status        | Uptime\n"
               << "-----+---------------+--------\n";
     for (unsigned i = 1; i <= uavcan::NodeID::Max; i++)
     {
         const auto s = getNodeStatus(i);
         if (s.known)
         {
             printStatusLine(i, s);
         }
     }
     std::cout << std::flush;
 }
Ejemplo n.º 7
0
//update string that will be drawn on status bar
void Editor::updateStatus(const std::string& append)
{
    std::stringstream status;
    std::lock_guard<std::recursive_mutex> lock(_mutex);
    switch(_mode)
    {
        case NORMAL:
            if(_cmd.empty())
                status << _statusTitle << "\t" << STATUS_VERSION;
            else
                status << _cmd;
            break;
        case INSERT:
            status << "Insert Mode";
            break;
        case EXIT:
            status << "Exiting";
            break;
    }
    status << "\tCOL: " << _x  << "\tLINE: " << _lowerbound + _y;
    status << " Y:" << _y << " SL: " << _screenLines << " LR" << _lowerbound;
    status << append;
    printStatusLine(status.str());
}