From f7da5b7dde8b9c342c805edd65e66febf705ed48 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 25 Feb 2023 16:17:03 +0100 Subject: limit thread count to core count --- src/ppusim/sim.c | 5 ++++- src/ppusim/work.c | 20 +++++++++++--------- src/ppusim/work.h | 5 ++++- 3 files changed, 19 insertions(+), 11 deletions(-) (limited to 'src/ppusim') diff --git a/src/ppusim/sim.c b/src/ppusim/sim.c index 4226a9b..2816b31 100644 --- a/src/ppusim/sim.c +++ b/src/ppusim/sim.c @@ -1,6 +1,5 @@ #include #include -#include #include #include "main.h" @@ -17,11 +16,15 @@ void hh_ppu_init() { g_hh_window = SDL_CreateWindow("ppusim", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, HH_PPU_SCREEN_WIDTH * HH_PPUSIM_UPSCALE_FACTOR, HH_PPU_SCREEN_HEIGHT * HH_PPUSIM_UPSCALE_FACTOR, SDL_WINDOW_SHOWN); g_hh_renderer = SDL_CreateRenderer(g_hh_window, -1, SDL_RENDERER_ACCELERATED); + g_hh_ppusim_core_count = SDL_GetCPUCount(); + g_hh_ppusim_threads = malloc(sizeof(pthread_t) * g_hh_ppusim_core_count); + g_hh_ppusim_vram = malloc(sizeof(hh_ppu_data_t) * 0xffff); memset(g_hh_ppusim_vram, 0x0000, 0xffff); } void hh_ppu_deinit() { + free(g_hh_ppusim_threads); free(g_hh_ppusim_vram); SDL_DestroyRenderer(g_hh_renderer); diff --git a/src/ppusim/work.c b/src/ppusim/work.c index 2073907..fcb80ca 100644 --- a/src/ppusim/work.c +++ b/src/ppusim/work.c @@ -7,21 +7,23 @@ #include "ppu/consts.h" #include "ppusim/pixel.h" -pthread_t g_hh_ppusim_scanline_threads[HH_PPU_SCREEN_HEIGHT]; +pthread_t* g_hh_ppusim_threads; +unsigned g_hh_ppusim_core_count; hh_s_ppusim_screen g_hh_ppusim_screen; -void* hh_ppusim_draw_scanline(void* scanline_context) { - unsigned scanline = (unsigned long) scanline_context; - for (unsigned x = 0; x < HH_PPU_SCREEN_WIDTH; x++) - hh_ppusim_pixel(g_hh_ppusim_screen[scanline][x], x, scanline); +void* hh_ppusim_draw_thread(void* arg) { + unsigned core = (unsigned long) arg; + for (unsigned y = core; y < HH_PPU_SCREEN_HEIGHT; y += g_hh_ppusim_core_count) + for (unsigned x = 0; x < HH_PPU_SCREEN_WIDTH; x++) + hh_ppusim_pixel(g_hh_ppusim_screen[y][x], x, y); return NULL; } void hh_ppusim_draw_frame(SDL_Renderer* renderer) { - for (unsigned y = 0; y < HH_PPU_SCREEN_HEIGHT; y++) - pthread_create(&g_hh_ppusim_scanline_threads[y], NULL, hh_ppusim_draw_scanline, (void*)(unsigned long)y); - for (unsigned y = 0; y < HH_PPU_SCREEN_HEIGHT; y++) - pthread_join(g_hh_ppusim_scanline_threads[y], NULL); + for (unsigned core = 0; core < g_hh_ppusim_core_count; core++) + pthread_create(&g_hh_ppusim_threads[core], NULL, hh_ppusim_draw_thread, (void*)(unsigned long)core); + for (unsigned core = 0; core < g_hh_ppusim_core_count; core++) + pthread_join(g_hh_ppusim_threads[core], NULL); for (unsigned x = 0; x < HH_PPU_SCREEN_WIDTH; x++) { for (unsigned y = 0; y < HH_PPU_SCREEN_HEIGHT; y++) { diff --git a/src/ppusim/work.h b/src/ppusim/work.h index 1171a22..c1a5148 100644 --- a/src/ppusim/work.h +++ b/src/ppusim/work.h @@ -4,11 +4,14 @@ #include "ppu/consts.h" +extern unsigned g_hh_ppusim_core_count; +extern pthread_t* g_hh_ppusim_threads; + typedef uint8_t hh_s_ppusim_color[3]; typedef hh_s_ppusim_color hh_s_ppusim_scanline[HH_PPU_SCREEN_WIDTH]; typedef hh_s_ppusim_scanline hh_s_ppusim_screen[HH_PPU_SCREEN_HEIGHT]; -void* hh_ppusim_draw_scanline(void*); +void* hh_ppusim_draw_thread(void*); void hh_ppusim_draw_frame(SDL_Renderer*); -- cgit v1.2.3