greg@205: #include greg@205: #include greg@205: #include greg@205: #include greg@205: #include greg@205: #include greg@205: greg@205: long AtomicCompareExchange(long* atomicvar,long exchange, long compared) greg@205: { greg@205: return __sync_val_compare_and_swap(atomicvar, compared, exchange); greg@205: } greg@205: greg@205: //long AtomicExchange(long* atomicvar,long exchange) greg@205: //{ greg@205: // return __sync_lock_test_and_set(atomicvar, exchange); greg@205: //} greg@205: greg@205: void PLC_GetTime(IEC_TIME *CURRENT_TIME) greg@205: { greg@205: clock_gettime(CLOCK_REALTIME, CURRENT_TIME); greg@205: } greg@205: greg@205: void PLC_timer_notify(sigval_t val) greg@205: { greg@205: PLC_GetTime(&__CURRENT_TIME); greg@205: __run(); greg@205: } greg@205: greg@205: timer_t PLC_timer; greg@205: greg@205: void PLC_SetTimer(long long next, long long period) greg@205: { greg@205: struct itimerspec timerValues; greg@205: /* greg@205: printf("SetTimer(%lld,%lld)\n",next, period); greg@205: */ greg@205: memset (&timerValues, 0, sizeof (struct itimerspec)); greg@205: { greg@205: #ifdef __lldiv_t_defined greg@205: lldiv_t nxt_div = lldiv(next, 1000000000); greg@205: lldiv_t period_div = lldiv(period, 1000000000); greg@205: timerValues.it_value.tv_sec = nxt_div.quot; greg@205: timerValues.it_value.tv_nsec = nxt_div.rem; greg@205: timerValues.it_interval.tv_sec = period_div.quot; greg@205: timerValues.it_interval.tv_nsec = period_div.rem; greg@205: #else greg@205: timerValues.it_value.tv_sec = next / 1000000000; greg@205: timerValues.it_value.tv_nsec = next % 1000000000; greg@205: timerValues.it_interval.tv_sec = period / 1000000000; greg@205: timerValues.it_interval.tv_nsec = period % 1000000000; greg@205: #endif greg@205: } greg@205: timer_settime (PLC_timer, 0, &timerValues, NULL); greg@205: } greg@205: // greg@205: void catch_signal(int sig) greg@205: { greg@205: // signal(SIGTERM, catch_signal); greg@205: signal(SIGINT, catch_signal); greg@205: printf("Got Signal %d\n",sig); greg@205: exit(0); greg@205: } greg@205: greg@205: int startPLC(int argc,char **argv) greg@205: { greg@205: struct sigevent sigev; greg@205: /* Translate PLC's microseconds to Ttick nanoseconds */ greg@205: Ttick = 1000000 * maxval(common_ticktime__,1); greg@205: greg@205: memset (&sigev, 0, sizeof (struct sigevent)); greg@205: sigev.sigev_value.sival_int = 0; greg@205: sigev.sigev_notify = SIGEV_THREAD; greg@205: sigev.sigev_notify_attributes = NULL; greg@205: sigev.sigev_notify_function = PLC_timer_notify; greg@205: greg@205: timer_create (CLOCK_REALTIME, &sigev, &PLC_timer); greg@205: if( __init(argc,argv) == 0 ){ greg@205: PLC_SetTimer(Ttick,Ttick); greg@205: greg@205: /* install signal handler for manual break */ greg@205: // signal(SIGTERM, catch_signal); greg@205: signal(SIGINT, catch_signal); greg@205: }else{ greg@205: return 1; greg@205: } greg@205: return 0; greg@205: } greg@205: etisserant@235: static int __debug_tick; etisserant@235: etisserant@235: static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER; etisserant@235: static pthread_cond_t wait_cond = PTHREAD_COND_INITIALIZER; etisserant@235: etisserant@235: void AbortDebug() etisserant@235: { etisserant@235: /* Eventually unlock debugger thread*/ etisserant@235: __debug_tick = -1; etisserant@235: pthread_mutex_lock(&wait_mutex); etisserant@235: pthread_cond_broadcast(&wait_cond); etisserant@235: pthread_mutex_unlock(&wait_mutex); etisserant@235: } etisserant@235: greg@205: int stopPLC() greg@205: { greg@205: /* Stop the PLC */ greg@205: PLC_SetTimer(0,0); greg@205: timer_delete (PLC_timer); greg@205: __cleanup(); etisserant@235: AbortDebug(); greg@205: } greg@205: etisserant@227: extern int __tick; greg@205: /* from plc_debugger.c */ etisserant@227: int WaitDebugData() greg@205: { greg@205: /* Wait signal from PLC thread */ etisserant@235: pthread_mutex_lock(&wait_mutex); etisserant@235: pthread_cond_wait(&wait_cond, &wait_mutex); etisserant@235: pthread_mutex_unlock(&wait_mutex); etisserant@227: return __debug_tick; greg@205: } greg@205: greg@205: /* Called by PLC thread when debug_publish finished greg@205: * This is supposed to unlock debugger thread in WaitDebugData*/ greg@205: void InitiateDebugTransfer() greg@205: { greg@205: /* signal debugger thread to continue*/ etisserant@227: __debug_tick = __tick; etisserant@235: pthread_mutex_lock(&wait_mutex); etisserant@235: pthread_cond_broadcast(&wait_cond); etisserant@235: pthread_mutex_unlock(&wait_mutex); greg@205: }