Changeset 264 for hvcontrol/src/HV.cc


Ignore:
Timestamp:
07/27/10 07:27:04 (14 years ago)
Author:
ogrimm
Message:
Removed mutex locking in hvcontrol, use lockf() in Communicate()
File:
1 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
Note: See TracChangeset for help on using the changeset viewer.