Skip to main content

Instructions

The Instructions object defines all instruction types supported by the simulator. Each instruction type represents an atomic operation that a thread can perform.

Instruction Types

All instruction types are exported as string constants:
instructions.js
export const Instructions = {
  ACQUIRE: "acquire",
  WITHDRAW: "withdraw",
  DEPOSIT: "deposit",
  RELEASE: "release",
  WAIT_SEM: "wait_sem",
  PRINT: "print",
  SIGNAL_SEM: "signal_sem",
  WAIT_FOOD: "wait_food",
  EAT_FOOD: "eat_food",
  COOK_DISH: "cook_dish",
  SIGNAL_FOOD: "signal_food",
  ENTER_READ: "enter_read",
  READ_BOOK: "read_book",
  EXIT_READ: "exit_read",
  ENTER_WRITE: "enter_write",
  UPDATE_CATALOG: "update_catalog",
  EXIT_WRITE: "exit_write",
  RUN_STAGE: "run_stage",
  BARRIER_WAIT: "barrier_wait",
  BUILD_STAGE: "build_stage",
  JOIN_THREAD: "join_thread",
  AWAIT_ALL: "await_all",
  COMPLETE_HOUSE: "complete_house",
  PETERSON_LOCK: "peterson_lock",
  USE_STATION: "use_station",
  PETERSON_UNLOCK: "peterson_unlock",
  END: "end"
};

Instruction Categories

Mutex Instructions

Tries to lock the mutex. Blocks if already locked.
{ type: Instructions.ACQUIRE }
Blocking: Yes (if mutex is locked)
Unlocks the mutex and transfers ownership to next waiting thread.
{ type: Instructions.RELEASE }
Blocking: No

Bank Account Instructions

Removes money from shared account balance.
{ type: Instructions.WITHDRAW, amount: 100 }
amount
number
required
Amount to withdraw (capped at available balance)
Blocking: No
Adds money to shared account balance.
{ type: Instructions.DEPOSIT, amount: 50 }
amount
number
required
Amount to deposit
Blocking: No

Semaphore Instructions

Attempts to acquire a token and printer. Blocks if none available.
{ type: Instructions.WAIT_SEM }
Blocking: Yes (if count is 0)
Releases printer and token. May transfer to waiting thread.
{ type: Instructions.SIGNAL_SEM }
Blocking: No

Condition Variable Instructions

Customer waits for available dish. Blocks if none ready.
{ type: Instructions.WAIT_FOOD }
Blocking: Yes (if no dishes available)
Consumes reserved dish.
{ type: Instructions.EAT_FOOD }
Blocking: No
Chef prepares one dish.
{ type: Instructions.COOK_DISH }
Blocking: No
Chef notifies one waiting customer.
{ type: Instructions.SIGNAL_FOOD }
Blocking: No

Monitor Instructions

Request read access to library catalog. Multiple readers allowed.
{ type: Instructions.ENTER_READ }
Blocking: Yes (if writer is active)
Simulates reading operation.
{ type: Instructions.READ_BOOK }
Blocking: No
Release read access.
{ type: Instructions.EXIT_READ }
Blocking: No
Request exclusive write access.
{ type: Instructions.ENTER_WRITE }
Blocking: Yes (if any readers or writer active)
Modifies catalog (increments version).
{ type: Instructions.UPDATE_CATALOG }
Blocking: No
Release write access.
{ type: Instructions.EXIT_WRITE }
Blocking: No

Barrier Instructions

Advance through race segment.
{ type: Instructions.RUN_STAGE }
Blocking: No
Wait at checkpoint until all racers arrive.
{ type: Instructions.BARRIER_WAIT }
Blocking: Yes (until all participants arrive)

Join/Await Instructions

Complete one stage of house construction.
{ type: Instructions.BUILD_STAGE, stageName: "Foundation", duration: 3 }
stageName
string
required
Name of construction stage
duration
number
required
Number of ticks to complete
Blocking: No
Wait for specific thread to finish.
{ type: Instructions.JOIN_THREAD, targetName: "Worker-1" }
targetName
string
required
Name of thread to wait for
Blocking: Yes (until target finishes)
Wait for all specified threads to finish.
{ type: Instructions.AWAIT_ALL, targets: ["Worker-1", "Worker-2"] }
targets
Array<string>
required
Array of thread names to wait for
Blocking: Yes (until all targets finish)
Final delivery marker.
{ type: Instructions.COMPLETE_HOUSE }
Blocking: No

Peterson’s Algorithm Instructions

Acquire station using Peterson’s algorithm.
{ type: Instructions.PETERSON_LOCK, robotId: 0 }
robotId
number
required
Robot identifier (0 or 1)
Blocking: Yes (busy-wait until lock acquired)
Perform work at shared station.
{ type: Instructions.USE_STATION }
Blocking: No
Release station using Peterson’s algorithm.
{ type: Instructions.PETERSON_UNLOCK, robotId: 0 }
robotId
number
required
Robot identifier (0 or 1)
Blocking: No

Control Flow Instructions

Explicitly mark thread as finished.
{ type: Instructions.END }
Blocking: No (terminates thread)

Usage in Scenarios

Bank Account (Mutex)

const client = new Thread("Client-1", [
  { type: Instructions.ACQUIRE },
  { type: Instructions.WITHDRAW, amount: 100 },
  { type: Instructions.RELEASE }
]);

Printer Control (Semaphore)

const job = new Thread("Job-1", [
  { type: Instructions.WAIT_SEM },
  { type: Instructions.PRINT, pages: 10 },
  { type: Instructions.SIGNAL_SEM }
]);

Restaurant (Condition Variable)

const customer = new Thread("Customer-1", [
  { type: Instructions.WAIT_FOOD },
  { type: Instructions.EAT_FOOD }
]);

const chef = new Thread("Chef", [
  { type: Instructions.COOK_DISH },
  { type: Instructions.SIGNAL_FOOD }
]);
  • Engine - Instruction execution logic
  • Thread - Thread model with program counter
  • Scenarios - How instructions are used in scenarios