targets/Linux/plc_Linux_main.c
author Edouard Tisserant
Wed, 01 Mar 2023 10:54:54 +0100
changeset 3740 ac0e6de439b5
parent 3732 929276eea252
child 3748 a811e1ff718a
permissions -rw-r--r--
Linux runtime: overrun detection for real-time timers and for plc execution.

If real-time timer wakes-up PLC thread too late (10% over period), then
warning is logged.

If PLC code (IO retreive, execution, IO publish) takes longer than requested
PLC execution cycle, then warning is logged, and CPU hoogging is mitigated
by delaying next PLC execution a few cylces more until having at least
1ms minimal idle 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
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   120
        // 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
   121
        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
   122
        if(res==EINTR){
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   123
            continue;
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   124
        }
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   125
        if(res!=0){
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   126
            _LogError("PLC thread timer returned error %d \n", res);
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   127
            return;
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   128
        }
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   129
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   130
#ifdef REALTIME_LINUX
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   131
        // timer overrun detection
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   132
        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
   133
        deadline_time=next_cycle_time;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   134
        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
   135
        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
   136
            _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
   137
        }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   138
#endif
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   139
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   140
        PLC_GetTime(&__CURRENT_TIME);
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   141
        __run();
3740
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   142
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   143
        // 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
   144
        clock_gettime(CLOCK_MONOTONIC, &plc_end_time);
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   145
        while(timespec_gt(plc_end_time, next_cycle_time)){
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   146
            periods += 1;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   147
            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
   148
        }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   149
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   150
        // plc execution time overrun detection
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   151
        if(periods > 1) {
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   152
            // 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
   153
            //  - 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
   154
            //  - 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
   155
            //  - 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
   156
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   157
            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
   158
            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
   159
            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
   160
                periods += 1;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   161
                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
   162
            }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   163
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   164
            // 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
   165
            __tick+=periods-1;
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   166
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   167
            _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
   168
        }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   169
    }
ac0e6de439b5 Linux runtime: overrun detection for real-time timers and for plc execution.
Edouard Tisserant
parents: 3732
diff changeset
   170
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   171
    pthread_exit(0);
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   172
}
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   173
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   174
#define maxval(a,b) ((a>b)?a:b)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   175
int startPLC(int argc,char **argv)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   176
{
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   177
3732
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   178
    int ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   179
	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
   180
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   181
#ifdef REALTIME_LINUX
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   182
	struct sched_param param;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   183
	pthread_attr_t attr;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   184
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   185
    /* Lock memory */
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   186
    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
   187
    if(ret == -1) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   188
		_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
   189
		return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   190
    }
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   191
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   192
	/* 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
   193
	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
   194
	if (ret) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   195
		_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
   196
		return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   197
	}
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   198
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   199
	/* 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
   200
	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
   201
	if (ret) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   202
		_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
   203
		return ret;
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
	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
   206
	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
   207
	if (ret) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   208
		_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
   209
		return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   210
	}
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   211
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   212
	/* 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
   213
	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
   214
	if (ret) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   215
		_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
   216
		return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   217
	}
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   218
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   219
	pattr = &attr;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   220
#endif
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   221
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   222
    PLC_shutdown = 0;
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   223
333
e90aebdd2af1 fixed wrong pthread initialisation
lbessard
parents: 329
diff changeset
   224
    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
   225
    pthread_mutex_init(&debug_mutex, NULL);
333
e90aebdd2af1 fixed wrong pthread initialisation
lbessard
parents: 329
diff changeset
   226
    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
   227
    pthread_mutex_init(&python_mutex, NULL);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   228
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   229
    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
   230
    pthread_mutex_lock(&python_wait_mutex);
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   231
3732
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   232
    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
   233
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   234
        /* 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
   235
        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
   236
        /* 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
   237
        signal(SIGUSR2, PLCThreadSignalHandler);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   238
        /* install signal handler for manual break */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   239
        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
   240
3732
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   241
        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
   242
		if (ret) {
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   243
			_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
   244
			return ret;
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   245
		}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   246
    }else{
3732
929276eea252 Runtime: Add RealTime checkbox in Linux target options to enable PREEMPT_RT scheduling.
Edouard Tisserant
parents: 3731
diff changeset
   247
        return ret;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   248
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   249
    return 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   250
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   251
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   252
int TryEnterDebugSection(void)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   253
{
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   254
    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
   255
        /* Only enter if debug active */
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   256
        if(__DEBUG){
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   257
            return 1;
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   258
        }
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 462
diff changeset
   259
        pthread_mutex_unlock(&debug_mutex);
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   260
    }
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   261
    return 0;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   262
}
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   263
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   264
void LeaveDebugSection(void)
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   265
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   266
    pthread_mutex_unlock(&debug_mutex);
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   267
}
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   268
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   269
int stopPLC()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   270
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   271
    /* Stop the PLC */
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   272
    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
   273
    /* 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
   274
    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
   275
    pthread_join(PLC_thread, NULL);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   276
    __cleanup();
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   277
    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
   278
    pthread_mutex_destroy(&debug_mutex);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   279
    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
   280
    pthread_mutex_destroy(&python_mutex);
386
2932b0dd437c Applying patch from Iztok for old gcc versions
laurent
parents: 333
diff changeset
   281
    return 0;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   282
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   283
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
   284
extern unsigned long __tick;
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   285
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   286
int WaitDebugData(unsigned long *tick)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   287
{
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   288
    int res;
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   289
    if (PLC_shutdown) return 1;
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   290
    /* Wait signal from PLC thread */
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   291
    res = pthread_mutex_lock(&debug_wait_mutex);
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   292
    *tick = __debug_tick;
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   293
    return res;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   294
}
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   295
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   296
/* Called by PLC thread when debug_publish finished
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   297
 * This is supposed to unlock debugger thread in WaitDebugData*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   298
void InitiateDebugTransfer()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   299
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   300
    /* remember tick */
227
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 205
diff changeset
   301
    __debug_tick = __tick;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   302
    /* 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
   303
    pthread_mutex_unlock(&debug_wait_mutex);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   304
}
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   305
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 592
diff changeset
   306
int suspendDebug(int disable)
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   307
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   308
    /* Prevent PLC to enter debug code */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   309
    pthread_mutex_lock(&debug_mutex);
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   310
    /*__DEBUG is protected by this mutex */
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   311
    __DEBUG = !disable;
485
8b2da4b9d408 Bug that block SetVariableList on Linux fixed
laurent
parents: 483
diff changeset
   312
    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
   313
        pthread_mutex_unlock(&debug_mutex);
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 592
diff changeset
   314
    return 0;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   315
}
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   316
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   317
void resumeDebug(void)
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   318
{
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 280
diff changeset
   319
    __DEBUG = 1;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   320
    /* Let PLC enter debug code */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   321
    pthread_mutex_unlock(&debug_mutex);
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   322
}
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   323
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   324
/* 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
   325
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
   326
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   327
    /* Wait signal from PLC thread */
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   328
    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
   329
}
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   330
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   331
/* 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
   332
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
   333
{
3334
d7e0ddb5974b Fix wrong comment
Edouard Tisserant
parents: 3295
diff changeset
   334
    /* 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
   335
    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
   336
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   337
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   338
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
   339
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   340
    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
   341
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   342
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   343
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
   344
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   345
    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
   346
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   347
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   348
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
   349
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   350
    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
   351
}
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
   352
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
   353
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
   354
    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
   355
    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
   356
    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
   357
};
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
   358
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
   359
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
   360
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
   361
#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
   362
    {\
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   363
        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
   364
        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
   365
        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
   366
        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
   367
    }
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
   368
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
   369
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
   370
    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
   371
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   372
    if(!sig)
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   373
        _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
   374
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
   375
    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
   376
    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
   377
    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
   378
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
    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
   380
}
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
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
   383
    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
   384
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
   385
    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
   386
    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
   387
    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
   388
    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
   389
}
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
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
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
   392
    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
   393
    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
   394
    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
   395
    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
   396
    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
   397
    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
   398
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
    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
   400
        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
   401
        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
   402
        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
   403
    }
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
   404
    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
   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
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
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
   408
    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
   409
    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
   410
}
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
   411
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
   412
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
   413
    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
   414
}