targets/Linux/plc_Linux_main.c
author Edouard Tisserant <edouard.tisserant@gmail.com>
Mon, 27 Mar 2023 10:12:20 +0200
branchwxPython4
changeset 3748 a811e1ff718a
parent 3740 ac0e6de439b5
permissions -rw-r--r--
Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.

Adding BEREMIZ_TEST_CYCLES=1000 in a project's CFLAGS will:
- run 1000 cycles with no pause
- emulate time flowing normaly for PLC code
- exit PLC thread

This allows:
- testing standard library blocks that deal with time without having to wait
- unit testing and code coverage with POUs that uses time
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
     1
/**
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
     2
 * Linux specific code
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
     3
 **/
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
     4
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     5
#include <stdio.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     6
#include <string.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     7
#include <time.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     8
#include <signal.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     9
#include <stdlib.h>
3725
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
    10
#include <errno.h>
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
    11
#include <pthread.h>
568
20a223828a06 Moved locales out of platform agnostic C template
edouard
parents: 522
diff changeset
    12
#include <locale.h>
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    13
#include <semaphore.h>
3732
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
    14
#ifdef REALTIME_LINUX
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
    15
#include <sys/mman.h>
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
    16
#endif
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    17
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    18
#define _Log(level,text,...) \
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    19
    {\
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    20
        char mstr[256];\
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    21
        snprintf(mstr, 255, text, ##__VA_ARGS__);\
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    22
        LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    23
    }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    24
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    25
#define _LogError(text,...) _Log(LOG_CRITICAL, text, ##__VA_ARGS__)
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    26
#define _LogWarning(text,...) _Log(LOG_WARNING, text, ##__VA_ARGS__)
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    27
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    28
static unsigned long __debug_tick;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    29
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    30
static pthread_t PLC_thread;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    31
static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    32
static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    33
static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    34
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    35
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    36
static int PLC_shutdown = 0;
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    37
236
a32817e81f5e Now debug all ticks, not only odd ones :-)
etisserant
parents: 235
diff changeset
    38
long AtomicCompareExchange(long* atomicvar,long compared, long exchange)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    39
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    40
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    41
}
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 876
diff changeset
    42
long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange)
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 876
diff changeset
    43
{
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 876
diff changeset
    44
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 876
diff changeset
    45
}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    46
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    47
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    48
{
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    49
    struct timespec tmp;
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    50
    clock_gettime(CLOCK_REALTIME, &tmp);
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    51
    CURRENT_TIME->tv_sec = tmp.tv_sec;
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    52
    CURRENT_TIME->tv_nsec = tmp.tv_nsec;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    53
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    54
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    55
static long long period_ns = 0;
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    56
struct timespec next_cycle_time;
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    57
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    58
static void inc_timespec(struct timespec *ts, unsigned long long value_ns)
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    59
{
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    60
    long long next_ns = ((long long) ts->tv_sec * 1000000000) + ts->tv_nsec + value_ns;
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    61
#ifdef __lldiv_t_defined
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    62
    lldiv_t next_div = lldiv(next_ns, 1000000000);
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    63
    ts->tv_sec = next_div.quot;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    64
    ts->tv_nsec = next_div.rem;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    65
#else
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    66
    ts->tv_sec = next_ns / 1000000000;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    67
    ts->tv_nsec = next_ns % 1000000000;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    68
#endif
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    69
}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    70
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 485
diff changeset
    71
void PLC_SetTimer(unsigned long long next, unsigned long long period)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    72
{
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    73
    /*
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    74
    printf("SetTimer(%lld,%lld)\n",next, period);
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    75
    */
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    76
    period_ns = period;
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    77
    clock_gettime(CLOCK_MONOTONIC, &next_cycle_time);
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
    78
    inc_timespec(&next_cycle_time, next);
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    79
    // interrupt clock_nanpsleep
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    80
    pthread_kill(PLC_thread, SIGUSR1);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    81
}
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    82
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    83
void catch_signal(int sig)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    84
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    85
//  signal(SIGTERM, catch_signal);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    86
  signal(SIGINT, catch_signal);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    87
  printf("Got Signal %d\n",sig);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    88
  exit(0);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    89
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    90
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    91
void PLCThreadSignalHandler(int sig)
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    92
{
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    93
    if (sig == SIGUSR2)
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    94
        pthread_exit(NULL);
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    95
}
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    96
2173
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    97
int ForceSaveRetainReq(void) {
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    98
    return PLC_shutdown;
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    99
}
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
   100
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   101
#define MAX_JITTER period_ns/10
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   102
#define MIN_IDLE_TIME_NS 1000000 /* 1ms */
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   103
/* Macro to compare timespec, evaluate to True if a is past b */
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   104
#define timespec_gt(a,b) (a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec > b.tv_nsec))
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   105
void PLC_thread_proc(void *arg)
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   106
{
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   107
    /* initialize next occurence and period */
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   108
    period_ns = common_ticktime__;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   109
    clock_gettime(CLOCK_MONOTONIC, &next_cycle_time);
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   110
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   111
    while (!PLC_shutdown) {
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   112
        int res;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   113
        struct timespec plc_end_time;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   114
        int periods = 0;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   115
#ifdef REALTIME_LINUX
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   116
        struct timespec deadline_time;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   117
        struct timespec plc_start_time;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   118
#endif
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   119
3748
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   120
// BEREMIZ_TEST_CYCLES is defined in tests that need to emulate time:
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   121
// - all BEREMIZ_TEST_CYCLES cycles are executed in a row with no pause
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   122
// - __CURRENT_TIME is incremented each cycle according to emulated cycle period
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   123
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   124
#ifndef BEREMIZ_TEST_CYCLES
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   125
        // Sleep until next PLC run
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   126
        res = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_cycle_time, NULL);
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   127
        if(res==EINTR){
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   128
            continue;
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   129
        }
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   130
        if(res!=0){
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   131
            _LogError("PLC thread timer returned error %d \n", res);
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   132
            return;
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   133
        }
3748
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   134
#endif // BEREMIZ_TEST_CYCLES
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   135
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   136
#ifdef REALTIME_LINUX
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   137
        // timer overrun detection
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   138
        clock_gettime(CLOCK_MONOTONIC, &plc_start_time);
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   139
        deadline_time=next_cycle_time;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   140
        inc_timespec(&deadline_time, MAX_JITTER);
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   141
        if(timespec_gt(plc_start_time, deadline_time)){
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   142
            _LogWarning("PLC thread woken up too late. PLC cyclic task interval is too small.\n");
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   143
        }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   144
#endif
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   145
3748
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   146
#ifdef BEREMIZ_TEST_CYCLES
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   147
#define xstr(s) str(s)
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   148
#define str(arg) #arg
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   149
        // fake current time
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   150
        __CURRENT_TIME.tv_sec = next_cycle_time.tv_sec;
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   151
        __CURRENT_TIME.tv_nsec = next_cycle_time.tv_nsec;
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   152
        // exit loop when enough cycles
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   153
        if(__tick >= BEREMIZ_TEST_CYCLES) {
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   154
            _LogWarning("TEST PLC thread ended after "xstr(BEREMIZ_TEST_CYCLES)" cycles.\n");
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   155
            // After pre-defined test cycles count, PLC thread exits.
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   156
            // Remaining PLC runtime is expected to be cleaned-up/killed by test script
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   157
            return;
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   158
        }
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   159
#else
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   160
        PLC_GetTime(&__CURRENT_TIME);
3748
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   161
#endif
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   162
        __run();
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   163
3748
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   164
#ifndef BEREMIZ_TEST_CYCLES
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   165
        // ensure next PLC cycle occurence is in the future
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   166
        clock_gettime(CLOCK_MONOTONIC, &plc_end_time);
3748
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   167
        while(timespec_gt(plc_end_time, next_cycle_time))
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   168
#endif
a811e1ff718a Tests: Add time emulation feature for tests with BEREMIZ_TEST_CYCLES CFLAG.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3740
diff changeset
   169
        {
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   170
            periods += 1;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   171
            inc_timespec(&next_cycle_time, period_ns);
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   172
        }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   173
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   174
        // plc execution time overrun detection
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   175
        if(periods > 1) {
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   176
            // Mitigate CPU hogging, in case of too small cyclic task interval:
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   177
            //  - since cycle deadline already missed, better keep system responsive
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   178
            //  - test if next cycle occurs after minimal idle
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   179
            //  - enforce minimum idle time if not
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   180
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   181
            struct timespec earliest_possible_time = plc_end_time;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   182
            inc_timespec(&earliest_possible_time, MIN_IDLE_TIME_NS);
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   183
            while(timespec_gt(earliest_possible_time, next_cycle_time)){
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   184
                periods += 1;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   185
                inc_timespec(&next_cycle_time, period_ns);
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   186
            }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   187
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   188
            // increment tick count anyhow, so that task scheduling keeps consistent
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   189
            __tick+=periods-1;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   190
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   191
            _LogWarning("PLC execution time is longer than requested PLC cyclic task interval. %d cycles skipped\n", periods);
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   192
        }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   193
    }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   194
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   195
    pthread_exit(0);
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   196
}
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   197
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   198
#define maxval(a,b) ((a>b)?a:b)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   199
int startPLC(int argc,char **argv)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   200
{
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   201
3732
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   202
    int ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   203
	pthread_attr_t *pattr = NULL;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   204
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   205
#ifdef REALTIME_LINUX
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   206
	struct sched_param param;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   207
	pthread_attr_t attr;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   208
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   209
    /* Lock memory */
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   210
    ret = mlockall(MCL_CURRENT|MCL_FUTURE);
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   211
    if(ret == -1) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   212
		_LogError("mlockall failed: %m\n");
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   213
		return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   214
    }
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   215
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   216
	/* Initialize pthread attributes (default values) */
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   217
	ret = pthread_attr_init(&attr);
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   218
	if (ret) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   219
		_LogError("init pthread attributes failed\n");
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   220
		return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   221
	}
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   222
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   223
	/* Set scheduler policy and priority of pthread */
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   224
	ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   225
	if (ret) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   226
		_LogError("pthread setschedpolicy failed\n");
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   227
		return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   228
	}
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   229
	param.sched_priority = PLC_THREAD_PRIORITY;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   230
	ret = pthread_attr_setschedparam(&attr, &param);
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   231
	if (ret) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   232
		_LogError("pthread setschedparam failed\n");
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   233
		return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   234
	}
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   235
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   236
	/* Use scheduling parameters of attr */
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   237
	ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   238
	if (ret) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   239
		_LogError("pthread setinheritsched failed\n");
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   240
		return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   241
	}
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   242
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   243
	pattr = &attr;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   244
#endif
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   245
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   246
    PLC_shutdown = 0;
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   247
333
e90aebdd2af1 fixed wrong pthread initialisation
lbessard
parents: 329
diff changeset
   248
    pthread_mutex_init(&debug_wait_mutex, NULL);
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   249
    pthread_mutex_init(&debug_mutex, NULL);
333
e90aebdd2af1 fixed wrong pthread initialisation
lbessard
parents: 329
diff changeset
   250
    pthread_mutex_init(&python_wait_mutex, NULL);
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   251
    pthread_mutex_init(&python_mutex, NULL);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   252
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   253
    pthread_mutex_lock(&debug_wait_mutex);
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   254
    pthread_mutex_lock(&python_wait_mutex);
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   255
3732
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   256
    if((ret = __init(argc,argv)) == 0 ){
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   257
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   258
        /* Signal to wakeup PLC thread when period changes */
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   259
        signal(SIGUSR1, PLCThreadSignalHandler);
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   260
        /* Signal to end PLC thread */
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   261
        signal(SIGUSR2, PLCThreadSignalHandler);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   262
        /* install signal handler for manual break */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   263
        signal(SIGINT, catch_signal);
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   264
3732
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   265
        ret = pthread_create(&PLC_thread, pattr, (void*) &PLC_thread_proc, NULL);
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   266
		if (ret) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   267
			_LogError("create pthread failed\n");
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   268
			return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   269
		}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   270
    }else{
3732
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   271
        return ret;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   272
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   273
    return 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   274
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   275
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   276
int TryEnterDebugSection(void)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   277
{
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   278
    if (pthread_mutex_trylock(&debug_mutex) == 0){
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   279
        /* Only enter if debug active */
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   280
        if(__DEBUG){
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   281
            return 1;
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   282
        }
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 462
diff changeset
   283
        pthread_mutex_unlock(&debug_mutex);
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   284
    }
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   285
    return 0;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   286
}
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   287
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   288
void LeaveDebugSection(void)
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   289
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   290
    pthread_mutex_unlock(&debug_mutex);
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   291
}
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   292
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   293
int stopPLC()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   294
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   295
    /* Stop the PLC */
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   296
    PLC_shutdown = 1;
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   297
    /* Order PLCThread to exit */
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   298
    pthread_kill(PLC_thread, SIGUSR2);
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   299
    pthread_join(PLC_thread, NULL);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   300
    __cleanup();
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   301
    pthread_mutex_destroy(&debug_wait_mutex);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 462
diff changeset
   302
    pthread_mutex_destroy(&debug_mutex);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   303
    pthread_mutex_destroy(&python_wait_mutex);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 462
diff changeset
   304
    pthread_mutex_destroy(&python_mutex);
386
2932b0dd437c Applying patch from Iztok for old gcc versions
laurent
parents: 333
diff changeset
   305
    return 0;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   306
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   307
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 386
diff changeset
   308
extern unsigned long __tick;
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   309
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   310
int WaitDebugData(unsigned long *tick)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   311
{
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   312
    int res;
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   313
    if (PLC_shutdown) return 1;
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   314
    /* Wait signal from PLC thread */
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   315
    res = pthread_mutex_lock(&debug_wait_mutex);
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   316
    *tick = __debug_tick;
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   317
    return res;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   318
}
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   319
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   320
/* Called by PLC thread when debug_publish finished
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   321
 * This is supposed to unlock debugger thread in WaitDebugData*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   322
void InitiateDebugTransfer()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   323
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   324
    /* remember tick */
227
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 205
diff changeset
   325
    __debug_tick = __tick;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   326
    /* signal debugger thread it can read data */
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   327
    pthread_mutex_unlock(&debug_wait_mutex);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   328
}
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   329
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 592
diff changeset
   330
int suspendDebug(int disable)
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   331
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   332
    /* Prevent PLC to enter debug code */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   333
    pthread_mutex_lock(&debug_mutex);
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   334
    /*__DEBUG is protected by this mutex */
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   335
    __DEBUG = !disable;
485
8b2da4b9d408 Bug that block SetVariableList on Linux fixed
laurent
parents: 483
diff changeset
   336
    if (disable)
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   337
        pthread_mutex_unlock(&debug_mutex);
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 592
diff changeset
   338
    return 0;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   339
}
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   340
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   341
void resumeDebug(void)
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   342
{
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 280
diff changeset
   343
    __DEBUG = 1;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   344
    /* Let PLC enter debug code */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   345
    pthread_mutex_unlock(&debug_mutex);
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   346
}
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   347
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   348
/* from plc_python.c */
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   349
int WaitPythonCommands(void)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   350
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   351
    /* Wait signal from PLC thread */
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   352
    return pthread_mutex_lock(&python_wait_mutex);
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   353
}
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   354
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   355
/* Called by PLC thread on each new python command*/
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   356
void UnBlockPythonCommands(void)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   357
{
3334
d7e0ddb5974b Fix wrong comment
Edouard Tisserant
parents: 3295
diff changeset
   358
    /* signal python thread it can read data */
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   359
    pthread_mutex_unlock(&python_wait_mutex);
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   360
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   361
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   362
int TryLockPython(void)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   363
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   364
    return pthread_mutex_trylock(&python_mutex) == 0;
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   365
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   366
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   367
void UnLockPython(void)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   368
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   369
    pthread_mutex_unlock(&python_mutex);
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   370
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   371
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   372
void LockPython(void)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   373
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   374
    pthread_mutex_lock(&python_mutex);
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   375
}
2820
d9b5303d43dc SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents: 2173
diff changeset
   376
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   377
struct RT_to_nRT_signal_s {
3725
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   378
    int used;
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   379
    pthread_cond_t WakeCond;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   380
    pthread_mutex_t WakeCondLock;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   381
};
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   382
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   383
typedef struct RT_to_nRT_signal_s RT_to_nRT_signal_t;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   384
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   385
#define _LogAndReturnNull(text) \
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   386
    {\
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   387
        char mstr[256] = text " for ";\
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   388
        strncat(mstr, name, 255);\
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   389
        LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   390
        return NULL;\
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   391
    }
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   392
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   393
void *create_RT_to_nRT_signal(char* name){
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   394
    RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)malloc(sizeof(RT_to_nRT_signal_t));
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   395
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   396
    if(!sig)
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   397
        _LogAndReturnNull("Failed allocating memory for RT_to_nRT signal");
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   398
3725
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   399
    sig->used = 1;
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   400
    pthread_cond_init(&sig->WakeCond, NULL);
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   401
    pthread_mutex_init(&sig->WakeCondLock, NULL);
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   402
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   403
    return (void*)sig;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   404
}
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   405
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   406
void delete_RT_to_nRT_signal(void* handle){
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   407
    RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   408
3725
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   409
    pthread_mutex_lock(&sig->WakeCondLock);
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   410
    sig->used = 0;
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   411
    pthread_cond_signal(&sig->WakeCond);
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   412
    pthread_mutex_unlock(&sig->WakeCondLock);
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   413
}
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   414
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   415
int wait_RT_to_nRT_signal(void* handle){
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   416
    int ret;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   417
    RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   418
    pthread_mutex_lock(&sig->WakeCondLock);
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   419
    ret = pthread_cond_wait(&sig->WakeCond, &sig->WakeCondLock);
3725
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   420
    if(!sig->used) ret = -EINVAL;
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   421
    pthread_mutex_unlock(&sig->WakeCondLock);
3725
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   422
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   423
    if(!sig->used){
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   424
        pthread_cond_destroy(&sig->WakeCond);
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   425
        pthread_mutex_destroy(&sig->WakeCondLock);
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   426
        free(sig);
0043e2b9dbec Linux runtime: fix thread waiting on wait_RT_to_nRT_signal not being waken-up when delete_RT_to_nRT_signal is invoked.
Edouard Tisserant
parents: 3334
diff changeset
   427
    }
3294
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   428
    return ret;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   429
}
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   430
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   431
int unblock_RT_to_nRT_signal(void* handle){
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   432
    RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   433
    return pthread_cond_signal(&sig->WakeCond);
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   434
}
e3db472b0dfb Runtime+SVGHMI: Added a generic way to wakeup non-real-time threads from real-time PLC thread. Replace SVGHMI specific calls in Linux and Xenomai implementations of plc_main.c. Fixed xenomai build, xeno-config making problems with --no-auto-init argument.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2820
diff changeset
   435
3295
0375d801fff7 Runtime+SVGHMI: Add generic wakeup of threads from PLC thread to windows implementation of plc_main.c. Also added nRT_reschedule to abstract sched_yield.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3294
diff changeset
   436
void nRT_reschedule(void){
0375d801fff7 Runtime+SVGHMI: Add generic wakeup of threads from PLC thread to windows implementation of plc_main.c. Also added nRT_reschedule to abstract sched_yield.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3294
diff changeset
   437
    sched_yield();
0375d801fff7 Runtime+SVGHMI: Add generic wakeup of threads from PLC thread to windows implementation of plc_main.c. Also added nRT_reschedule to abstract sched_yield.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3294
diff changeset
   438
}