Index: source/ariba/utility/system/SystemQueue.cpp
===================================================================
--- source/ariba/utility/system/SystemQueue.cpp	(revision 12761)
+++ source/ariba/utility/system/SystemQueue.cpp	(revision 12762)
@@ -43,9 +43,8 @@
 namespace utility {
     
+    typedef boost::mutex::scoped_lock scoped_lock;
     
     using boost::posix_time::microsec_clock;
     using boost::posix_time::time_duration;
-//     using boost::mutex::scoped_lock;
-    typedef boost::mutex::scoped_lock scoped_lock;
     using boost::date_time::not_a_date_time;
     using boost::scoped_ptr;
@@ -54,7 +53,8 @@
 use_logging_cpp(SystemQueue);
 
-SystemQueue::SystemQueue()
-{
-	logging_info("Creating SystemQueue at: " << this);
+SystemQueue::SystemQueue() :
+    SysQ( new QueueThread() )
+{
+	logging_debug("Creating SystemQueue at: " << this);
 }
 
@@ -76,5 +76,5 @@
     SystemEvent ev(event);
     
-    SysQ.insert(ev, delay);
+    SysQ->insert(ev, delay);
 }
 
@@ -95,10 +95,10 @@
 void SystemQueue::run()
 {
-    assert ( ! SysQ.running );
-    
-    SysQ.running = true;
+    assert ( ! SysQ->running );
+    
+    SysQ->running = true;
     
     // start thread
-    sysq_thread.reset( new boost::thread(boost::ref(SysQ)) );
+    sysq_thread.reset( new boost::thread(boost::ref(*SysQ)) );
 }
 
@@ -106,9 +106,9 @@
 {
     // signal SysQ to quit (and abort queued events)
-    SysQ.cancel();
+    SysQ->cancel();
     
     // wait till actually completes
     //   (should be fast, but the current event is allowed to finish)
-    logging_info("/// joining system queue thread");
+    logging_debug("/// ... joining SysQ thread");
     sysq_thread->join();
     
@@ -116,10 +116,10 @@
     sysq_thread.reset();
     
-    assert ( ! SysQ.isRunning() );
-    
-    
-    // TODO FIXME gtest reuses the singleton... :-/
-    //   maybe delete the SysQ object here..? (scoped_ptr...)
-    SysQ.aborted = false;  // XXX hotfix..
+    assert ( ! SysQ->isRunning() );
+
+    
+    // clean up and respawn
+    logging_debug("/// respawning SysQ");
+    SysQ.reset( new QueueThread() );
 }
 
@@ -133,13 +133,10 @@
 bool SystemQueue::isEmpty()
 {
-//  XXX
-// 	return directScheduler.isEmpty() || delayScheduler.isEmpty();
-
-    return true;
+    return SysQ->isEmpty();
 }
 
 bool SystemQueue::isRunning()
 {
-    return SysQ.isRunning();
+    return SysQ->isRunning();
 }
 
@@ -159,4 +156,5 @@
     now( not_a_date_time ),
     next_deadline( not_a_date_time ),
+    processing_event( false ),
     running( false ),
     aborted( false )
@@ -170,6 +168,5 @@
 void SystemQueue::QueueThread::operator()()
 {
-    // XXX debug
-    logging_info( "/// SysQ thread is alive." );
+    logging_debug( "/// SysQ thread is alive." );
     
     assert( running );  // this is set before the thread starts
@@ -188,6 +185,5 @@
     }
     
-    // XXX debug
-    logging_info( "/// SysQ thread is quitting." );
+    logging_debug( "/// SysQ thread is quitting." );
     
     running = false;
@@ -213,4 +209,6 @@
         {
             scoped_lock lock( queue_mutex );
+            
+            this->processing_event = true;
             
             // dequeue first event
@@ -219,6 +217,5 @@
         }
         
-        // XXX debug
-        logging_info("/// SysQ: dispatching event");
+        logging_debug("/// SysQ: dispatching event");
         
         currently_processed_event->getListener()->handleSystemEvent( *currently_processed_event );
@@ -228,4 +225,6 @@
     now = get_clock();   // NOTE: this is reused in check_timed_queue();
     time_duration execution_time = now - start_time;
+    
+    this->processing_event = false;
     
     // DEBUG OUTPUT: warning when execution takes too much time
@@ -233,5 +232,5 @@
     if ( execution_time.total_milliseconds() > 50 )
     {
-        logging_debug("WARNING: Last event took " << execution_time.total_milliseconds() << " to complete.");
+        logging_info("WARNING: Last event took " << execution_time.total_milliseconds() << " to complete.");
     }
 }
@@ -290,6 +289,5 @@
         if ( this->next_deadline.is_not_a_date_time() )
         {
-            // XXX debug output
-            logging_info("/// SysQ is going to sleep (forever..).");
+            logging_debug("/// SysQ is going to sleep.");
             
             this->system_queue_idle.wait( lock );
@@ -298,7 +296,7 @@
         else
         {
-            // XXX debug output
-            time_duration sleep_time = next_deadline - get_clock();
-            logging_info("/// SysQ is going to sleep for " << sleep_time.total_milliseconds() << "ms.");
+            logging_debug("/// SysQ is going to sleep for "
+                        << ( next_deadline - get_clock() ).total_milliseconds()
+                        << "ms.");
 
             this->system_queue_idle.timed_wait( lock, next_deadline );
@@ -326,5 +324,5 @@
 void SystemQueue::QueueThread::cancel()
 {
-    logging_debug("/// cancelling system queue");
+    logging_debug("/// Cancelling system queue... ");
 
     // SYNCHRONIZED
@@ -334,6 +332,5 @@
     }
     
-    // XXX debug output
-    logging_info("/// SysQ: " << immediateEventsQ.size() << " immediate event(s) + "
+    logging_debug("/// SysQ: " << immediateEventsQ.size() << " immediate event(s) + "
                               << timedEventsQ.size() << " timed event(s) left.");
     
@@ -360,4 +357,13 @@
     system_queue_idle.notify_one();  // NOTE: there is only one thread
                                      // (so it doesn't matter whether to call notify_one, or notify_all)
+}
+
+
+bool SystemQueue::QueueThread::isEmpty()
+{
+    // SYNCHRONIZED
+    scoped_lock lock( queue_mutex );
+    
+    return immediateEventsQ.empty() && timedEventsQ.empty() && ! processing_event;
 }
 
Index: source/ariba/utility/system/SystemQueue.h
===================================================================
--- source/ariba/utility/system/SystemQueue.h	(revision 12761)
+++ source/ariba/utility/system/SystemQueue.h	(revision 12762)
@@ -206,7 +206,4 @@
         EventQueue immediateEventsQ;
         EventQueue timedEventsQ;
-//      boost::mutex queueMutex;
-        
-//         SystemEvent& currently_processed_event;
         
         ptime now;
@@ -215,6 +212,7 @@
         boost::condition_variable system_queue_idle;
         boost::mutex queue_mutex;
-        
-// 		boost::condition_variable itemsAvailable;  DEPRECATED
+
+        bool processing_event;
+        
 		volatile bool running;
         volatile bool aborted;
@@ -244,5 +242,5 @@
     /// member variables of class SystemQueue
 private:
-    QueueThread SysQ;
+    boost::scoped_ptr<QueueThread> SysQ;
     boost::scoped_ptr<boost::thread> sysq_thread;
     
