Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
authorEdouard Tisserant
Wed, 23 May 2018 10:53:57 +0200
changeset 2011 64268e87613e
parent 2010 bb9c28bd204f
child 2017 c76d4be6f438
child 2018 983c482f8e42
Make Modbus mutexes non blocking for PLC. If data related to some modbus request is alredy locked when PLC wants to access it, then data isn't copied to/from PLC
modbus/mb_runtime.c
--- a/modbus/mb_runtime.c	Thu May 17 09:33:58 2018 +0200
+++ b/modbus/mb_runtime.c	Wed May 23 10:53:57 2018 +0200
@@ -492,12 +492,13 @@
 	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
 		/*just do the output requests */
 		if (client_requests[index].req_type == req_output){
-			pthread_mutex_lock(&(client_requests[index].coms_buf_mutex));
-			// copy from plcv_buffer to coms_buffer
-			memcpy((void *)client_requests[index].coms_buffer /* destination */,
-			       (void *)client_requests[index].plcv_buffer /* source */,
-			       REQ_BUF_SIZE * sizeof(u16) /* size in bytes */);
-			pthread_mutex_unlock(&(client_requests[index].coms_buf_mutex));
+			if(pthread_mutex_trylock(&(client_requests[index].coms_buf_mutex)) == 0){
+                // copy from plcv_buffer to coms_buffer
+                memcpy((void *)client_requests[index].coms_buffer /* destination */,
+                       (void *)client_requests[index].plcv_buffer /* source */,
+                       REQ_BUF_SIZE * sizeof(u16) /* size in bytes */);
+                pthread_mutex_unlock(&(client_requests[index].coms_buf_mutex));
+            }
 		}
 	}
 }
@@ -512,12 +513,13 @@
 	for (index=0; index < NUMBER_OF_CLIENT_REQTS; index ++){
 		/*just do the input requests */
 		if (client_requests[index].req_type == req_input){
-			pthread_mutex_lock(&(client_requests[index].coms_buf_mutex));
-			// copy from coms_buffer to plcv_buffer
-			memcpy((void *)client_requests[index].plcv_buffer /* destination */,
-			       (void *)client_requests[index].coms_buffer /* source */,
-			       REQ_BUF_SIZE * sizeof(u16) /* size in bytes */);
-			pthread_mutex_unlock(&(client_requests[index].coms_buf_mutex));
+			if(pthread_mutex_trylock(&(client_requests[index].coms_buf_mutex)) == 0){
+                // copy from coms_buffer to plcv_buffer
+                memcpy((void *)client_requests[index].plcv_buffer /* destination */,
+                       (void *)client_requests[index].coms_buffer /* source */,
+                       REQ_BUF_SIZE * sizeof(u16) /* size in bytes */);
+                pthread_mutex_unlock(&(client_requests[index].coms_buf_mutex));
+            }
 		}
 	}
 }