7 #include <time.h> |
7 #include <time.h> |
8 #include <signal.h> |
8 #include <signal.h> |
9 #include <stdlib.h> |
9 #include <stdlib.h> |
10 #include <pthread.h> |
10 #include <pthread.h> |
11 #include <locale.h> |
11 #include <locale.h> |
|
12 #include <semaphore.h> |
12 |
13 |
13 extern unsigned long long common_ticktime__; |
14 extern unsigned long long common_ticktime__; |
|
15 static sem_t Run_PLC; |
14 |
16 |
15 long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
17 long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
|
18 { |
|
19 return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
20 } |
|
21 long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange) |
16 { |
22 { |
17 return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
23 return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
18 } |
24 } |
19 |
25 |
20 void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
26 void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
67 } |
73 } |
68 |
74 |
69 |
75 |
70 static unsigned long __debug_tick; |
76 static unsigned long __debug_tick; |
71 |
77 |
|
78 pthread_t PLC_thread; |
72 static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
79 static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
73 static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER; |
80 static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER; |
74 static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
81 static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
75 static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
82 static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
76 |
83 |
|
84 int PLC_shutdown = 0; |
|
85 |
|
86 void PLC_thread_proc(void *arg) |
|
87 { |
|
88 while (!PLC_shutdown) { |
|
89 sem_wait(&Run_PLC); |
|
90 __run(); |
|
91 } |
|
92 pthread_exit(0); |
|
93 } |
|
94 |
77 #define maxval(a,b) ((a>b)?a:b) |
95 #define maxval(a,b) ((a>b)?a:b) |
78 int startPLC(int argc,char **argv) |
96 int startPLC(int argc,char **argv) |
79 { |
97 { |
80 struct sigevent sigev; |
98 struct sigevent sigev; |
81 setlocale(LC_NUMERIC, "C"); |
99 setlocale(LC_NUMERIC, "C"); |
82 /* Define Ttick to 1ms if common_ticktime not defined */ |
100 /* Define Ttick to 1ms if common_ticktime not defined */ |
83 Ttick = common_ticktime__?common_ticktime__:1000000; |
101 Ttick = common_ticktime__?common_ticktime__:1000000; |
|
102 |
|
103 PLC_shutdown = 0; |
|
104 |
|
105 sem_init(&Run_PLC, 0, 0); |
|
106 |
|
107 pthread_create(&PLC_thread, NULL, (void*) &PLC_thread_proc, NULL); |
84 |
108 |
85 memset (&sigev, 0, sizeof (struct sigevent)); |
109 memset (&sigev, 0, sizeof (struct sigevent)); |
86 sigev.sigev_value.sival_int = 0; |
110 sigev.sigev_value.sival_int = 0; |
87 sigev.sigev_notify = SIGEV_THREAD; |
111 sigev.sigev_notify = SIGEV_THREAD; |
88 sigev.sigev_notify_attributes = NULL; |
112 sigev.sigev_notify_attributes = NULL; |
126 } |
150 } |
127 |
151 |
128 int stopPLC() |
152 int stopPLC() |
129 { |
153 { |
130 /* Stop the PLC */ |
154 /* Stop the PLC */ |
|
155 PLC_shutdown = 1; |
|
156 sem_post(&Run_PLC); |
131 PLC_SetTimer(0,0); |
157 PLC_SetTimer(0,0); |
|
158 pthread_join(PLC_thread, NULL); |
|
159 sem_destroy(&Run_PLC); |
132 timer_delete (PLC_timer); |
160 timer_delete (PLC_timer); |
133 __cleanup(); |
161 __cleanup(); |
134 pthread_mutex_destroy(&debug_wait_mutex); |
162 pthread_mutex_destroy(&debug_wait_mutex); |
135 pthread_mutex_destroy(&debug_mutex); |
163 pthread_mutex_destroy(&debug_mutex); |
136 pthread_mutex_destroy(&python_wait_mutex); |
164 pthread_mutex_destroy(&python_wait_mutex); |
141 extern unsigned long __tick; |
169 extern unsigned long __tick; |
142 |
170 |
143 int WaitDebugData(unsigned long *tick) |
171 int WaitDebugData(unsigned long *tick) |
144 { |
172 { |
145 int res; |
173 int res; |
|
174 if (PLC_shutdown) return 1; |
146 /* Wait signal from PLC thread */ |
175 /* Wait signal from PLC thread */ |
147 res = pthread_mutex_lock(&debug_wait_mutex); |
176 res = pthread_mutex_lock(&debug_wait_mutex); |
148 *tick = __debug_tick; |
177 *tick = __debug_tick; |
149 return res; |
178 return res; |
150 } |
179 } |