Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
authorEdouard Tisserant
Tue, 14 Feb 2023 12:10:35 +0100
changeset 3725 0043e2b9dbec
parent 3709 7fbcc7b741b7
child 3726 516779f11803
Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
targets/Linux/plc_Linux_main.c
--- a/targets/Linux/plc_Linux_main.c	Fri Dec 16 13:46:51 2022 +0100
+++ b/targets/Linux/plc_Linux_main.c	Tue Feb 14 12:10:35 2023 +0100
@@ -7,6 +7,7 @@
 #include <time.h>
 #include <signal.h>
 #include <stdlib.h>
+#include <errno.h>
 #include <pthread.h>
 #include <locale.h>
 #include <semaphore.h>
@@ -237,6 +238,7 @@
 }
 
 struct RT_to_nRT_signal_s {
+    int used;
     pthread_cond_t WakeCond;
     pthread_mutex_t WakeCondLock;
 };
@@ -257,6 +259,7 @@
     if(!sig) 
     	_LogAndReturnNull("Failed allocating memory for RT_to_nRT signal");
 
+    sig->used = 1;
     pthread_cond_init(&sig->WakeCond, NULL);
     pthread_mutex_init(&sig->WakeCondLock, NULL);
 
@@ -266,10 +269,10 @@
 void delete_RT_to_nRT_signal(void* handle){
     RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
 
-    pthread_cond_destroy(&sig->WakeCond);
-    pthread_mutex_destroy(&sig->WakeCondLock);
-
-    free(sig);
+    pthread_mutex_lock(&sig->WakeCondLock);
+    sig->used = 0;
+    pthread_cond_signal(&sig->WakeCond);
+    pthread_mutex_unlock(&sig->WakeCondLock);
 }
 
 int wait_RT_to_nRT_signal(void* handle){
@@ -277,7 +280,14 @@
     RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
     pthread_mutex_lock(&sig->WakeCondLock);
     ret = pthread_cond_wait(&sig->WakeCond, &sig->WakeCondLock);
+    if(!sig->used) ret = -EINVAL;
     pthread_mutex_unlock(&sig->WakeCondLock);
+
+    if(!sig->used){
+        pthread_cond_destroy(&sig->WakeCond);
+        pthread_mutex_destroy(&sig->WakeCondLock);
+        free(sig);
+    }
     return ret;
 }