From 11bcdeed598efba7158a2aaa6bb6fe90b69a0d3f Mon Sep 17 00:00:00 2001 From: "Hannes T." Date: Tue, 6 Feb 2024 16:55:37 +0100 Subject: [PATCH 1/2] revert: fix for #1 ee5efe The fix for #1 relies on comparing pthread_t, which according to pthread_equal(3) is not allowed. --- sensors_plugin.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sensors_plugin.c b/sensors_plugin.c index 1a631e2..3235df5 100644 --- a/sensors_plugin.c +++ b/sensors_plugin.c @@ -248,9 +248,7 @@ SCOREP_Metric_Plugin_MetricProperties * scorep_plugin_libsensors_get_event_info( static void scorep_plugin_libsensors_fini() { counter_enabled=0; - if ( thread != 0 ) { - pthread_join(thread, NULL); - } + pthread_join(thread, NULL); sensors_cleanup(); } From 8d001d76b28d1207e90c8395ac0d92762ebedc75 Mon Sep 17 00:00:00 2001 From: "Hannes T." Date: Tue, 6 Feb 2024 16:38:32 +0100 Subject: [PATCH 2/2] track if measurement thread is active This patch adds a variable thread_active which is 1 while the measurement thread is active, and is 0 otherwise. This state is tracked manually. This is required to avoid the attempt of joining a non-created thread. According to pthread_equal(3) it is not acceptable to directly compare two pthread_t, as they should be considered opaque. Hence, external tracking whether a thread is active is required. (For this reason, the hacky solution of comparing to 0 is not used, also it would probably work.) This patch further adds error checking for the final thread join. On error, errno is printed with a debug message. --- sensors_plugin.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/sensors_plugin.c b/sensors_plugin.c index 3235df5..c71d0ad 100644 --- a/sensors_plugin.c +++ b/sensors_plugin.c @@ -62,6 +62,7 @@ static pthread_mutex_t read_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_t thread; static int thread_finished; +static int thread_active; /* used to get the timer from Score-P */ static void scorep_plugin_libsensors_set_timer( uint64_t (*timer)(void)){ @@ -149,6 +150,7 @@ static int32_t scorep_plugin_libsensors_init() counter_enabled=1; thread=0; thread_finished=1; + thread_active=0; return 0; } @@ -248,7 +250,15 @@ SCOREP_Metric_Plugin_MetricProperties * scorep_plugin_libsensors_get_event_info( static void scorep_plugin_libsensors_fini() { counter_enabled=0; - pthread_join(thread, NULL); + if (1 == thread_active) { + int ret = pthread_join(thread, NULL); + if (0 != ret) { + fprintf(stderr, "failed to clean up measurement thread (%d): %s\n", + ret, strerror(errno)); + } + thread_active=0; + } + pthread_mutex_destroy(&read_mutex); sensors_cleanup(); } @@ -304,11 +314,13 @@ static int32_t scorep_plugin_libsensors_add_counter(char * event_name) return -ENOMEM; } // are all events added? - if (added_sensor_counters == number_sensors) + if (added_sensor_counters == number_sensors) { if (pthread_create(&thread, NULL, &scorep_plugin_libsensors_thread_report, NULL)){ fprintf(stderr, "Score-P Sensors Plugin: Unable to start measurement thread.\n"); return -ECHILD; } + thread_active=1; + } return added_sensor_counters-1; }