targets/Linux/plc_Linux_main.c
author Edouard Tisserant
Tue, 21 Feb 2023 11:38:45 +0100
changeset 3731 549763a28934
parent 3727 265fc8001d0a
child 3732 929276eea252
permissions -rw-r--r--
Runtime: move LC_NUMERIC locale setting to Beremiz_service.py.
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>
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    14
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    15
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
    16
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    17
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
    18
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
    19
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
    20
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
    21
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
    22
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    23
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
    24
236
a32817e81f5e Now debug all ticks, not only odd ones :-)
etisserant
parents: 235
diff changeset
    25
long AtomicCompareExchange(long* atomicvar,long compared, long exchange)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    26
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    27
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    28
}
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 876
diff changeset
    29
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
    30
{
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 876
diff changeset
    31
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 876
diff changeset
    32
}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    33
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    34
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    35
{
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    36
    struct timespec tmp;
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    37
    clock_gettime(CLOCK_REALTIME, &tmp);
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    38
    CURRENT_TIME->tv_sec = tmp.tv_sec;
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    39
    CURRENT_TIME->tv_nsec = tmp.tv_nsec;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    40
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    41
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    42
static long long period_ns = 0;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    43
struct timespec next_abs_time;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    44
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    45
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
    46
{
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    47
    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
    48
#ifdef __lldiv_t_defined
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    49
    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
    50
    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
    51
    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
    52
#else
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    53
    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
    54
    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
    55
#endif
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    56
}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    57
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 485
diff changeset
    58
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
    59
{
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    60
    /*
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    61
    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
    62
    */
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    63
    period_ns = period;
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    64
    clock_gettime(CLOCK_MONOTONIC, &next_abs_time);
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    65
    inc_timespec(&next_abs_time, next);
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    66
    // 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
    67
    pthread_kill(PLC_thread, SIGUSR1);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    68
}
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    69
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    70
void catch_signal(int sig)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    71
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    72
//  signal(SIGTERM, catch_signal);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    73
  signal(SIGINT, catch_signal);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    74
  printf("Got Signal %d\n",sig);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    75
  exit(0);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    76
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    77
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    78
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
    79
{
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    80
    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
    81
        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
    82
}
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    83
2173
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    84
int ForceSaveRetainReq(void) {
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    85
    return PLC_shutdown;
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    86
}
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    87
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    88
void PLC_thread_proc(void *arg)
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    89
{
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    90
    while (!PLC_shutdown) {
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    91
        // Sleep until next PLC run
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    92
        // TODO check result of clock_nanosleep and wait again or exit eventually
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
    93
        int res = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_abs_time, NULL);
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    94
        if(res==EINTR){
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    95
            continue;
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    96
        }
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    97
        if(res!=0){
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    98
            printf("PLC thread died with error %d \n", res);
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
    99
            return;
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   100
        }
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   101
        PLC_GetTime(&__CURRENT_TIME);
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   102
        __run();
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   103
        inc_timespec(&next_abs_time, period_ns);
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   104
    }
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   105
    pthread_exit(0);
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   106
}
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   107
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   108
#define maxval(a,b) ((a>b)?a:b)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   109
int startPLC(int argc,char **argv)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   110
{
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   111
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   112
    PLC_shutdown = 0;
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   113
333
e90aebdd2af1 fixed wrong pthread initialisation
lbessard
parents: 329
diff changeset
   114
    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
   115
    pthread_mutex_init(&debug_mutex, NULL);
333
e90aebdd2af1 fixed wrong pthread initialisation
lbessard
parents: 329
diff changeset
   116
    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
   117
    pthread_mutex_init(&python_mutex, NULL);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   118
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   119
    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
   120
    pthread_mutex_lock(&python_wait_mutex);
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   121
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   122
    if(  __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
   123
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   124
        /* 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
   125
        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
   126
        /* 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
   127
        signal(SIGUSR2, PLCThreadSignalHandler);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   128
        /* install signal handler for manual break */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   129
        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
   130
3727
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   131
        /* initialize next occurence and period */
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   132
        period_ns = common_ticktime__;
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   133
        clock_gettime(CLOCK_MONOTONIC, &next_abs_time);
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   134
265fc8001d0a Runtime: Fix bugs in Linux target.
Edouard Tisserant
parents: 3726
diff changeset
   135
        pthread_create(&PLC_thread, NULL, (void*) &PLC_thread_proc, NULL);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   136
    }else{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   137
        return 1;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   138
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   139
    return 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   140
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   141
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   142
int TryEnterDebugSection(void)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   143
{
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   144
    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
   145
        /* Only enter if debug active */
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   146
        if(__DEBUG){
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   147
            return 1;
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   148
        }
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 462
diff changeset
   149
        pthread_mutex_unlock(&debug_mutex);
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   150
    }
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   151
    return 0;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   152
}
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   153
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   154
void LeaveDebugSection(void)
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   155
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   156
    pthread_mutex_unlock(&debug_mutex);
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   157
}
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   158
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   159
int stopPLC()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   160
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   161
    /* Stop the PLC */
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   162
    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
   163
    /* 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
   164
    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
   165
    pthread_join(PLC_thread, NULL);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   166
    __cleanup();
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   167
    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
   168
    pthread_mutex_destroy(&debug_mutex);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   169
    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
   170
    pthread_mutex_destroy(&python_mutex);
386
2932b0dd437c Applying patch from Iztok for old gcc versions
laurent
parents: 333
diff changeset
   171
    return 0;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   172
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   173
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
   174
extern unsigned long __tick;
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   175
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   176
int WaitDebugData(unsigned long *tick)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   177
{
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   178
    int res;
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   179
    if (PLC_shutdown) return 1;
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   180
    /* Wait signal from PLC thread */
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   181
    res = pthread_mutex_lock(&debug_wait_mutex);
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   182
    *tick = __debug_tick;
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   183
    return res;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   184
}
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   185
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   186
/* Called by PLC thread when debug_publish finished
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   187
 * This is supposed to unlock debugger thread in WaitDebugData*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   188
void InitiateDebugTransfer()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   189
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   190
    /* remember tick */
227
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 205
diff changeset
   191
    __debug_tick = __tick;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   192
    /* 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
   193
    pthread_mutex_unlock(&debug_wait_mutex);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   194
}
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   195
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 592
diff changeset
   196
int suspendDebug(int disable)
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   197
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   198
    /* Prevent PLC to enter debug code */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   199
    pthread_mutex_lock(&debug_mutex);
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   200
    /*__DEBUG is protected by this mutex */
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   201
    __DEBUG = !disable;
485
8b2da4b9d408 Bug that block SetVariableList on Linux fixed
laurent
parents: 483
diff changeset
   202
    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
   203
        pthread_mutex_unlock(&debug_mutex);
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 592
diff changeset
   204
    return 0;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   205
}
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   206
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   207
void resumeDebug(void)
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   208
{
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 280
diff changeset
   209
    __DEBUG = 1;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   210
    /* Let PLC enter debug code */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   211
    pthread_mutex_unlock(&debug_mutex);
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   212
}
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   213
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   214
/* 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
   215
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
   216
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   217
    /* Wait signal from PLC thread */
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   218
    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
   219
}
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   220
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   221
/* 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
   222
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
   223
{
3334
d7e0ddb5974b Fix wrong comment
Edouard Tisserant
parents: 3295
diff changeset
   224
    /* 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
   225
    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
   226
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   227
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   228
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
   229
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   230
    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
   231
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   232
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   233
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
   234
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   235
    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
   236
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   237
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   238
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
   239
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   240
    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
   241
}
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
   242
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
   243
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
   244
    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
   245
    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
   246
    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
   247
};
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
   248
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
   249
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
   250
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
   251
#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
   252
    {\
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   253
        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
   254
        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
   255
        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
   256
        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
   257
    }
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
   258
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
   259
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
   260
    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
   261
3726
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   262
    if(!sig)
516779f11803 runtime: Change Linux target to use clock_nanosleep instead of timer (better rt-preempt perf).
Edouard Tisserant
parents: 3725
diff changeset
   263
        _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
   264
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
   265
    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
   266
    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
   267
    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
   268
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
   269
    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
   270
}
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
   271
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
   272
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
   273
    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
   274
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
   275
    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
   276
    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
   277
    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
   278
    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
   279
}
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
   280
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
   281
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
   282
    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
   283
    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
   284
    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
   285
    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
   286
    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
   287
    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
   288
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
   289
    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
   290
        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
   291
        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
   292
        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
   293
    }
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
   294
    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
   295
}
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
   296
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
   297
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
   298
    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
   299
    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
   300
}
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
   301
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
   302
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
   303
    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
   304
}