Changeset 264 for hvcontrol/src


Ignore:
Timestamp:
07/27/10 07:27:04 (14 years ago)
Author:
ogrimm
Message:
Removed mutex locking in hvcontrol, use lockf() in Communicate()
Location:
hvcontrol/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • hvcontrol/src/HV.cc

    r230 r264  
    9696
    9797  unsigned char rbuf;
    98   int ret;
     98  int N, Ret = 0;
    9999  fd_set SelectDescriptor;
    100  
     100  struct timeval WaitTime = {(long) fTimeOut, (long) ((fTimeOut-(long) fTimeOut)*1e6)};
     101 
     102  // === Lock file descriptor ===
     103  if (lockf(fDescriptor, F_LOCK, 0) == -1) {
     104        m->Message(m->ERROR, "Failed to lock file descriptor (%s)", strerror(errno));
     105        return 0;
     106  }
     107
    101108  // === Write data ===
    102   if ((ret=write(fDescriptor, wbuf, Bytes)) < Bytes) {
    103     if (ret == -1) m->Message(m->ERROR, "Could not write data to HV board (%s)", strerror(errno));
    104     else m->Message(m->ERROR, "Could write only %d of %d bytes to HV board", ret, Bytes);
     109  if ((N = write(fDescriptor, wbuf, Bytes)) < Bytes) {
     110    if (N == -1) m->Message(m->ERROR, "Could not write data to HV board (%s)", strerror(errno));
     111    else m->Message(m->ERROR, "Could write only %d of %d bytes to HV board", N, Bytes);
    105112    ErrorCount++;
    106     return 0;
     113        goto ExitCommunicate;
    107114  }
    108115
    109116  // === Try to read until time-out ===
    110117  FD_ZERO(&SelectDescriptor);   FD_SET(fDescriptor, &SelectDescriptor);
    111   struct timeval WaitTime = {(long) fTimeOut, (long) ((fTimeOut-(long) fTimeOut)*1e6)};
    112118  if (select(fDescriptor+1, &SelectDescriptor, NULL, NULL, &WaitTime)==-1) {
    113119    m->Message(m->ERROR, "Error with select() (%s)", strerror(errno));
    114     return 0;
     120        goto ExitCommunicate;
    115121  }
    116122  // Time-out expired?
    117   if (!FD_ISSET(fDescriptor, &SelectDescriptor)) return -1;
     123  if (!FD_ISSET(fDescriptor, &SelectDescriptor)) {
     124        Ret = -1;
     125        goto ExitCommunicate;
     126  }
    118127
    119128  // Read error?   
    120   if ((ret = read(fDescriptor, &rbuf, 1)) == -1) {
     129  if (read(fDescriptor, &rbuf, 1) == -1) {
    121130    m->Message(m->ERROR, "Read error (%s)", strerror(errno));
    122131    ErrorCount++;
    123     return 0;
     132        goto ExitCommunicate;
    124133  }
    125134
     
    132141  for (int i=0; i<NUM_CHAINS; i++) Overcurrent[i] = (rbuf & (0X08 << i));
    133142  ResetButton = (bool) (rbuf & 0X80);
    134 
    135   return 1;
    136 }
    137 
    138 
    139 /* Reset HV board - uses TRead() and has same return values */
     143  Ret =  1;
     144
     145  // === UnLock file descriptor ===
     146  ExitCommunicate:
     147  if (lockf(fDescriptor, F_LOCK, 0) == -1) {
     148        m->Message(m->ERROR, "Failed to lock file descriptor (%s)", strerror(errno));
     149        return 0;
     150  }
     151 
     152  return Ret;
     153}
     154
     155
     156// Reset HV board
    140157int HVBoard::Reset() {
    141158 
     
    151168
    152169
    153 /* Read status register - uses TRead() and has same return values */
     170// Read status register
    154171int HVBoard::GetStatus() {
    155172
     
    160177
    161178
    162 /* Set high voltage - uses TRead() and has same return values */
     179// Set high voltage
    163180int HVBoard::SetHV(int chain, unsigned int channel, int hv) {
    164181
  • hvcontrol/src/ProcessIO.cc

    r230 r264  
    3030        {"rate", &ProcessIO::cmd_rate, 1, "<rate>", "Set status refresh rate in Hz"},
    3131        {"timeout", &ProcessIO::cmd_timeout, 1, "<time>", "Set timeout to return from read in seconds"},
    32         {"reset", &ProcessIO::cmd_reset, 0, "", "Reset active bias board"},
     32        {"reset", &ProcessIO::cmd_reset, 0, "", "Reset active bias boards"},
    3333        {"start", &ProcessIO::cmd_start, 0, "", "Start bias status monitor"},
    3434        {"stop", &ProcessIO::cmd_stop, 0, "", "Stop bias status monitor"},
     
    5353  FirstBoard  = 0;
    5454  LastBoard   = -1;
    55 
    56   // Initialize mutex for thread synchronisation
    57   if (pthread_mutex_init(&Mutex, NULL) != 0) {
    58     Message(FATAL, "pthread_mutex_init() failed");
    59   }
    6055
    6156  // DIM console service used in PrintMessage()
     
    109104  delete calib;
    110105  delete ConsoleOut;   
    111   free(ConsoleText);
    112  
    113   // Destroy mutex
    114   if (pthread_mutex_destroy(&Mutex) != 0) Message(ERROR, "pthread_mutex_destroy() failed");
     106  free(ConsoleText); 
    115107}
    116108
     
    370362void ProcessIO::cmd_reset() {
    371363
    372   for (int i=FirstBoard; i<=LastBoard; i++) ResetBoard(i);
     364  for (int i=FirstBoard; i<=LastBoard; i++) {
     365        if (fHVBoard[i]->Reset() == 1) {
     366          PrintMessage("Reset of board %d\n", fHVBoard[i]->GetBoardNumber());
     367          PrintBoardStatus(i);
     368        }
     369        else PrintMessage("Error: Could not reset board %d\n",fHVBoard[i]->GetBoardNumber());
     370  }
    373371}
    374372
     
    527525
    528526  static bool Warned = false;
    529   int Ret;
    530 
    531   // Lock because command execution runs in different thread
    532   if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
    533         Message(FATAL, "pthread_mutex_lock() failed in commandHandler() (%s)", strerror(Ret));
    534   }
    535527
    536528  for (int i=0; i<NumHVBoards; i++) {
     
    549541    if (fHVBoard[i]->ResetButton) {
    550542      Message(INFO, "Manual reset of board %d", fHVBoard[i]->GetBoardNumber());
    551       ResetBoard(i);
     543          fHVBoard[i]->Reset();
    552544    }
    553545   
     
    558550    for (int j=0; j<NUM_CHAINS; j++) {
    559551      if (fHVBoard[i]->Overcurrent[j]) {
    560                 Message(WARN, "Warning: Overcurrent in chain %d of board %d",j,fHVBoard[i]->GetBoardNumber());
    561                 ResetBoard(i);
     552                Message(WARN, "Warning: Overcurrent in chain %d of board %d, resetting board",j,fHVBoard[i]->GetBoardNumber());
     553                fHVBoard[i]->Reset();
    562554      }
    563555    }
    564   }
    565  
    566   // Unlock
    567   if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
    568         Message(FATAL, "pthread_mutex_lock() failed in commandHandler() (%s)", strerror(Ret));
    569   }
    570 
    571 }
    572 
    573 
    574 // Send reset to board and clear voltage arrays
    575 void ProcessIO::ResetBoard(int i) {
    576    
    577   if (fHVBoard[i]->Reset() == 1) {
    578     PrintMessage("Reset of board %d\n", fHVBoard[i]->GetBoardNumber());
    579     PrintBoardStatus(i);
    580   }
    581   else PrintMessage("Error: Could not reset board %d\n",fHVBoard[i]->GetBoardNumber());
    582 }
     556  } 
     557}
     558
    583559
    584560
     
    597573
    598574
    599 // Command handling (mutex needed because of monitor thread)
     575// Command handling
    600576void ProcessIO::commandHandler() {
    601577
    602   int Ret;
    603 
    604   if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
    605         Message(FATAL, "pthread_mutex_lock() failed in commandHandler() (%s)", strerror(Ret));
    606   }
    607 
    608578  if ((getCommand()==Command) && (*(Command->getString()+Command->getSize()-1)=='\0')) {
    609         SendToLog("Command '%s' from %s (ID %d)", Command->getString(), getClientName(), getClientId());
    610579        CommandControl(Command->getString());
    611   }
    612 
    613   if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
    614         Message(FATAL, "pthread_mutex_unlock() failed in commandHandler() (%s)", strerror(Ret));
    615580  }
    616581}
  • hvcontrol/src/ProcessIO.h

    r230 r264  
    5454        unsigned int fHVMaxDiff;
    5555
    56         // Status variables 
    5756        pthread_t HVMonitor;       // exit function sends signal to these threads
    58         pthread_mutex_t Mutex;
    59 
    6057        int NumHVBoards;
    6158        int FirstBoard;
     
    7168        bool RampVoltage(unsigned int, int, int, int);
    7269        void Monitor();
    73         void ResetBoard(int);
    7470        void PrintBoardStatus(int);
    7571       
Note: See TracChangeset for help on using the changeset viewer.