103 inc_timespec(&next_abs_time, period_ns); |
106 inc_timespec(&next_abs_time, period_ns); |
104 } |
107 } |
105 pthread_exit(0); |
108 pthread_exit(0); |
106 } |
109 } |
107 |
110 |
|
111 #define _LogError(text,...) \ |
|
112 {\ |
|
113 char mstr[256];\ |
|
114 snprintf(mstr, 255, text, ##__VA_ARGS__);\ |
|
115 LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\ |
|
116 } |
108 #define maxval(a,b) ((a>b)?a:b) |
117 #define maxval(a,b) ((a>b)?a:b) |
109 int startPLC(int argc,char **argv) |
118 int startPLC(int argc,char **argv) |
110 { |
119 { |
111 setlocale(LC_NUMERIC, "C"); |
120 |
|
121 int ret; |
|
122 pthread_attr_t *pattr = NULL; |
|
123 |
|
124 #ifdef REALTIME_LINUX |
|
125 struct sched_param param; |
|
126 pthread_attr_t attr; |
|
127 |
|
128 /* Lock memory */ |
|
129 ret = mlockall(MCL_CURRENT|MCL_FUTURE); |
|
130 if(ret == -1) { |
|
131 _LogError("mlockall failed: %m\n"); |
|
132 return ret; |
|
133 } |
|
134 |
|
135 /* Initialize pthread attributes (default values) */ |
|
136 ret = pthread_attr_init(&attr); |
|
137 if (ret) { |
|
138 _LogError("init pthread attributes failed\n"); |
|
139 return ret; |
|
140 } |
|
141 |
|
142 /* Set scheduler policy and priority of pthread */ |
|
143 ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO); |
|
144 if (ret) { |
|
145 _LogError("pthread setschedpolicy failed\n"); |
|
146 return ret; |
|
147 } |
|
148 param.sched_priority = PLC_THREAD_PRIORITY; |
|
149 ret = pthread_attr_setschedparam(&attr, ¶m); |
|
150 if (ret) { |
|
151 _LogError("pthread setschedparam failed\n"); |
|
152 return ret; |
|
153 } |
|
154 |
|
155 /* Use scheduling parameters of attr */ |
|
156 ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); |
|
157 if (ret) { |
|
158 _LogError("pthread setinheritsched failed\n"); |
|
159 return ret; |
|
160 } |
|
161 |
|
162 pattr = &attr; |
|
163 #endif |
112 |
164 |
113 PLC_shutdown = 0; |
165 PLC_shutdown = 0; |
114 |
166 |
115 pthread_mutex_init(&debug_wait_mutex, NULL); |
167 pthread_mutex_init(&debug_wait_mutex, NULL); |
116 pthread_mutex_init(&debug_mutex, NULL); |
168 pthread_mutex_init(&debug_mutex, NULL); |
118 pthread_mutex_init(&python_mutex, NULL); |
170 pthread_mutex_init(&python_mutex, NULL); |
119 |
171 |
120 pthread_mutex_lock(&debug_wait_mutex); |
172 pthread_mutex_lock(&debug_wait_mutex); |
121 pthread_mutex_lock(&python_wait_mutex); |
173 pthread_mutex_lock(&python_wait_mutex); |
122 |
174 |
123 if( __init(argc,argv) == 0 ){ |
175 if((ret = __init(argc,argv)) == 0 ){ |
124 |
176 |
125 /* Signal to wakeup PLC thread when period changes */ |
177 /* Signal to wakeup PLC thread when period changes */ |
126 signal(SIGUSR1, PLCThreadSignalHandler); |
178 signal(SIGUSR1, PLCThreadSignalHandler); |
127 /* Signal to end PLC thread */ |
179 /* Signal to end PLC thread */ |
128 signal(SIGUSR2, PLCThreadSignalHandler); |
180 signal(SIGUSR2, PLCThreadSignalHandler); |
131 |
183 |
132 /* initialize next occurence and period */ |
184 /* initialize next occurence and period */ |
133 period_ns = common_ticktime__; |
185 period_ns = common_ticktime__; |
134 clock_gettime(CLOCK_MONOTONIC, &next_abs_time); |
186 clock_gettime(CLOCK_MONOTONIC, &next_abs_time); |
135 |
187 |
136 pthread_create(&PLC_thread, NULL, (void*) &PLC_thread_proc, NULL); |
188 ret = pthread_create(&PLC_thread, pattr, (void*) &PLC_thread_proc, NULL); |
|
189 if (ret) { |
|
190 _LogError("create pthread failed\n"); |
|
191 return ret; |
|
192 } |
137 }else{ |
193 }else{ |
138 return 1; |
194 return ret; |
139 } |
195 } |
140 return 0; |
196 return 0; |
141 } |
197 } |
142 |
198 |
143 int TryEnterDebugSection(void) |
199 int TryEnterDebugSection(void) |