limit reclaim from free to half the segments to prevent a pure freeing thread to reclaim too many segments

This commit is contained in:
daanx 2024-03-03 17:38:50 -08:00
parent 40517890d4
commit 5fe83bf327
4 changed files with 36 additions and 14 deletions

View file

@ -37,11 +37,12 @@ static int ITER = 50; // N full iterations destructing and re-creating a
// static int THREADS = 8; // more repeatable if THREADS <= #processors
// static int SCALE = 100; // scaling factor
#define STRESS // undefine for leak test
#define STRESS // undefine for leak test
static bool allow_large_objects = true; // allow very large objects? (set to `true` if SCALE>100)
static size_t use_one_size = 0; // use single object size of `N * sizeof(uintptr_t)`?
static bool main_participates = false; // main thread participates as a worker too
// #define USE_STD_MALLOC
#ifdef USE_STD_MALLOC
@ -196,10 +197,13 @@ static void test_stress(void) {
free_items(p);
}
}
// mi_collect(false);
#if !defined(NDEBUG) || defined(MI_TSAN)
#ifndef NDEBUG
//mi_collect(false);
//mi_debug_show_arenas();
#endif
#if !defined(NDEBUG) || defined(MI_TSAN)
if ((n + 1) % 10 == 0) { printf("- iterations left: %3d\n", ITER - (n + 1)); }
#endif
#endif
}
}
@ -292,14 +296,15 @@ static void run_os_threads(size_t nthreads, void (*fun)(intptr_t)) {
thread_entry_fun = fun;
DWORD* tids = (DWORD*)custom_calloc(nthreads,sizeof(DWORD));
HANDLE* thandles = (HANDLE*)custom_calloc(nthreads,sizeof(HANDLE));
for (uintptr_t i = 1; i < nthreads; i++) {
const size_t start = (main_participates ? 1 : 0);
for (size_t i = start; i < nthreads; i++) {
thandles[i] = CreateThread(0, 8*1024, &thread_entry, (void*)(i), 0, &tids[i]);
}
fun(0); // run the main thread as well
for (size_t i = 1; i < nthreads; i++) {
if (main_participates) fun(0); // run the main thread as well
for (size_t i = start; i < nthreads; i++) {
WaitForSingleObject(thandles[i], INFINITE);
}
for (size_t i = 1; i < nthreads; i++) {
for (size_t i = start; i < nthreads; i++) {
CloseHandle(thandles[i]);
}
custom_free(tids);
@ -326,12 +331,13 @@ static void run_os_threads(size_t nthreads, void (*fun)(intptr_t)) {
thread_entry_fun = fun;
pthread_t* threads = (pthread_t*)custom_calloc(nthreads,sizeof(pthread_t));
memset(threads, 0, sizeof(pthread_t) * nthreads);
const size_t start = (main_participates ? 1 : 0);
//pthread_setconcurrency(nthreads);
for (size_t i = 1; i < nthreads; i++) {
for (size_t i = start; i < nthreads; i++) {
pthread_create(&threads[i], NULL, &thread_entry, (void*)i);
}
fun(0); // run the main thread as well
for (size_t i = 1; i < nthreads; i++) {
if (main_participates) fun(0); // run the main thread as well
for (size_t i = start; i < nthreads; i++) {
pthread_join(threads[i], NULL);
}
custom_free(threads);