Files
RetroArch/samples/menu/dirwalk/Makefile
LibretroAdmin 5ba7f8c8cb task_queue: a threaded runner without locks
Where the atomics have compare-and-swap and pointer operations, the
threaded runner takes no lock and waits on no condition variable.

Every task is on a live list from its push until it is retired: any
thread links it at the head, only the checking thread unlinks it, and
find(), retrieve() and the lookups behind wake, cancel and reset walk
it from any thread inside a read epoch. A retired task is cleaned up
and freed once no finder can still hold it, at once when none is
inside, since finders read task->state.

Workers take ready tasks from two bounded MPMC rings: the plain one,
which only the one worker takes from, and the parallel one, which
every worker does; what overflows waits behind them in order. A task
with a 'when' to wait for goes to the timer keeper, whichever idle
worker holds the token, which sleeps until the earliest is due and
hands the token on before it runs anything. Main-thread tasks and
finished ones reach the checking thread through lock-free stacks.
Workers, task_queue_wait() and deinit's bounded wait for the workers
park on eventcounts. Each gather with work in flight and each pass's
end advance one counter that both read, so a handler still sees what
the frame loop wrote before a check, as the frame count a load waits
on, and has read it before the loop writes again.

The volatile and PS2 backends keep the locked runner, as does GCD.
task_queue.c now uses retro_eventcount.c, which the sample builds
compiling it with threads now link.

task_queue_parallel_test floods both rings past what they hold with
every worker held; task_queue_switch_test holds a waker in
retro_eventcount_notify() where the runner takes no lock.
2026-10-30 13:59:17 +01:00

107 lines
2.6 KiB
Makefile

include ../../sample_objdir.mk
compiler := gcc
extra_flags :=
EXE_EXT :=
TARGET := dirwalk_budget_test
HAVE_THREADS := 1
ifeq ($(platform),)
platform = unix
ifeq ($(shell uname -a),)
platform = win
else ifneq ($(findstring MINGW,$(shell uname -a)),)
platform = win
else ifneq ($(findstring Darwin,$(shell uname -a)),)
platform = osx
endif
endif
ifeq ($(build),)
build = release
endif
ifeq ($(DEBUG), 1)
build = debug
endif
ifeq (release,$(build))
CFLAGS += -O2
LDFLAGS += -O2
endif
ifeq (debug,$(build))
CFLAGS += -O0 -g1
LDFLAGS += -O0 -g1
endif
ifneq ($(SANITIZER),)
CFLAGS := -fsanitize=$(SANITIZER) $(CFLAGS)
LDFLAGS := -fsanitize=$(SANITIZER) $(LDFLAGS)
endif
ifeq ($(platform), unix)
else ifeq ($(platform), osx)
compiler := $(CC)
else
EXE_EXT = .exe
endif
CORE_DIR = ../../..
LIBRETRO_COMM_DIR = $(CORE_DIR)/libretro-common
INCDIRS := -I$(LIBRETRO_COMM_DIR)/include -I$(CORE_DIR)
CC := $(compiler)
flags += -std=c99
SOURCES_C := \
$(CORE_DIR)/samples/menu/dirwalk/dirwalk_budget_test.c \
$(CORE_DIR)/menu/menu_dirwalk.c \
$(CORE_DIR)/tasks/task_nbio_slice.c \
$(LIBRETRO_COMM_DIR)/queues/task_queue.c \
$(LIBRETRO_COMM_DIR)/lists/dir_list.c \
$(LIBRETRO_COMM_DIR)/lists/string_list.c \
$(LIBRETRO_COMM_DIR)/file/file_path.c \
$(LIBRETRO_COMM_DIR)/file/file_path_io.c \
$(LIBRETRO_COMM_DIR)/file/retro_dirent.c \
$(LIBRETRO_COMM_DIR)/streams/file_stream.c \
$(LIBRETRO_COMM_DIR)/string/rstrtod.c \
$(LIBRETRO_COMM_DIR)/vfs/vfs_implementation.c \
$(LIBRETRO_COMM_DIR)/compat/compat_strl.c \
$(LIBRETRO_COMM_DIR)/compat/compat_strldup.c \
$(LIBRETRO_COMM_DIR)/compat/compat_posix_string.c \
$(LIBRETRO_COMM_DIR)/compat/fopen_utf8.c \
$(LIBRETRO_COMM_DIR)/string/stdstring.c \
$(LIBRETRO_COMM_DIR)/encodings/encoding_utf.c \
$(LIBRETRO_COMM_DIR)/time/rtime.c \
$(LIBRETRO_COMM_DIR)/rthreads/rthreads.c \
$(LIBRETRO_COMM_DIR)/rthreads/retro_eventcount.c
CFLAGS += -Wall $(INCDIRS)
CFLAGS += -DHAVE_THREADS -D_GNU_SOURCE
# Tiny sort-machinery constants: the oracle drives run sorting, the
# resumable merge, and sort-only deferral deterministically.
CFLAGS += -DMENU_DIRWALK_SORT_RUN=32 -DMENU_DIRWALK_SORT_BATCH=8 -DMENU_DIRWALK_SORT_SYNC_MAX=64
LDFLAGS += -lm -lpthread
OBJS := $(call sample_objs,$(SOURCES_C:.c=.o))
all: $(TARGET)
%.o: %.c
$(CC) $(INCDIRS) $(CFLAGS) $(flags) -c -o$@ $<
$(SAMPLE_OBJDIR)/%.o: $$(call sample_src,$$*).c
@mkdir -p $(@D)
$(CC) $(INCDIRS) $(CFLAGS) $(flags) -c -o$@ $<
$(TARGET): $(OBJS)
$(CC) -o$@$(EXE_EXT) $(OBJS) $(LDFLAGS)
clean:
rm -rf $(SAMPLE_OBJDIR)
rm -f $(OBJS)
rm -f $(TARGET)
.PHONY: clean all