(Referring to the threads by the name of the pthread handle variable.)
- Main thread
- Handles: Rendering, gui, sound, input
- The "main loop" is the framestarted function in ODFrameListener.
- I think the rendering, gui and input has to be done in the main thread, especially since the ogre version in the PPA is not compiled with thread support. Not much point in putting the sound in it's own thread, though it might need a lock as it could be called from more than one thread.
- serverThread - serverSocketProcessor() in Server.cpp -
- Started from: startServer() in Functions.cpp
- Handles: Incoming connections.
- Blocks while waiting for a new connection. Spawns a clientHandlerThread whenever a client connects.
- Function comment:
- {l Code}: {l Select All Code}
/*! \brief A thread function which runs on the server and listens for new connections from clients.
*
* A single instance of this thread is spawned by running the "host" command
* from the in-game console. The thread then binds annd listens on the
* specified port and when clients connect a new socket, and a
* clientHandlerThread are spawned to handle communications with that client.
* This function currently has no way of breaking out of its primary loop, so
* once it is started it never exits until the program is closed.
*/
- serverNotificationThread - serverNotificationProcessor() in Server.cpp
- Started from: startServer() in Functions.cpp
- Handles: Sending events (serverNotifications) to clients
- Blocks with sem_wait() while waiting for a new item to appear in the queue.
- Function comment:
- {l Code}: {l Select All Code}
/*! \brief The thread which monitors the serverNotificationQueue for new events and informs the clients about them.
*
* This thread runs on the server and acts as a "consumer" on the
* serverNotificationQueue. It takes an event out of the queue, determines
* which clients need to be informed about that particular event, and
* dispacthes TCP packets to inform the clients about the new information.
*/
- creatureThread - creatureAIThread() in Server.cpp
- Started from: startServer() in Functions.cpp
- Handles: Incrementing turn, running the game logic.
- Runs untill ODFrameListener::getThreadStopRequested() returns true.
- Function comment:
- {l Code}: {l Select All Code}
/*! \brief The thread which decides what creatures will do and carries out their actions.
*
* Creature AI is currently done only on an individual basis. The creatures
* are looped over, calling each one's doTurn method in succession. The
* doTurn method is responsible for deciding what action is taken by the
* creature in the upcoming turn. Once a course of action has been decided
* upon the doTurn method also moves the creature, sets its animation state,
* adjusts the creature's HP, etc.
*
* Since the creature AI thread runs on the server, changes to the creature's
* state must be communicated to the some or all clients (depending on fog of
* war, etc). This is accomplished by building a ServerNotification request
* and placing it in the serverNotificationQueue. Since the
* serverNotificationProcessor will decide which clients should know about a
* given event, we can simply generate a notification request for any state
* change and dump it in the queue and not worry about which clients need to
* know about it.
*/
- creatureDoTurnThread - GameMap::creatureDoTurnThread() -> GameMap::doCreatureTurns(); in GameMap.cpp
- Started from: GameMap::doTurn() in GameMap.cpp (which is again called by creatureThread)
- Handles: Spawns threads to do the creature AI.
- Spawned and joined each turn.
- miscUpkeepThread - GameMap::miscUpkeepThread() -> GameMap::doMiscUpkeep(); in GameMap.cpp
- Started from: GameMap::doTurn() in GameMap.cpp (which is again called by creatureThread)
- Handles:
- Checks if a seat has won,
- runs upkeep on all activeObject objects in the map.
- Handles kobold spawning
- Clears empty rooms from the GameMap
- Updates mana and gold totals
- Increment tile counts
- Spawned and joined each turn.
- creatureDoTurnHelperThread - GameMap::creatureDoTurnHelperThread() in GameMap.cpp
- Started from: GameMap::doCreatureTurns() in GameMap.cpp (which is again called by creatureDoTurnThread)
- Handles: Creature decisions, AI (calls Creature::doTurn) on all creatures)
- One or more of these are spawned and joined each turn according to a parameter. (defaults to 1, according to a comment more will make it segfault currently).