int TaskScheduler::terminateAllTasks()
{
	TaskSet copy = runningThreads;
	TaskSet::iterator it;
	PRINTF(1,"Terminating all tasks\n");
	for (it = copy.begin();it!=copy.end();it++) {
		// delete pointer and empty the list of running tasks
		terminateTask(it->second);
	}
	runningThreads.clear();
	mainThread.reset();
	return 0;
}
void SchedViz::Visit_Node( const Semantics::Node &node ) {
	std::string nodeName = node.name();
	DBGOUT("Node: " << nodeName << std::endl)

	// Add a superblock to the viz
	TVSuperblock* superblock = this->_traceViz->AddSuperblock( nodeName, this->_hyperperiodSec );

	// Now iterate through all of the task children
	TaskSet taskSet = node.executes();
	TaskSet::iterator taskIter = taskSet.begin();
	for ( ; taskIter != taskSet.end(); taskIter++ ) {
		// Visit the task
		this->Visit_Task( *taskIter, superblock );
	}

	// Now interate through all of the device children
	DeviceSet deviceSet = node.integrates();
	DeviceSet::iterator deviceIter = deviceSet.begin();
	for ( ; deviceIter != deviceSet.end(); deviceIter++ ) {
		// Visit the device
		this->Visit_Device( *deviceIter, superblock );
	}
}
void TrueTimeSourceVisitor::Visit_Node( const Semantics::Node & node ) {

	// Setup the node name
	std::string kernelName = node.name();
	std::string kernelInitName = kernelName + "_init";
	DEBUGOUT( "\tNode: " << kernelName << std::endl );

	// Must be the second pass
		_SchedHeaderLines.push_back( string( "// Define Schedule Offsets and Durations" ) );
		_SchedHeaderLines.push_back( string( "" ) );
	
		// Create the file name
		std::string filename = std::string( node.name() ) + "_init";

		// Create the hyperperiod symbol string for the node
		std::string hyp_str = kernelName + "_HYPERPERIOD";

		// Set some dictionary items for this node
		GetMainDictionary().SetValue( "FILENAME", filename );
		GetMainDictionary().SetValue( "KERNEL_SCHEDULE", "prioFP" );
		GetMainDictionary().SetFormattedValue( "KERNEL_HYPERPERIOD", "%f", (double) node.hyperperiodsecs() );
		GetMainDictionary().SetValue( "NODE_HYPERPERIOD_STR", hyp_str );

		DeviceSet devices = node.integrates();

		for ( DeviceSet::iterator devIter = devices.begin(); devIter != devices.end(); devIter++ )
		{
			if ( (*devIter).type() == Semantics::CommInterface::meta )
			{
				Semantics::CommInterface ci = Semantics::CommInterface::Cast( *devIter );
				Semantics::CommMedium cm = ci.commMedium();

				if ( cm != Udm::null ) {
					string busname = cm.name();
					AddSectionDictionary( "BUS_DEFINES" );
					GetTemplateDictionary().SetValue( "BUSNAME", busname );
					PopSectionDictionary();
				}
			}
		}
			

		if ( _SchedHeaderLines.size() < 4 )
		{
			ostringstream out;
			out << "#define " << hyp_str << " " << (double)node.hyperperiodsecs();
			_SchedHeaderLines.push_back( out.str() );
		}

		_already_included.clear();  // clear include list for each node

		// Visit all tasks assigned to this node
		TaskSet taskSet = node.executes();
		TaskVector taskVector( taskSet.begin(), taskSet.end() );
		TaskVector::iterator taskIter = taskVector.begin();
		for ( ; taskIter != taskVector.end(); taskIter++ ){
			// Visit the task
			taskIter->Accept( *this );
		}

		// Clear the analog in/out counters
		_analogIn = 1;
		_analogOut = 1;

		// Set up a sorted list of IChans and OChans
		SortedIChan_ByChanIndex_Set ichans;
		SortedOChan_ByChanIndex_Set ochans;

		// Visit all devices contained in this node
		DeviceSet deviceSet = node.integrates();
		DeviceSet::iterator deviceIter = deviceSet.begin();
		for ( ; deviceIter != deviceSet.end(); deviceIter++ ) {

			// Collect all of the IChans and OChans, and keep them in the proper sorted order
			// See if device is input
			if ( Semantics::InputDevice::meta == (*deviceIter).type() ) {

				// Cast to an InputDevice
				Semantics::InputDevice input = Semantics::InputDevice::Cast( *deviceIter );

				// Get the associated IChans
				SortedIChan_ByChanIndex_Set iChanSet = input.inputChannels_sorted( IChanIndexSorter() );
				ichans.insert( iChanSet.begin(), iChanSet.end() );
			}

			// See if device is output
			else if ( Semantics::OutputDevice::meta == (*deviceIter).type() ) {

				// Cast to an InputDevice
				Semantics::OutputDevice output = Semantics::OutputDevice::Cast( *deviceIter );

				// Get the associated OChans
				SortedOChan_ByChanIndex_Set oChanSet = output.outputChannels_sorted( OChanIndexSorter() );
				ochans.insert( oChanSet.begin(), oChanSet.end() );
			}
		}

		// Now process the IChans and OChans in order
		IChanVector iChanVector( ichans.begin(), ichans.end() );
		for ( IChanVector::iterator ichanIter = iChanVector.begin(); ichanIter != iChanVector.end(); ichanIter++ )
		{
			ichanIter->Accept( *this );
		}

		OChanVector oChanVector( ochans.begin(), ochans.end() );
		for ( OChanVector::iterator ochanIter = oChanVector.begin(); ochanIter != oChanVector.end(); ochanIter++ )
		{
			ochanIter->Accept( *this );
		}

		// Visit all local dependencies on this node
		LocalDependencySet dependencySet = node.nodeLocalDeps();
		LocalDependencyVector dependencyVector( dependencySet.begin(), dependencySet.end() );
		LocalDependencyVector::iterator dependencyIter = dependencyVector.begin();
		for ( ; dependencyIter != dependencyVector.end(); dependencyIter++ ) {

			// Visit the dependency
			dependencyIter->Accept( *this );
		}

		// Visit the dummy dependency message on this node (if we have one)
		Semantics::Msg dummyMsg = node.nodeDummyMsg();
		if ( dummyMsg != Udm::null )
			Visit_DummyMessage( dummyMsg );


		// Get the template file name
		std::string templateName = ConfigKeeper::inst().GetTemplatePath() + "\\truetime_src.tpl";
		// Initialize the template 
		ctemplate::Template* googleTemplate = ctemplate::Template::GetTemplate( templateName, ctemplate::DO_NOT_STRIP );
		std::string output;
		// Expand the output into a string
		googleTemplate->Expand( &output, &GetMainDictionary() );
		
		// Create the _init file for this TT kernel
		std::string directoryName = ConfigKeeper::inst().GetDirectoryName();
		string srcfilename = directoryName + "\\" + filename + ".cpp";
		ofstream initFile( srcfilename.c_str() );
		// Write the generated code out to the file and close
		initFile << output;
		// Close up the file
		initFile.close();
		// Clear out the dictionary
		ClearDictionary();

		// Write out the header file for the defines
		string hdrfilename = directoryName + "\\" + filename + "_defines.h";
		cout << "Writing header: " << hdrfilename << endl;
		ofstream headerFile( hdrfilename.c_str() );

		for ( std::vector< std::string >::iterator lineIter = _SchedHeaderLines.begin(); 
				lineIter != _SchedHeaderLines.end(); lineIter++ )
		{
			headerFile << *lineIter << endl;
		}
		headerFile.close();
		_SchedHeaderLines.clear(); // flush the list
}