targets/Linux/plc_Linux_main.c
changeset 239 112b4bc523b3
parent 236 a32817e81f5e
child 245 60a221d72152
--- a/targets/Linux/plc_Linux_main.c	Thu Sep 04 16:07:14 2008 +0200
+++ b/targets/Linux/plc_Linux_main.c	Fri Sep 05 16:25:57 2008 +0200
@@ -61,6 +61,12 @@
   exit(0);
 }
 
+
+static int __debug_tick;
+
+static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 int startPLC(int argc,char **argv)
 {
     struct sigevent sigev;
@@ -73,6 +79,8 @@
     sigev.sigev_notify_attributes = NULL;
     sigev.sigev_notify_function = PLC_timer_notify;
 
+    pthread_mutex_lock(&wait_mutex);
+
     timer_create (CLOCK_REALTIME, &sigev, &PLC_timer);
     if(  __init(argc,argv) == 0 ){
         PLC_SetTimer(Ttick,Ttick);
@@ -86,18 +94,14 @@
     return 0;
 }
 
-static int __debug_tick;
+int TryEnterDebugSection(void)
+{
+    return pthread_mutex_trylock(&debug_mutex) == 0;
+}
 
-static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER;
-
-void AbortDebug()
+void LeaveDebugSection(void)
 {
-    /* Eventually unlock debugger thread*/
-    __debug_tick = -1;
-    //pthread_mutex_lock(&wait_mutex);
-    pthread_cond_broadcast(&wait_cond);
-    //pthread_mutex_unlock(&wait_mutex);
+    pthread_mutex_unlock(&debug_mutex);
 }
 
 int stopPLC()
@@ -106,7 +110,8 @@
     PLC_SetTimer(0,0);
     timer_delete (PLC_timer);
     __cleanup();
-    AbortDebug();
+    __debug_tick = -1;
+    pthread_mutex_unlock(&wait_mutex);
 }
 
 extern int __tick;
@@ -115,8 +120,6 @@
 {
     /* Wait signal from PLC thread */
     pthread_mutex_lock(&wait_mutex);
-    pthread_cond_wait(&wait_cond, &wait_mutex);
-    pthread_mutex_unlock(&wait_mutex);
     return __debug_tick;
 }
  
@@ -124,9 +127,23 @@
  * This is supposed to unlock debugger thread in WaitDebugData*/
 void InitiateDebugTransfer()
 {
-    /* signal debugger thread to continue*/
+    /* Leave debugger section */
+    pthread_mutex_unlock(&debug_mutex);
+    /* remember tick */
     __debug_tick = __tick;
-    //pthread_mutex_lock(&wait_mutex);
-    pthread_cond_broadcast(&wait_cond);
-    //pthread_mutex_unlock(&wait_mutex);
+    /* signal debugger thread it can read data */
+    pthread_mutex_unlock(&wait_mutex);
 }
+
+void suspendDebug()
+{
+    /* Prevent PLC to enter debug code */
+    pthread_mutex_lock(&debug_mutex);
+}
+
+void resumeDebug()
+{
+    /* Let PLC enter debug code */
+    pthread_mutex_unlock(&debug_mutex);
+}
+