Fixed busy error
[u/mrichter/AliRoot.git] / EVE / EveHLT / AliEveEventBuffer.cxx
index edef8e20791c8044cd2833864a6cdf0350e25db4..755c7651d8b7e53a9019f7e4f2a7eafbcebfd047 100644 (file)
@@ -62,23 +62,27 @@ AliEveEventBuffer::~AliEveEventBuffer() {
 
 }
 
+///___________________________________________________________________________
 void AliEveEventBuffer::CreateBufferThread() {
-  //  cout << "hereherehere"<<endl;
-  TThread * fThread = new TThread(AliEveEventBuffer::BufferThread, (void*) this);
-  fThread->Run();
-
+  if(GetBusy()) {
+    cout << "Buffer is busy, no thread created"<< endl;
+  } else {
+    if ( (CalculateDifference(fBIndex[kTop],fBIndex[kLast]) < fPreBuffer) ) {
+      SetBusy(kTRUE);
+      cout << "CreateBufferThread()"<<endl;
+      TThread * fThread = new TThread(AliEveEventBuffer::BufferThread, (void*) this);
+      fThread->Run();
+      cout << "Started BufferThread"<<endl;
+    } else { 
+      cout << "Buffer is full already"<<endl;
+    }
+  }
 }
-
 ///___________________________________________________________________________
 void * AliEveEventBuffer::BufferThread(void * buffer) {
   cout <<"BufferThread : " <<endl;
   if(buffer) {
-    if (!reinterpret_cast<AliEveEventBuffer*>(buffer)->GetBusy()) {
       reinterpret_cast<AliEveEventBuffer*>(buffer)->MonitorBuffer();
-    } else {
-      cout << "busy"<<endl;
-    }
-    
   } else {
     cout << "no buffer"<<endl;
   }
@@ -87,37 +91,19 @@ void * AliEveEventBuffer::BufferThread(void * buffer) {
 
 ///_____________________________________________________________________________
 void AliEveEventBuffer::MonitorBuffer() {
-  cout << "Monitorbuffer: " << endl;
-  if ( (CalculateDifference(fBIndex[kTop],fBIndex[kLast]) < fPreBuffer) ) {
-    if(GetBusy()) {
-      cout << "Already called FetchEvent()" << endl;
-      return;
-    } else {
-      fBusy = kTRUE;
-      FetchEvent();
-      fBusy = kFALSE;
-    }
-  } else {
-    //StopBufferMonitor();
-    
-    fBusy = kFALSE;
-  }
+  cout << "Monitorbuffer() ";
+  SetBusy(kTRUE);
+  FetchEvent();
+  SetBusy(kFALSE);
+  cout << "done " << endl;
 }
 
 
 ///_______________________________________________________________________________
 TObject * AliEveEventBuffer::NextEvent() {
   //See header file for documentation
-  cout << "In enxtevent"<<endl;
-
-
-  // if(fBusy) {
-  //   cout << "Event Buffer busy"<<endl;
-  //   return NULL;
-  // }
-  //  else SetBusy(kTRUE);
+  cout << "NextEvent()"<<endl;
   TObject * nextEvent = GetNextUnSeen();
-  //SetBusy(kFALSE);
   return nextEvent;
 }
 
@@ -252,9 +238,15 @@ Int_t AliEveEventBuffer::CalculateDifference(Int_t top, Int_t low) {
 void AliEveEventBuffer::StartBufferMonitor() {
   //cout << "NOT !!! starting buffer mon"<<endl;
   cout << "starting buffer mon"<<endl;
-  CreateBufferThread();
-  SetBufferMonStarted(kTRUE);
-  fTimer->Start(3000);
+  if(!GetBufferMonStarted()) {
+    CreateBufferThread();
+    SetBufferMonStarted(kTRUE);
+    fTimer->Start(3000);
+  } else {
+    cout << "Stopping buffer monitor"<<endl;
+    fTimer->Stop();
+    SetBufferMonStarted(kFALSE);
+  }
 }
 ///___________________________________________________________________________________
 void AliEveEventBuffer::StopBufferMonitor() {