bool AIThreadImpl::state_machine_done(LLThread* threadp)
{
  StateMachineThread_wat state_machine_thread_w(mStateMachineThread);
  AIStateMachineThreadBase* state_machine_thread = *state_machine_thread_w;
  bool need_cleanup = !state_machine_thread;
  if (!need_cleanup)
  {
	// If state_machine_thread is non-NULL, then AIThreadImpl::thread_done wasnt called yet
	// (or at least didn't return yet) which means the thread is still running.
	// Try telling the thread that it can stop.
	threadp->setQuitting();
	// Finally, mark that we are NOT going to do the cleanup by setting mStateMachineThread to NULL.
	*state_machine_thread_w = NULL;
  }
  return need_cleanup;
}
bool AIThreadImpl::thread_done(bool result)
{
  StateMachineThread_wat state_machine_thread_w(mStateMachineThread);
  AIStateMachineThreadBase* state_machine_thread = *state_machine_thread_w;
  bool need_cleanup = !state_machine_thread;
  if (!need_cleanup)
  {
	// If state_machine_thread is non-NULL then AIThreadImpl::abort_impl wasn't called,
	// which means the state machine still exists. In fact, it should be in the waiting() state.
	// It can also happen that the state machine is being aborted right now.
	llassert(state_machine_thread->waiting_or_aborting());
	state_machine_thread->schedule_abort(!result);
	// Note that if the state machine is not running (being aborted, ie - hanging in abort_impl
	// waiting for the lock on mStateMachineThread) then this is simply ignored.
	state_machine_thread->cont();
	// Finally, mark that we are NOT going to do the cleanup by setting mStateMachineThread to NULL.
	*state_machine_thread_w = NULL;
  }
  return need_cleanup;
}
bool AIThreadImpl::thread_done(bool result)
{
  StateMachineThread_wat state_machine_thread_w(mStateMachineThread);
  AIStateMachineThreadBase* state_machine_thread = *state_machine_thread_w;
  bool need_cleanup = !state_machine_thread;
  if (!need_cleanup)
  {
	// If state_machine_thread is non-NULL then AIThreadImpl::abort_impl wasn't called,
	// which means the state machine still exists. In fact, it should be in the waiting() state.
	// It can also happen that the state machine is being aborted right now (but it will still exist).
	// (Note that waiting() and running() aren't strictly thread-safe (we should really lock
	// mSetStateLock here) but by first calling waiting() and then running(), and assuming that
	// changing an int from the value 1 to the value 2 is atomic, this will work since the
	// only possible transition is from waiting to not running).
	llassert(state_machine_thread->waiting() || !state_machine_thread->running());
	state_machine_thread->schedule_abort(!result);
	// Note that if the state machine is not running (being aborted, ie - hanging in abort_impl
	// waiting for the lock on mStateMachineThread) then this is simply ignored.
	state_machine_thread->cont();
	// Finally, mark that we are NOT going to do the cleanup by setting mStateMachineThread to NULL.
	*state_machine_thread_w = NULL;
  }
  return need_cleanup;
}