static unsigned long __debug_tick;
static pthread_t PLC_thread;
static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
static int PLC_shutdown = 0;
long AtomicCompareExchange(long* atomicvar,long compared, long exchange)
return __sync_val_compare_and_swap(atomicvar, compared, exchange);
long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange)
return __sync_val_compare_and_swap(atomicvar, compared, exchange);
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
clock_gettime(CLOCK_REALTIME, &tmp);
CURRENT_TIME->tv_sec = tmp.tv_sec;
CURRENT_TIME->tv_nsec = tmp.tv_nsec;
static long long period_ns = 0;
struct timespec next_abs_time;
static void inc_timespec(struct timespec *ts, unsigned long long value_ns)
long long next_ns = ((long long) ts->tv_sec * 1000000000) + ts->tv_nsec + value_ns;
lldiv_t next_div = lldiv(next_ns, 1000000000);
ts->tv_sec = next_div.quot;
ts->tv_nsec = next_div.rem;
ts->tv_sec = next_ns / 1000000000;
ts->tv_nsec = next_ns % 1000000000;
void PLC_SetTimer(unsigned long long next, unsigned long long period)
printf("SetTimer(%lld,%lld)\n",next, period);
clock_gettime(CLOCK_MONOTONIC, &next_abs_time);
inc_timespec(&next_abs_time, next);
// interrupt clock_nanpsleep
pthread_kill(PLC_thread, SIGUSR1);
void catch_signal(int sig)
// signal(SIGTERM, catch_signal);
signal(SIGINT, catch_signal);
printf("Got Signal %d\n",sig);
void PLCThreadSignalHandler(int sig)
int ForceSaveRetainReq(void) {
void PLC_thread_proc(void *arg)
// Sleep until next PLC run
// TODO check result of clock_nanosleep and wait again or exit eventually
int res = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_abs_time, NULL);
printf("PLC thread died with error %d \n", res);
PLC_GetTime(&__CURRENT_TIME);
inc_timespec(&next_abs_time, period_ns);
#define maxval(a,b) ((a>b)?a:b)
int startPLC(int argc,char **argv)
setlocale(LC_NUMERIC, "C");
pthread_mutex_init(&debug_wait_mutex, NULL);
pthread_mutex_init(&debug_mutex, NULL);
pthread_mutex_init(&python_wait_mutex, NULL);
pthread_mutex_init(&python_mutex, NULL);
pthread_mutex_lock(&debug_wait_mutex);
pthread_mutex_lock(&python_wait_mutex);
if( __init(argc,argv) == 0 ){
/* Signal to wakeup PLC thread when period changes */
signal(SIGUSR1, PLCThreadSignalHandler);
/* Signal to end PLC thread */
signal(SIGUSR2, PLCThreadSignalHandler);
/* install signal handler for manual break */
signal(SIGINT, catch_signal);
/* initialize next occurence and period */
period_ns = common_ticktime__;
clock_gettime(CLOCK_MONOTONIC, &next_abs_time);
pthread_create(&PLC_thread, NULL, (void*) &PLC_thread_proc, NULL);
int TryEnterDebugSection(void)
if (pthread_mutex_trylock(&debug_mutex) == 0){
/* Only enter if debug active */
pthread_mutex_unlock(&debug_mutex);
void LeaveDebugSection(void)
pthread_mutex_unlock(&debug_mutex);
/* Order PLCThread to exit */
pthread_kill(PLC_thread, SIGUSR2);
pthread_join(PLC_thread, NULL);
pthread_mutex_destroy(&debug_wait_mutex);
pthread_mutex_destroy(&debug_mutex);
pthread_mutex_destroy(&python_wait_mutex);
pthread_mutex_destroy(&python_mutex);
extern unsigned long __tick;
int WaitDebugData(unsigned long *tick)
if (PLC_shutdown) return 1;
/* Wait signal from PLC thread */
res = pthread_mutex_lock(&debug_wait_mutex);
/* Called by PLC thread when debug_publish finished
* This is supposed to unlock debugger thread in WaitDebugData*/
void InitiateDebugTransfer()
/* signal debugger thread it can read data */
pthread_mutex_unlock(&debug_wait_mutex);
int suspendDebug(int disable)
/* Prevent PLC to enter debug code */
pthread_mutex_lock(&debug_mutex);
/*__DEBUG is protected by this mutex */
pthread_mutex_unlock(&debug_mutex);
/* Let PLC enter debug code */
pthread_mutex_unlock(&debug_mutex);
int WaitPythonCommands(void)
/* Wait signal from PLC thread */
return pthread_mutex_lock(&python_wait_mutex);
/* Called by PLC thread on each new python command*/
void UnBlockPythonCommands(void)
/* signal python thread it can read data */
pthread_mutex_unlock(&python_wait_mutex);
return pthread_mutex_trylock(&python_mutex) == 0;
pthread_mutex_unlock(&python_mutex);
pthread_mutex_lock(&python_mutex);
struct RT_to_nRT_signal_s {
pthread_mutex_t WakeCondLock;
typedef struct RT_to_nRT_signal_s RT_to_nRT_signal_t;
#define _LogAndReturnNull(text) \
char mstr[256] = text " for ";\
strncat(mstr, name, 255);\
LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\
void *create_RT_to_nRT_signal(char* name){
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)malloc(sizeof(RT_to_nRT_signal_t));
_LogAndReturnNull("Failed allocating memory for RT_to_nRT signal");
pthread_cond_init(&sig->WakeCond, NULL);
pthread_mutex_init(&sig->WakeCondLock, NULL);
void delete_RT_to_nRT_signal(void* handle){
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
pthread_mutex_lock(&sig->WakeCondLock);
pthread_cond_signal(&sig->WakeCond);
pthread_mutex_unlock(&sig->WakeCondLock);
int wait_RT_to_nRT_signal(void* handle){
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
pthread_mutex_lock(&sig->WakeCondLock);
ret = pthread_cond_wait(&sig->WakeCond, &sig->WakeCondLock);
if(!sig->used) ret = -EINVAL;
pthread_mutex_unlock(&sig->WakeCondLock);
pthread_cond_destroy(&sig->WakeCond);
pthread_mutex_destroy(&sig->WakeCondLock);
int unblock_RT_to_nRT_signal(void* handle){
RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
return pthread_cond_signal(&sig->WakeCond);
void nRT_reschedule(void){