diff --git a/workspace/all/common/api.c b/workspace/all/common/api.c index 19f3f5439..35be15bc4 100644 --- a/workspace/all/common/api.c +++ b/workspace/all/common/api.c @@ -3858,6 +3858,22 @@ int PAD_tappedSelect(uint32_t now) return PAD_tappedBtn(BTN_SELECT, now); } +/////////////////////////////// + +// the RLIMIT_STACK default (8MB per thread) wastes address space +// workaround for libretro/gpsp#248 and NextUI#814 +#define THREAD_STACK_SIZE (1024 * 1024) + +static int spawn_thread(pthread_t *pt, void *(*fn)(void *), void *arg) +{ + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE); + int rc = pthread_create(pt, &attr, fn, arg); + pthread_attr_destroy(&attr); + return rc; +} + /////////////////////////////// static struct VIB_Context { @@ -3891,7 +3907,7 @@ static void *VIB_thread(void *arg) void VIB_init(void) { vib.queued_strength = vib.strength = 0; - pthread_create(&vib.pt, NULL, &VIB_thread, NULL); + spawn_thread(&vib.pt, &VIB_thread, NULL); vib.initialized = 1; } void VIB_quit(void) @@ -4026,7 +4042,7 @@ void PWR_init(void) PWR_updateBatteryStatus(); - pthread_create(&pwr.battery_pt, NULL, &PWR_monitorBattery, &pwr); + spawn_thread(&pwr.battery_pt, &PWR_monitorBattery, &pwr); LOG_info("PWR_init complete\n"); } void PWR_quit(void) diff --git a/workspace/all/minarch/minarch.c b/workspace/all/minarch/minarch.c index b3e156eda..fd6afb977 100644 --- a/workspace/all/minarch/minarch.c +++ b/workspace/all/minarch/minarch.c @@ -1,5 +1,8 @@ #include #include +#ifdef __GLIBC__ +#include +#endif #include @@ -139,6 +142,14 @@ int main(int argc , char* argv[]) { if(argc < 2) return EXIT_FAILURE; + // keep malloc arenas and thread stacks out of the address range dynarec cores + // need for their JIT cache (must happen before any thread is created) + // workaround for libretro/gpsp#248 and NextUI#814 +#ifdef __GLIBC__ + mallopt(M_ARENA_MAX, 2); // glibc extension +#endif + SDL_SetHint(SDL_HINT_THREAD_STACK_SIZE, "1048576"); + PWR_setCPUSpeed(CPU_SPEED_PERFORMANCE); // start in performance mode for fast loading PWR_pinToCores(CPU_CORE_PERFORMANCE); // thread affinity