void SC_TerminalClient::endInput()
{
	// NOTE: On Windows, there is no way to safely interrupt
	// the pipe-reading thread. So just quit and let it die.

#ifdef _WIN32
	if (mUseReadline) {
#endif
	// wake up the input thread in case it is waiting
	// for input to be processed
	lockInput();
		mInputShouldBeRunning = false;
		pthread_cond_signal( &mInputCond );
	unlockInput();

#ifndef _WIN32
	char c = 'q';
	ssize_t bytes = write( mInputCtlPipe[1], &c, 1 );
	if( bytes < 1 )
		postfl("WARNING: could not send quit command to input thread.\n");
#else
	SetEvent( mQuitInputEvent );
#endif

	postfl("main: waiting for input thread to join...\n");
	pthread_join( mInputThread, NULL );

#ifdef _WIN32
	} // if (mUseReadline)
#endif
	postfl("main: quitting...\n");
}
void SC_TerminalClient::pushCmdLine( SC_StringBuffer &buf, const char *newData, size_t size)
{
	lockInput();

	bool signal = false;

	while (size--) {
		char c = *newData++;
		switch (c) {
		case kRecompileLibrary:
		case kInterpretCmdLine:
		case kInterpretPrintCmdLine:
			mInputBuf.append( buf.getData(), buf.getSize() );
			mInputBuf.append(c);
			signal = true;
			buf.reset();
			break;

		default:
			buf.append(c);
		}
	}

	if(signal) sendSignal(sig_input);

	unlockInput();
}
void SC_TerminalClient::commandLoop()
{
	bool haveNext = false;
	struct timespec nextAbsTime;

	lockSignal();

	while( shouldBeRunning() )
	{

		while ( mSignals ) {
			int sig = mSignals;
			mSignals = 0;

			unlockSignal();

			if (sig & sig_input) {
				//postfl("input\n");
				lockInput();
				interpretInput();
				unlockInput();
			}

			if (sig & sig_sched) {
				//postfl("tick\n");
				double secs;
				lock();
				haveNext = tickLocked( &secs );
				unlock();

				flush();

				//postfl("tick -> next time = %f\n", haveNext ? secs : -1);
				ElapsedTimeToTimespec( secs, &nextAbsTime );
			}

			if (sig & sig_stop) {
				stopMain();
			}

			if (sig & sig_recompile) {
				recompileLibrary();
			}

			lockSignal();
		}

		if( !shouldBeRunning() ) {
			break;
		}
		else if( haveNext ) {
			int result = pthread_cond_timedwait( &mCond, &mSignalMutex, &nextAbsTime );
			if( result == ETIMEDOUT ) mSignals |= sig_sched;
		}
		else {
			pthread_cond_wait( &mCond, &mSignalMutex );
		}
	}

	unlockSignal();
}
void SC_TerminalClient::commandLoop()
{
	bool haveNext = false;
	struct timespec nextAbsTime;

	lockSignal();

	while( shouldBeRunning() )
	{

		while ( mSignals ) {
			int sig = mSignals;

			unlockSignal();

			if (sig & sig_input) {
				//postfl("input\n");
				lockInput();
				interpretInput();
				// clear input signal, as we've processed anything signalled so far.
				lockSignal();
				mSignals &= ~sig_input;
				unlockSignal();
				unlockInput();
			}

			if (sig & sig_sched) {
				//postfl("tick\n");
				double secs;
				lock();
				haveNext = tickLocked( &secs );
				// clear scheduler signal, as we've processed all items scheduled up to this time.
				// and will enter the wait according to schedule.
				lockSignal();
				mSignals &= ~sig_sched;
				unlockSignal();
				unlock();

				flush();

				//postfl("tick -> next time = %f\n", haveNext ? secs : -1);
				ElapsedTimeToTimespec( secs, &nextAbsTime );
			}

			if (sig & sig_stop) {
				stopMain();
				lockSignal();
				mSignals &= ~sig_stop;
				unlockSignal();
			}

			if (sig & sig_recompile) {
				recompileLibrary();
				lockSignal();
				mSignals &= ~sig_recompile;
				unlockSignal();
			}

			lockSignal();
		}

		if( !shouldBeRunning() ) {
			break;
		}
		else if( haveNext ) {
			int result = pthread_cond_timedwait( &mCond, &mSignalMutex, &nextAbsTime );
			if( result == ETIMEDOUT ) mSignals |= sig_sched;
		}
		else {
			pthread_cond_wait( &mCond, &mSignalMutex );
		}
	}

	unlockSignal();
}
void SC_TerminalClient::commandLoop()
{
	bool haveNext = false;
	mutex_chrono::system_clock::time_point nextAbsTime;

	lockSignal();

	while( shouldBeRunning() ) {

		while ( mSignals ) {
			int sig = mSignals;
			mSignals = 0;

			unlockSignal();

			if (sig & sig_input) {
				//postfl("input\n");
				lockInput();
				interpretInput();
				unlockInput();
			}

			if (sig & sig_sched) {
				//postfl("tick\n");
				double secs;
				lock();
				haveNext = tickLocked( &secs );
				unlock();

				flush();

				//postfl("tick -> next time = %f\n", haveNext ? secs : -1);
				ElapsedTimeToChrono( secs, nextAbsTime );
			}

			if (sig & sig_stop) {
				stopMain();
			}

			if (sig & sig_recompile) {
				recompileLibrary();
			}

			lockSignal();
		}

		if( !shouldBeRunning() ) {
			break;
		}
		else if( haveNext ) {
			unlockSignal();
			{
				unique_lock<SC_Lock> lock(mSignalMutex);

				cv_status status = mCond.wait_until(lock, nextAbsTime);
				if( status == cv_status::timeout )
					mSignals |= sig_sched;
			}
			lockSignal();
		}
		else {
			unlockSignal();
			{
				unique_lock<SC_Lock> lock(mSignalMutex);
				mCond.wait(lock);
			}
			lockSignal();
		}
	}

	unlockSignal();
}