Skip to main content

Transactions and the order of work

A world runs its server scripts on one thread, one transaction at a time, in the order below. The world itself (field values, saving, the events coming in) is the game's; Kalem reads fields from it and hands it the writes of each transaction.

Transactions​

A transaction is one handler (an event, a timer, ready, migrate) running once, with everything it calls.

  • The exit and enter a goto causes are in the same transaction.
  • In a new instance, the auto state's enter and on ready are one transaction.

Everything a transaction does is gathered in one record: the last value of every field it wrote and the line that wrote it; memory and state; the timers it started and stopped; the state of its random numbers.

  • A transaction sees its own writes: business.visitors += 1 twice adds 2.
  • When it ends, it is applied in this order:
    1. the game checks all the field writes together: type, min/max, one writer;
    2. it applies them at once; then the rest of the record takes effect;
    3. only after that do the events it caused (object.stateChanged) and the new signal values come about.
  • If it fails, nothing of it is applied. Its log lines stay, marked as failed.

The order of work​

The world's clock counts whole minutes. When it moves on, every minute in between is worked through in turn; fast play neither skips nor merges minutes. A minute's work has two steps:

  1. Timers due run in instance order. Within one instance, declared timers come first (in the order declared), then started ones (in the order started).
  2. Outside events (Play's test events, events of the game) come in the order the game gives them.

A moment is one timer firing or one outside event, with every transaction it leads to:

  • the event is delivered to all its receivers in instance order;
  • events raised while it is delivered join a first-in, first-out queue and are delivered the same way after it;
  • when the queue is empty, the moment ends and the next piece of work begins.

Instance order: the object's id → the component's place on the object → the binding's id.

So at 03:00 the closing timer runs before a visitor arriving in the same minute: the business has closed, and the visitor is not counted.