From 294d815e27f3fe79df463b5504ca9099667574d8 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 17 Nov 2010 01:51:15 -0500 Subject: Fix race when one worker thread errors out. --- libdimension/raytrace.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/libdimension/raytrace.c b/libdimension/raytrace.c index df3d973..74f4557 100644 --- a/libdimension/raytrace.c +++ b/libdimension/raytrace.c @@ -122,14 +122,23 @@ dmnsn_raytrace_scene_thread(void *ptr) } } + /* Join the rest of the threads after detecting an error, so we don't + free anything out from under them */ + bool join_failed = false; + bool error_seen = false; for (int i = 0; i < nthreads; ++i) { void *ptr = NULL; - if (pthread_join(threads[i], &ptr)) { + if (pthread_join(threads[i], &ptr) != 0) + join_failed = true; + if (!ptr) + error_seen = true; + } + if (join_failed) { dmnsn_error(DMNSN_SEVERITY_MEDIUM, "Couldn't join worker thread in raytrace engine."); - } - if (!ptr) - dmnsn_error(DMNSN_SEVERITY_HIGH, "Error occurred in worker thread."); + } + if (error_seen) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Error occurred in worker thread."); } dmnsn_complete_timer(payload->scene->render_timer); -- cgit v1.2.3