59 signal(SIGINT, catch_signal); |
59 signal(SIGINT, catch_signal); |
60 printf("Got Signal %d\n",sig); |
60 printf("Got Signal %d\n",sig); |
61 exit(0); |
61 exit(0); |
62 } |
62 } |
63 |
63 |
|
64 |
|
65 static int __debug_tick; |
|
66 |
|
67 static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
|
68 static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
|
69 |
64 int startPLC(int argc,char **argv) |
70 int startPLC(int argc,char **argv) |
65 { |
71 { |
66 struct sigevent sigev; |
72 struct sigevent sigev; |
67 /* Translate PLC's microseconds to Ttick nanoseconds */ |
73 /* Translate PLC's microseconds to Ttick nanoseconds */ |
68 Ttick = 1000000 * maxval(common_ticktime__,1); |
74 Ttick = 1000000 * maxval(common_ticktime__,1); |
70 memset (&sigev, 0, sizeof (struct sigevent)); |
76 memset (&sigev, 0, sizeof (struct sigevent)); |
71 sigev.sigev_value.sival_int = 0; |
77 sigev.sigev_value.sival_int = 0; |
72 sigev.sigev_notify = SIGEV_THREAD; |
78 sigev.sigev_notify = SIGEV_THREAD; |
73 sigev.sigev_notify_attributes = NULL; |
79 sigev.sigev_notify_attributes = NULL; |
74 sigev.sigev_notify_function = PLC_timer_notify; |
80 sigev.sigev_notify_function = PLC_timer_notify; |
|
81 |
|
82 pthread_mutex_lock(&wait_mutex); |
75 |
83 |
76 timer_create (CLOCK_REALTIME, &sigev, &PLC_timer); |
84 timer_create (CLOCK_REALTIME, &sigev, &PLC_timer); |
77 if( __init(argc,argv) == 0 ){ |
85 if( __init(argc,argv) == 0 ){ |
78 PLC_SetTimer(Ttick,Ttick); |
86 PLC_SetTimer(Ttick,Ttick); |
79 |
87 |
84 return 1; |
92 return 1; |
85 } |
93 } |
86 return 0; |
94 return 0; |
87 } |
95 } |
88 |
96 |
89 static int __debug_tick; |
97 int TryEnterDebugSection(void) |
|
98 { |
|
99 return pthread_mutex_trylock(&debug_mutex) == 0; |
|
100 } |
90 |
101 |
91 static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
102 void LeaveDebugSection(void) |
92 static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER; |
|
93 |
|
94 void AbortDebug() |
|
95 { |
103 { |
96 /* Eventually unlock debugger thread*/ |
104 pthread_mutex_unlock(&debug_mutex); |
97 __debug_tick = -1; |
|
98 //pthread_mutex_lock(&wait_mutex); |
|
99 pthread_cond_broadcast(&wait_cond); |
|
100 //pthread_mutex_unlock(&wait_mutex); |
|
101 } |
105 } |
102 |
106 |
103 int stopPLC() |
107 int stopPLC() |
104 { |
108 { |
105 /* Stop the PLC */ |
109 /* Stop the PLC */ |
106 PLC_SetTimer(0,0); |
110 PLC_SetTimer(0,0); |
107 timer_delete (PLC_timer); |
111 timer_delete (PLC_timer); |
108 __cleanup(); |
112 __cleanup(); |
109 AbortDebug(); |
113 __debug_tick = -1; |
|
114 pthread_mutex_unlock(&wait_mutex); |
110 } |
115 } |
111 |
116 |
112 extern int __tick; |
117 extern int __tick; |
113 /* from plc_debugger.c */ |
118 /* from plc_debugger.c */ |
114 int WaitDebugData() |
119 int WaitDebugData() |
115 { |
120 { |
116 /* Wait signal from PLC thread */ |
121 /* Wait signal from PLC thread */ |
117 pthread_mutex_lock(&wait_mutex); |
122 pthread_mutex_lock(&wait_mutex); |
118 pthread_cond_wait(&wait_cond, &wait_mutex); |
|
119 pthread_mutex_unlock(&wait_mutex); |
|
120 return __debug_tick; |
123 return __debug_tick; |
121 } |
124 } |
122 |
125 |
123 /* Called by PLC thread when debug_publish finished |
126 /* Called by PLC thread when debug_publish finished |
124 * This is supposed to unlock debugger thread in WaitDebugData*/ |
127 * This is supposed to unlock debugger thread in WaitDebugData*/ |
125 void InitiateDebugTransfer() |
128 void InitiateDebugTransfer() |
126 { |
129 { |
127 /* signal debugger thread to continue*/ |
130 /* Leave debugger section */ |
|
131 pthread_mutex_unlock(&debug_mutex); |
|
132 /* remember tick */ |
128 __debug_tick = __tick; |
133 __debug_tick = __tick; |
129 //pthread_mutex_lock(&wait_mutex); |
134 /* signal debugger thread it can read data */ |
130 pthread_cond_broadcast(&wait_cond); |
135 pthread_mutex_unlock(&wait_mutex); |
131 //pthread_mutex_unlock(&wait_mutex); |
|
132 } |
136 } |
|
137 |
|
138 void suspendDebug() |
|
139 { |
|
140 /* Prevent PLC to enter debug code */ |
|
141 pthread_mutex_lock(&debug_mutex); |
|
142 } |
|
143 |
|
144 void resumeDebug() |
|
145 { |
|
146 /* Let PLC enter debug code */ |
|
147 pthread_mutex_unlock(&debug_mutex); |
|
148 } |
|
149 |