From 0439399f05351abf6243df516243196399f0ae47 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Tue, 19 Dec 2023 12:29:11 +0100 Subject: [PATCH] Fix ovni_thread_isready() after ovni_thread_free() Until now, the value returned by ovni_thread_isready() was still non-zero when the thread stream was destroyed in ovni_thread_free() This was making it impossible to detect when a stream was destroyed. This change makes it return 0 after ovni_thread_free() by using a new finished flag. The flag keeps track on then the stream is destroyed, preventing double initialization of a free stream. --- CHANGELOG.md | 4 ++++ src/rt/ovni.c | 10 ++++++++++ test/emu/ovni/CMakeLists.txt | 1 + test/emu/ovni/thread-free-isready.c | 27 +++++++++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 test/emu/ovni/thread-free-isready.c diff --git a/CHANGELOG.md b/CHANGELOG.md index e845c81..b377a33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Calling `ovni_thread_isready()` after `ovni_thread_free()` now returns 0. + ## [1.5.0] - 2023-12-15 ### Added diff --git a/src/rt/ovni.c b/src/rt/ovni.c index 1691bcf..3c7bcdf 100644 --- a/src/rt/ovni.c +++ b/src/rt/ovni.c @@ -26,6 +26,7 @@ struct ovni_rthread { int streamfd; int ready; + int finished; /* The number of bytes filled with events */ size_t evlen; @@ -552,6 +553,9 @@ ovni_thread_init(pid_t tid) return; } + if (rthread.finished) + die("thread %d has finished, cannot init again", tid); + if (tid == 0) die("cannot use tid=%d", tid); @@ -578,6 +582,9 @@ ovni_thread_init(pid_t tid) void ovni_thread_free(void) { + if (rthread.finished) + die("thread already finished"); + if (!rthread.ready) die("thread not initialized"); @@ -597,6 +604,9 @@ ovni_thread_free(void) close(rthread.streamfd); rthread.streamfd = -1; + + rthread.ready = 0; + rthread.finished = 1; } int diff --git a/test/emu/ovni/CMakeLists.txt b/test/emu/ovni/CMakeLists.txt index 7f28418..a77ad4e 100644 --- a/test/emu/ovni/CMakeLists.txt +++ b/test/emu/ovni/CMakeLists.txt @@ -22,6 +22,7 @@ test_emu(require-bad-version.c SHOULD_FAIL REGEX "unsupported ovni model version test_emu(require-compat.c REGEX "loading trace in compatibility mode") test_emu(require-repeated.c) test_emu(thread-crash.c SHOULD_FAIL REGEX "incomplete stream") +test_emu(thread-free-isready.c) test_emu(flush-tmpdir.c MP DRIVER "flush-tmpdir.driver.sh") test_emu(tmpdir-metadata.c MP DRIVER "tmpdir-metadata.driver.sh") test_emu(dummy.c NAME "ovniver" DRIVER "ovniver.driver.sh") diff --git a/test/emu/ovni/thread-free-isready.c b/test/emu/ovni/thread-free-isready.c new file mode 100644 index 0000000..72bc012 --- /dev/null +++ b/test/emu/ovni/thread-free-isready.c @@ -0,0 +1,27 @@ +/* Copyright (c) 2023 Barcelona Supercomputing Center (BSC) + * SPDX-License-Identifier: GPL-3.0-or-later */ + +#include +#include "instr.h" + +/* Check the behavior of ovni_thread_isready(), which should only be 1 + * after ovni_thread_init() and before ovni_thread_free(). */ + +int +main(void) +{ + if (ovni_thread_isready()) + die("thread must not be ready before init"); + + instr_start(0, 1); + + if (!ovni_thread_isready()) + die("thread must be ready after init"); + + instr_end(); + + if (ovni_thread_isready()) + die("thread must not be ready after free"); + + return 0; +}