Skip to content
Lunna Boo edited this page Aug 8, 2026 · 8 revisions

Backlog

Parser

  1. Make it accept only 8 arguments, no more no less.
  2. Check all their types and return errors

Structs

Coder

It needs to have:

  • thread = pthread_t
  • id = int
  • is_compiling = int
  • compiling_score = int | NEEDS LOCK TO READ/WRITE
  • last_compile = long | NEEDS LOCK TO READ/WRITE
  • r_dongle = *pthread_mutex_t
  • l_dongle = *pthread_mutex_t
  • dead_lock = *pthread_mutex_t
  • compile_lock = *pthread_mutex_t
  • print_lock = *pthread_mutex_t
  • sim = *t_simulation

Simulation

  • thread = pthread_t
  • nb_of_coders = int
  • start_time = long
  • dead_flag = int
  • dead_lock = pthread_mutex_t
  • compile_lock = pthread_mutex_t
  • print_lock = pthread_mutex_t
  • time_to_burn = long
  • time_to_compile = long
  • time_to_debug = long
  • time_to_refac = long
  • compiling_quota = int
  • dongle_cooldown = long
  • scheduler = char *
  • *coders = t_coders

Main Thread

  • Create Coder and Simulation Structs

  • Initialize both of them

  • Create both coder_routine thread and monitor thread (passing the structs as arguments)

  • coder_routine argument will be simulation.coders[i] (gotta get the right coder struct to it)

  • monitor argument will be the simulation struct itself

  • pthread_join both threads.

  • destroy all mutexex.

  • free all allocated data

Coder Routine

Must:

  • Cast arg to t_coder * / t_coder *coder = (t_coder *)arg;

  • Check if simulation dead-flag is 1 / if it is, exit

  • Print each time a coder takes a dongle (timestamp id has taken a dongle!)

  • Compile and print / Lock dongles, update last_compile, lock print mutex, print, unlock print, and unlock dongles

  • Debug, lock print mutex, print and unlock

  • Refactor, lock print mutex, print and unlock

  • Serialize logging (never have 2 outputs on a single line)

Monitor Thread

Must:

  • Cast arg to t_simulation * / t_simulation *simulation = (t_simulation *)arg;
  • Loop every 1ms
  • Detect burnout and stop the simulation. (read last_compile, using mutex)
  • If burnout detected, lock print mutex, print death log, set dead_flag = 1 and unlock

It will detect burnout by checking

Communication between Threads

  1. Routine Thread locks mutex, updates last_compile = get_time(), unlocks mutex.
  2. Monitor Thread locks same mutex, reads last_compile, calculates elapsed time.
  3. Monitor Thread detects death, locks mutex, prints log, sets dead_flag = 1, unlocks mutex.
  4. Routine Thread (at the top of its loop, or after waking from sleep) checks: if (dead_flag == 1) return (NULL); and exits without printing anything.

Rules

  • Always pass everything by reference. Most importantly structs.
  • When accessing arguments inside routines always cast the argument from void * back to the desired type.

Clone this wiki locally