BACnet : ensure that Init did happen before continuing with PLC startup.
authorEdouard Tisserant
Wed, 13 Jun 2018 08:54:13 +0200
changeset 2187 c6321473fac1
parent 2020 6dddf3070806
child 2188 7f59aa398669
BACnet : ensure that Init did happen before continuing with PLC startup.
bacnet/runtime/server.c
--- a/bacnet/runtime/server.c	Fri Jun 08 13:28:00 2018 +0200
+++ b/bacnet/runtime/server.c	Wed Jun 13 08:54:13 2018 +0200
@@ -428,6 +428,10 @@
 }
 
 
+// This mutex blocks execution of __init_%(locstr)s() until initialization is done
+static int init_done = 0;
+static pthread_mutex_t init_done_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t init_done_cond = PTHREAD_COND_INITIALIZER;
 
 /** Main function of server demo. **/
 int bn_server_run(server_node_t *server_node) {
@@ -450,6 +454,12 @@
     bvlc_bdt_restore_local();
     /* Initiliaze the bacnet server 'device' */    
     Device_Init(server_node->device_name);
+
+    pthread_mutex_lock(&init_done_lock);
+    init_done = 1;
+    pthread_cond_signal(&init_done_cond);
+    pthread_mutex_unlock(&init_done_lock);
+
     /* Set the password (max 31 chars) for Device Communication Control request. */
     /* Default in the BACnet stack is hardcoded as "filister" */
     /* (char *) cast is to remove the cast. The function is incorrectly declared/defined in the BACnet stack! */
@@ -549,6 +559,7 @@
 int __init_%(locstr)s (int argc, char **argv){
 	int index;
 
+    init_done = 0;
 	/* init each local server */
 	/* NOTE: All server_nodes[].init_state are initialised to 0 in the code 
 	 *       generated by the BACnet plugin 
@@ -567,6 +578,13 @@
 			goto error_exit;
 		}
 	}
+
+    pthread_mutex_lock(&init_done_lock);
+    while (!init_done) {
+        pthread_cond_wait(&init_done_cond, &init_done_lock);
+    }
+    pthread_mutex_unlock(&init_done_lock);
+
 	server_node.init_state = 2; // we have created the node and thread
 
 	return 0;