targets/Linux/plc_Linux_main.c
changeset 3732 929276eea252
parent 3731 549763a28934
child 3740 ac0e6de439b5
equal deleted inserted replaced
3731:549763a28934 3732:929276eea252
     9 #include <stdlib.h>
     9 #include <stdlib.h>
    10 #include <errno.h>
    10 #include <errno.h>
    11 #include <pthread.h>
    11 #include <pthread.h>
    12 #include <locale.h>
    12 #include <locale.h>
    13 #include <semaphore.h>
    13 #include <semaphore.h>
       
    14 #ifdef REALTIME_LINUX
       
    15 #include <sys/mman.h>
       
    16 #endif
    14 
    17 
    15 static unsigned long __debug_tick;
    18 static unsigned long __debug_tick;
    16 
    19 
    17 static pthread_t PLC_thread;
    20 static pthread_t PLC_thread;
    18 static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
    21 static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
   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 {
       
   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, &param);
       
   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
   111 
   164 
   112     PLC_shutdown = 0;
   165     PLC_shutdown = 0;
   113 
   166 
   114     pthread_mutex_init(&debug_wait_mutex, NULL);
   167     pthread_mutex_init(&debug_wait_mutex, NULL);
   115     pthread_mutex_init(&debug_mutex, NULL);
   168     pthread_mutex_init(&debug_mutex, NULL);
   117     pthread_mutex_init(&python_mutex, NULL);
   170     pthread_mutex_init(&python_mutex, NULL);
   118 
   171 
   119     pthread_mutex_lock(&debug_wait_mutex);
   172     pthread_mutex_lock(&debug_wait_mutex);
   120     pthread_mutex_lock(&python_wait_mutex);
   173     pthread_mutex_lock(&python_wait_mutex);
   121 
   174 
   122     if(  __init(argc,argv) == 0 ){
   175     if((ret = __init(argc,argv)) == 0 ){
   123 
   176 
   124         /* Signal to wakeup PLC thread when period changes */
   177         /* Signal to wakeup PLC thread when period changes */
   125         signal(SIGUSR1, PLCThreadSignalHandler);
   178         signal(SIGUSR1, PLCThreadSignalHandler);
   126         /* Signal to end PLC thread */
   179         /* Signal to end PLC thread */
   127         signal(SIGUSR2, PLCThreadSignalHandler);
   180         signal(SIGUSR2, PLCThreadSignalHandler);
   130 
   183 
   131         /* initialize next occurence and period */
   184         /* initialize next occurence and period */
   132         period_ns = common_ticktime__;
   185         period_ns = common_ticktime__;
   133         clock_gettime(CLOCK_MONOTONIC, &next_abs_time);
   186         clock_gettime(CLOCK_MONOTONIC, &next_abs_time);
   134 
   187 
   135         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 		}
   136     }else{
   193     }else{
   137         return 1;
   194         return ret;
   138     }
   195     }
   139     return 0;
   196     return 0;
   140 }
   197 }
   141 
   198 
   142 int TryEnterDebugSection(void)
   199 int TryEnterDebugSection(void)