Changeset 254 for drsdaq/DAQReadout.cc


Ignore:
Timestamp:
07/20/10 13:20:47 (14 years ago)
Author:
ogrimm
Message:
Replaced mutex by Lock()/Unlock() from Evidence class, added feedback state service
File:
1 edited

Legend:

Unmodified
Added
Removed
  • drsdaq/DAQReadout.cc

    r230 r254  
    5757
    5858// Global pointer for thread entry routines
    59 class DAQReadout *This;
     59class DAQReadout *ThisClass;
    6060
    6161// -----------------------------------------------
     
    6767
    6868  // Initialization
    69   This = this;
    70   MainThread = getpid();
     69  ThisClass = this;
     70  MainThread = pthread_self();
    7171  ConsoleText = NULL;
    7272  time(&StartTime);
     
    7474  // DIM console service used in PrintMessage()
    7575  ConsoleOut = new DimService(SERVER_NAME"/ConsoleOut", (char *) "");
    76 
    77   // Initialize mutex for thread synchronisation
    78   if (pthread_mutex_init(&Mutex, NULL) != 0) {
    79     Message(FATAL, "pthread_mutex_init() failed");
    80   }
    8176
    8277  // Initialize status structure
     
    169164  delete ConsoleOut;
    170165  free(ConsoleText);
    171  
    172   if (pthread_mutex_destroy(&Mutex) != 0) Message(ERROR, "pthread_mutex_destroy() failed");
    173166}
    174167
     
    202195  else if(CommandList[Count].NeedNotBusy && NumBoards==0) PrintMessage("No boards available\n");
    203196  else {
    204         int Ret;
    205 
    206         // Lock (Execute() runs in thread spawned by commandHandler())
    207         if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
    208           Message(FATAL, "pthread_mutex_lock() failed (%s)", strerror(Ret));
    209         }
    210         // Run command
    211197        CmdNumber = Count;
    212198        (this->*CommandList[CmdNumber].CommandPointer)();
    213         // Unlock
    214         if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
    215           Message(FATAL, "pthread_mutex_unlock() failed (%s)", strerror(Ret));
    216         }
    217199  }
    218200}
     
    238220// Print DAQ configuration
    239221void DAQReadout::cmd_config() {
    240   PrintConfig(MsgToConsole);
     222  PrintConfig();
    241223}
    242224
     
    411393  }
    412394
    413   PrintMessage(MsgToConsole, "==START== %d %.2f %.2f ",kNumberOfBins+2,DRSFreq[Board],GetBoard(Board)->GetPrecision());
     395  PrintMessage("==START== %d %.2f %.2f ",kNumberOfBins+2,DRSFreq[Board],GetBoard(Board)->GetPrecision());
    414396  for (int k=0; k<kNumberOfBins; k++) {
    415     PrintMessage(MsgToConsole, "%.1f ", (float) WaveForm[Board][Chip][Channel][k]);
    416   }
    417   PrintMessage(MsgToConsole, "==END==");
    418   PrintMessage(MsgToConsole, "\n");
    419   PrintMessage(MsgToConsole, "Trigger cell: %d\n", TriggerCell[Board][Chip]); 
     397    PrintMessage("%.1f ", (float) WaveForm[Board][Chip][Channel][k]);
     398  }
     399  PrintMessage("==END==");
     400  PrintMessage("\n");
     401  PrintMessage("Trigger cell: %d\n", TriggerCell[Board][Chip]); 
    420402}
    421403
     
    757739  for(unsigned int i=0; i<sizeof(CommandList)/sizeof(CL_Struct); i++) {
    758740    snprintf(Buffer, sizeof(Buffer), "%s %s", CommandList[i].Name, CommandList[i].Parameters);
    759     PrintMessage(MsgToConsole, "%-28s%s\n", Buffer, CommandList[i].Help);
     741    PrintMessage("%-28s%s\n", Buffer, CommandList[i].Help);
    760742  }     
    761   PrintMessage(MsgToConsole,".<command>                  Execute shell command\n\n"
     743  PrintMessage(".<command>                  Execute shell command\n\n"
    762744   "Items in <> are mandatory, in [] optional, | indicates mutual exclusive or.\n"
    763745   "Test data can also be written if no DRS boards are available.\n"
     
    770752
    771753  if (daq_state == active) PrintMessage("Issue 'stop' first to stop daq\n");
    772   else kill(MainThread, SIGTERM); 
     754  else pthread_kill(MainThread, SIGTERM); 
    773755}
    774756
     
    777759
    778760  if(Match(Param[1],"off")) HVFB->SetFBMode(FB_Off);
    779   if(Match(Param[1],"active")) HVFB->SetFBMode(FB_Active);
    780   if(Match(Param[1],"targets")) HVFB->SetFBMode(FB_Targets);
    781   HVFB->GetFBMode();
     761  else if(Match(Param[1],"active")) HVFB->SetFBMode(FB_Active);
     762  else if(Match(Param[1],"targets")) HVFB->SetFBMode(FB_Targets);
     763  else HVFB->GetFBMode();
    782764}
    783765
     
    823805void DAQReadout::cmd_fconfig() {
    824806
    825   HVFB->PrintConfig(MsgToConsole);
     807  HVFB->PrintConfig();
    826808}
    827809
     
    11751157
    11761158// Print configuration to target
    1177 void DAQReadout::PrintConfig(int Target) {
    1178   PrintMessage(Target, "RawDataPath: %s\n"
     1159void DAQReadout::PrintConfig() {
     1160  PrintMessage("RawDataPath: %s\n"
    11791161               "DefaultFrequency: %.2f\tFirstSample: %d\tSamples: %u\n"
    11801162               "MinDiskSpaceMB: %u\tMaxFileSizeMB: %d\n"
     
    11911173// Print progress (used in DRS class)
    11921174void DAQReadout::Progress(int Progress) {
    1193   PrintMessage(MsgToConsole, "\rProgress: %d%%              ", Progress);
     1175  PrintMessage("\rProgress: %d%%              ", Progress);
    11941176  fflush(stdout);
    11951177};
    1196  
    1197 // Print message to selected target
    1198 void DAQReadout::PrintMessage(int Target, const char *Format, ...) {
    1199 
    1200   va_list ArgumentPointer;
    1201   va_start(ArgumentPointer, Format);
    1202   DoPrintMessage(Format, ArgumentPointer, Target);
    1203   va_end(ArgumentPointer);
    1204 }
    12051178
    12061179// Print message to console only
    12071180void DAQReadout::PrintMessage(const char *Format, ...) {
     1181
     1182  static char Error[] = "vasprintf() failed in PrintMessage()";
     1183  char *Text;
     1184
     1185  // Evaluate arguments
    12081186  va_list ArgumentPointer;
    12091187  va_start(ArgumentPointer, Format);
    1210   DoPrintMessage(Format, ArgumentPointer, MsgToConsole);
     1188  if (vasprintf(&Text, Format, ArgumentPointer) == -1) Text = Error;
    12111189  va_end(ArgumentPointer);
    1212 }
    1213 
    1214 // Function doing the actual printing work
    1215 // Note: Be careful when overloading variadic functions. va_list is
    1216 // in gcc an int, which can be interpreted as char *...
    1217 void DAQReadout::DoPrintMessage(const char *Format, va_list ArgumentPointer, int Target) {
    1218 
    1219   static char Error[] = "vasprintf() failed in DoPrintMessage()";
    1220   char *Text;
    1221 
    1222   // Evaluate arguments   
    1223   if (vasprintf(&Text, Format, ArgumentPointer) == -1) Text = Error;
    12241190 
    12251191  // Print to console
    1226   if(Target & MsgToConsole) {
    1227     if(strlen(Text)>0 && Text[strlen(Text)-1]=='\n') printf("\r%s%s", Text, Prompt);  // New prompt
    1228     else printf("%s", Text);
    1229         fflush(stdout);
    1230   }
    1231  
    1232   // Send to DIM console service and to log if requested
    1233   ConsoleOut->updateService(Text);
    1234   if(Target & MsgToLog) SendToLog("%s %s", SERVER_NAME, Text);
     1192  if(strlen(Text)>0 && Text[strlen(Text)-1]=='\n') printf("\r%s%s", Text, Prompt); // New prompt
     1193  else printf("%s", Text);
     1194  fflush(stdout);
     1195
     1196  // Send to DIM text service
     1197  ConsoleOut->updateService(Text);
    12351198
    12361199  // Free old text
     
    12461209          *((char *) getCommand()->getData()+getCommand()->getSize()-1) != '\0' ||
    12471210          strlen(getCommand()->getString()) == 0) return;
    1248 
    1249   // Log command reception
    1250   SendToLog("Command '%s' from %s (ID %d)", getCommand()->getString(), getClientName(), getClientId());
    12511211
    12521212  // Copy command to new buffer (will be freed by global Execute() function)
     
    12631223    PrintMessage("pthread_create() failed in DRSReadout::commandHandler() (%s)\n", strerror(Code));
    12641224  }
    1265   else {
    1266     if ((Code = pthread_detach(Thread)) != 0) {
    1267           PrintMessage("pthread_detach() failed in DRSReadout::commandHandler() (%s)\n", strerror(Code));
    1268         }
     1225  else if ((Code = pthread_detach(Thread)) != 0) {
     1226        PrintMessage("pthread_detach() failed in DRSReadout::commandHandler() (%s)\n", strerror(Code));
    12691227  } 
    12701228}
     
    14591417void Execute(char *Command) {
    14601418
    1461  This->Execute(Command);
    1462  free(Command);
    1463 }
     1419  ThisClass->Lock();
     1420  ThisClass->Execute(Command);
     1421  free(Command);
     1422  ThisClass->Unlock();
     1423}
Note: See TracChangeset for help on using the changeset viewer.