# HG changeset patch
# User Edouard Tisserant
# Date 1527065637 -7200
# Node ID 64268e87613e8a331b577221f19a0f8c336b2b01
# Parent  bb9c28bd204fae125a469c94ba81f3c834dea520
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

diff -r bb9c28bd204f -r 64268e87613e 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));
+            }
 		}
 	}
 }