[MUD-Dev] Multithreading: AI, Sockets, Brains, Bodies, and Storage

Brian Lindahl lindahlb at hotmail.com
Wed Feb 19 19:53:16 CET 2003


Multithreading: AI, Sockets, Brains, Bodies, and Storage
--------------------------------------------------------

  Since there doesn't seem to be any real consensus as to how
  threads should be used in a MUD running on a multiprocessor
  machine, I'll set up a good design for such: add multiple threads
  but remove the need for complex synchronization thus making
  development/debugging time minimal with maximum benefits.

  In addition, I'll be following my previous entity design pattern
  of Brains/Bodies (described for those of you who are unfamiliar
  with it).

Specifications
---------------------------

  IO Specifications
  
    - Input and output is stored in a tokenized form to provide
    optimization of execution 
  
    - Input and output tokens (flushed per tick) are not serialized
    to Storage
  
    - All entities such as characters, objects, accounts, questions,
    are considered Bodies
  
    - Each Body can have a Brain which provides control
  
    - Bodies contain output tokens
  
    - Brains contain input tokens Thread Specifications
  
    - Threads are allocated via a pool, called a Spool
  
    - Machine-specific optimization can be done by altering the
    Spool size
  
    - Threads can be suspended/resumed
  
    - SpawnThread creates a new thread
  
    - SpawnTiedThread creates a new tied thread
  
    - WaitOnThreads waits until all threads spawned from calling
    thread return
  
    - WaitOnTiedThreads waits until all tied threads spawned from
    calling thread return Brain Specifications
  
    - A PC Brain is a Socket
  
    - An NPC Brain is AI
  
    - When a Socket's Body has output tokens, it is found in
    SocketOutput
  
    - When an AI's Body has output tokens, it is found in AIOutput
  
    - Generate output tokens to simulate needs Body Specifications
  
    - Take input tokens and execute them to their own specification
    (if possible)
  
    - Block output tokens to the Brain
  
    - Generate input tokens from output tokens to simulate impulse
    responses
  
    - When a Body or Body's Brain has input tokens, it is found in
    InputList AI Specifications
  
    - Soft-coded scripts can be assigned to NPCs individually or to
    NPC-Types
  
    - NPC-Types can have hard coded responses.
  
    - Soft-coded script responses take precedence.
  
    Ideally, the only synchronization done will be waiting until
    threads finish execution, removal/addition of threads into the
    Spool. Note that if no threads are available in the Spool,
    SpawnThread/SpawnTiedThread suspends itself until it is signaled
    that one is available.
  
Concurrency:

  Since AI only reads game data, Storage can be done concurrently
  (assuming input/output tokens are not serialized as stated
  above). Since Socket Input/Output doesn't write to game data, it
  can be done concurrently with Storage and AI. Thus, all four
  proceedures can be executed concurrently. However, since game
  execution writes to Socket Output, AI and Storage (if asynchronous
  flushes are done by the OS) and reads from Socket Input and AI, it
  cannot be done concurrently with any of the operations. Thus, we
  wait on all threads before performing game execution. Although we
  could have Game Event execution done concurrently with Socket
  Input, it makes more sense from a player's point of view that
  their Input is executed before Game Events.

Functions
---------

  Main Loop

    a) ForEach Socket in SocketList: SpawnThread 'Socket Input'
  
    b) WaitOnThreads
  
    c) execute Server Events (shutdown, reboot, etc.)
  
    d) ForEach Body in InputList: execute input tokens
  
    e) execute Game Events
  
    f) SpawnThread 'Storage'
  
    g) ForEach Socket in SocketOutput: SpawnTiedThread 'Socket
    Output'
  
    h) ForEach AI in AIOutput: SpawnThread 'AI Response'
  
    i) Sleep to synchronize loop time
  
    j) WaitOnTiedThreads
  
    k) Repeat
  
  Socket Input
  
    a) Flush input into input buffer
  
    b) Retrieve first line
  
    c) Tokenize line
  
    d) Check proper syntax for token sequence
  
    e) Remove line from input buffer
  
  Game Events
  
    a) Event = event with the earliest execution time in queue
  
    b) If Event's execution time > now, End
  
    c) Execute Event
  
    d) Remove Event from queue
  
    e) Repeat
  
  Storage
  
    a) Synchronize in-memory data with in-storage
    data. (i.e. database commitals)
  
  Socket Output
  
    a) Generate text from output tokens
  
    b) Parse output codes in text (if any)
  
    c) Send text to socket
  
  AI Response
  
    a) Soft-coded scripts generate input tokens from output tokens
  
    b) Hard code generates extra input tokens from remaining output
    tokens
  
---------------------
Brian Lindahl
Development Director
The Cathyle Project
_______________________________________________
MUD-Dev mailing list
MUD-Dev at kanga.nu
https://www.kanga.nu/lists/listinfo/mud-dev



More information about the mud-dev-archive mailing list