Skip to content

Commit

Permalink
main: Fix deadlocks on _threads exit
Browse files Browse the repository at this point in the history
See issue (antonblanchard#33).

Since async pthread_cancel on threads that do hold locks is wrong, we
instead create a separate process that holds the threads. On exit, the
holding process just exits, and the parent wait()'s for it, and cleans
up the testcase.

Signed-off-by: Pedro Falcato <[email protected]>
  • Loading branch information
heatd committed Jun 9, 2023
1 parent 82a5e0d commit 7cbcc17
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ static bool use_affinity = true;
static pthread_t threads[2*MAX_TASKS];
static int nr_threads;

static pid_t thread_controller = 0;

void new_task(void *(func)(void *), void *arg)
{
pthread_create(&threads[nr_threads++], NULL, func, arg);
Expand Down Expand Up @@ -313,6 +315,19 @@ int main(int argc, char *argv[])

testcase_prepare(opt_tasks);

#ifdef THREADS
thread_controller = fork();

if (thread_controller < 0) {
perror("fork");
exit(1);
} else if (thread_controller > 0) {
pid_t wpid = wait(NULL);
assert(wpid == thread_controller);
goto out;
}
#endif

#if HAVE_HWLOC
hwloc_topology_init(&topology);
hwloc_topology_load(topology);
Expand Down Expand Up @@ -437,7 +452,11 @@ int main(int argc, char *argv[])
if (opt_iterations &&
(iterations > (opt_iterations + WARMUP_ITERATIONS))) {
printf("average:%llu\n", total / opt_iterations);
#ifdef THREADS
exit(0);
#else
break;
#endif
}
}

Expand All @@ -450,6 +469,9 @@ int main(int argc, char *argv[])
}
#endif
free(args);
#ifdef THREADS
out:
#endif

testcase_cleanup();
exit(0);
Expand Down

0 comments on commit 7cbcc17

Please sign in to comment.