targets/Linux/plc_Linux_main.c
changeset 876 179d5c455f29
parent 617 7c23fac40a2a
child 954 ab487d32ce9a
equal deleted inserted replaced
875:a8952b79caec 876:179d5c455f29
     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)
    16 {
    18 {
    17     return __sync_val_compare_and_swap(atomicvar, compared, exchange);
    19     return __sync_val_compare_and_swap(atomicvar, compared, exchange);
    18 }
    20 }
    26 }
    28 }
    27 
    29 
    28 void PLC_timer_notify(sigval_t val)
    30 void PLC_timer_notify(sigval_t val)
    29 {
    31 {
    30     PLC_GetTime(&__CURRENT_TIME);
    32     PLC_GetTime(&__CURRENT_TIME);
    31     __run();
    33     sem_post(&Run_PLC);
    32 }
    34 }
    33 
    35 
    34 timer_t PLC_timer;
    36 timer_t PLC_timer;
    35 
    37 
    36 void PLC_SetTimer(unsigned long long next, unsigned long long period)
    38 void PLC_SetTimer(unsigned long long next, unsigned long long period)
    67 }
    69 }
    68 
    70 
    69 
    71 
    70 static unsigned long __debug_tick;
    72 static unsigned long __debug_tick;
    71 
    73 
       
    74 pthread_t PLC_thread;
    72 static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
    75 static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
    73 static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER;
    76 static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER;
    74 static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
    77 static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
    75 static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
    78 static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
    76 
    79 
       
    80 int PLC_shutdown = 0;
       
    81 
       
    82 void PLC_thread_proc(void *arg)
       
    83 {
       
    84     while (!PLC_shutdown) {
       
    85         sem_wait(&Run_PLC);
       
    86         __run();
       
    87     }
       
    88     pthread_exit(0);
       
    89 }
       
    90 
    77 #define maxval(a,b) ((a>b)?a:b)
    91 #define maxval(a,b) ((a>b)?a:b)
    78 int startPLC(int argc,char **argv)
    92 int startPLC(int argc,char **argv)
    79 {
    93 {
    80     struct sigevent sigev;
    94     struct sigevent sigev;
    81     setlocale(LC_NUMERIC, "C");
    95     setlocale(LC_NUMERIC, "C");
    82     /* Define Ttick to 1ms if common_ticktime not defined */
    96     /* Define Ttick to 1ms if common_ticktime not defined */
    83     Ttick = common_ticktime__?common_ticktime__:1000000;
    97     Ttick = common_ticktime__?common_ticktime__:1000000;
       
    98 
       
    99     PLC_shutdown = 0;
       
   100 
       
   101     sem_init(&Run_PLC, 0, 0);
       
   102 
       
   103     pthread_create(&PLC_thread, NULL, (void*) &PLC_thread_proc, NULL);
    84 
   104 
    85     memset (&sigev, 0, sizeof (struct sigevent));
   105     memset (&sigev, 0, sizeof (struct sigevent));
    86     sigev.sigev_value.sival_int = 0;
   106     sigev.sigev_value.sival_int = 0;
    87     sigev.sigev_notify = SIGEV_THREAD;
   107     sigev.sigev_notify = SIGEV_THREAD;
    88     sigev.sigev_notify_attributes = NULL;
   108     sigev.sigev_notify_attributes = NULL;
   126 }
   146 }
   127 
   147 
   128 int stopPLC()
   148 int stopPLC()
   129 {
   149 {
   130     /* Stop the PLC */
   150     /* Stop the PLC */
       
   151     PLC_shutdown = 1;
       
   152     sem_post(&Run_PLC);
   131     PLC_SetTimer(0,0);
   153     PLC_SetTimer(0,0);
       
   154 	pthread_join(PLC_thread, NULL);
       
   155 	sem_destroy(&Run_PLC);
   132     timer_delete (PLC_timer);
   156     timer_delete (PLC_timer);
   133     __cleanup();
   157     __cleanup();
   134     pthread_mutex_destroy(&debug_wait_mutex);
   158     pthread_mutex_destroy(&debug_wait_mutex);
   135     pthread_mutex_destroy(&debug_mutex);
   159     pthread_mutex_destroy(&debug_mutex);
   136     pthread_mutex_destroy(&python_wait_mutex);
   160     pthread_mutex_destroy(&python_wait_mutex);
   141 extern unsigned long __tick;
   165 extern unsigned long __tick;
   142 
   166 
   143 int WaitDebugData(unsigned long *tick)
   167 int WaitDebugData(unsigned long *tick)
   144 {
   168 {
   145     int res;
   169     int res;
       
   170     if (PLC_shutdown) return 1;
   146     /* Wait signal from PLC thread */
   171     /* Wait signal from PLC thread */
   147     res = pthread_mutex_lock(&debug_wait_mutex);
   172     res = pthread_mutex_lock(&debug_wait_mutex);
   148     *tick = __debug_tick;
   173     *tick = __debug_tick;
   149     return res;
   174     return res;
   150 }
   175 }