# =============================================================================
# Makefile for Stanford CS149 Lecture 16 demos (fine-grained locking,
# lock-free programming)
# =============================================================================
#
# [What this builds]
#   02_deadlock_real  a real deadlock (time-out detector) and two fixes
#   03_linked_list    no lock / global lock / hand-over-hand / lazy list
#   04_spsc_queue     slide 51's single-producer single-consumer queue
#   06_treiber        lock-free stack, untagged vs tagged, vs a mutex
#   Demos 01 and 05 are Python (standard library only).
#
# [How to use]
#   make            # build
#   make run        # run all demos in lecture order (~15 s)
#   make selftest   # self-checks of lockworld.py and lockfree.py
#   make clean
# -----------------------------------------------------------------------------

CXX      ?= c++
CXXFLAGS := -std=c++20 -O2 -g -Wall -Wextra -pthread
PYTHON   ?= python3
HEADERS  := ../lecture01/cs149_timer.h ../lecture01/machine_info.h
BINS     := 02_deadlock_real 03_linked_list 04_spsc_queue 06_treiber

.PHONY: all run selftest clean
all: $(BINS)

$(BINS): %: %.cpp $(HEADERS)
	$(CXX) $(CXXFLAGS) -o $@ $<

run: all
	@echo; $(PYTHON) 01_deadlock.py
	@echo; ./02_deadlock_real
	@echo; ./03_linked_list
	@echo; ./04_spsc_queue
	@echo; $(PYTHON) 05_aba.py
	@echo; ./06_treiber

selftest:
	$(PYTHON) lockworld.py
	$(PYTHON) lockfree.py

clean:
	rm -f $(BINS)
	rm -rf *.dSYM __pycache__
