src/ai/ai_threads.cpp
branchnoai
changeset 11094 72be0534cd0f
parent 10978 13fd0364b2c6
equal deleted inserted replaced
11093:c69806443a1c 11094:72be0534cd0f
   108 		AIFiber *main = stFind(MAIN_FIBER);
   108 		AIFiber *main = stFind(MAIN_FIBER);
   109 		main->SwitchToState(RUNNING);
   109 		main->SwitchToState(RUNNING);
   110 
   110 
   111 		/* The main fiber wants this AI to die */
   111 		/* The main fiber wants this AI to die */
   112 		if (this->state == STOPPING) {
   112 		if (this->state == STOPPING) {
   113 			/* Suspend, and exit */
   113 			/* Exit the fiber */
   114 			this->state = SUSPENDED;
       
   115 			this->Exit();
   114 			this->Exit();
   116 
   115 
   117 			/* Here we will never come as... the fiber is dead */
   116 			/* Here we will never come as... the fiber is dead */
   118 			assert(false);
   117 			assert(false);
   119 
   118 
   149 	 * @note Only call this from within the main fiber.
   148 	 * @note Only call this from within the main fiber.
   150 	 */
   149 	 */
   151 	void RunTick()
   150 	void RunTick()
   152 	{
   151 	{
   153 		/* If we already stopped, don't do anything */
   152 		/* If we already stopped, don't do anything */
   154 		if (this->state == STOPPED) return;
   153 		if (this->state == STOPPING || this->state == STOPPED) return;
   155 
   154 
   156 		/* Should be called from main fiber only */
   155 		/* Should be called from main fiber only */
   157 		assert(stCurrentFiber() == stFind(MAIN_FIBER));
   156 		assert(stCurrentFiber() == stFind(MAIN_FIBER));
   158 		/* Only AI fibers should be resumed this way */
   157 		/* Only AI fibers should be resumed this way */
   159 		assert(this != stFind(MAIN_FIBER));
   158 		assert(this != stFind(MAIN_FIBER));
   185 	void Stop()
   184 	void Stop()
   186 	{
   185 	{
   187 		/* Should be called from main fiber only */
   186 		/* Should be called from main fiber only */
   188 		assert(stCurrentFiber() == stFind(MAIN_FIBER));
   187 		assert(stCurrentFiber() == stFind(MAIN_FIBER));
   189 		/* If we already stopped, don't do anything */
   188 		/* If we already stopped, don't do anything */
   190 		if (this->state == STOPPED) return;
   189 		if (this->state == STOPPING || this->state == STOPPED) return;
   191 
   190 
   192 		/* Trigger a STOPPING round */
   191 		/* Trigger a STOPPING round */
   193 		assert(this->state == SUSPENDED);
   192 		assert(this->state == SUSPENDED);
   194 		this->SwitchToState(STOPPING);
   193 		this->SwitchToState(STOPPING);
   195 		assert(this->state == SUSPENDED);
   194 		assert(this->state == STOPPING);
   196 
   195 
   197 		/* Mark the fiber as STOPPED */
   196 		/* Mark the fiber as STOPPED */
   198 		this->state = STOPPED;
   197 		this->state = STOPPED;
   199 	}
   198 	}
   200 
   199 
   252 		stCurrentFiber()->state = SUSPENDED;
   251 		stCurrentFiber()->state = SUSPENDED;
   253 
   252 
   254 		/* Switch to new state, activate fiber, and check if we are suspended again */
   253 		/* Switch to new state, activate fiber, and check if we are suspended again */
   255 		this->state = new_state;
   254 		this->state = new_state;
   256 		this->fiber->SwitchToFiber();
   255 		this->fiber->SwitchToFiber();
   257 		assert(this->state == SUSPENDED);
   256 		assert(this->state == SUSPENDED || this->state == STOPPING);
   258 	}
   257 	}
   259 
   258 
   260 	/**
   259 	/**
   261 	 * The method that runs in the new Fiber. Automaticly called when the Fiber becomes active.
   260 	 * The method that runs in the new Fiber. Automaticly called when the Fiber becomes active.
   262 	 */
   261 	 */
   285 			while (this->state != STOPPING) {
   284 			while (this->state != STOPPING) {
   286 				main->SwitchToState(RUNNING);
   285 				main->SwitchToState(RUNNING);
   287 			}
   286 			}
   288 		}
   287 		}
   289 
   288 
   290 		/* Suspend, and exit */
   289 		/* Stopping, and exit */
   291 		this->state = SUSPENDED;
   290 		this->state = STOPPING;
   292 		this->Exit();
   291 		this->Exit();
   293 
   292 
   294 		/* Here we will never come as... the fiber is dead */
   293 		/* Here we will never come as... the fiber is dead */
   295 		assert(false);
   294 		assert(false);
   296 	}
   295 	}