targets/Linux/plc_Linux_main.c
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Tue, 02 Oct 2018 16:53:14 +0300
changeset 2416 1ca207782dde
parent 2173 976841968d74
child 2820 d9b5303d43dc
permissions -rw-r--r--
Use predefined constants for PlcStatus instead of string literals
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>
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
    10
#include <pthread.h>
568
20a223828a06 Moved locales out of platform agnostic C template
edouard
parents: 522
diff changeset
    11
#include <locale.h>
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    12
#include <semaphore.h>
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    13
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    14
static sem_t Run_PLC;
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    15
236
a32817e81f5e Now debug all ticks, not only odd ones :-)
etisserant
parents: 235
diff changeset
    16
long AtomicCompareExchange(long* atomicvar,long compared, long exchange)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    17
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    18
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    19
}
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 876
diff changeset
    20
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
    21
{
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 876
diff changeset
    22
    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 876
diff changeset
    23
}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    24
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    25
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    26
{
592
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    27
    struct timespec tmp;
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    28
    clock_gettime(CLOCK_REALTIME, &tmp);
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    29
    CURRENT_TIME->tv_sec = tmp.tv_sec;
c6408f92da0a Initial TIME support in debugger
Edouard Tisserant
parents: 580
diff changeset
    30
    CURRENT_TIME->tv_nsec = tmp.tv_nsec;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    31
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    32
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    33
void PLC_timer_notify(sigval_t val)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    34
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    35
    PLC_GetTime(&__CURRENT_TIME);
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    36
    sem_post(&Run_PLC);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    37
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    38
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    39
timer_t PLC_timer;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    40
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 485
diff changeset
    41
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
    42
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    43
    struct itimerspec timerValues;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    44
	/*
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    45
	printf("SetTimer(%lld,%lld)\n",next, period);
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
    memset (&timerValues, 0, sizeof (struct itimerspec));
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    48
	{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    49
#ifdef __lldiv_t_defined
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    50
		lldiv_t nxt_div = lldiv(next, 1000000000);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    51
		lldiv_t period_div = lldiv(period, 1000000000);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    52
	    timerValues.it_value.tv_sec = nxt_div.quot;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    53
	    timerValues.it_value.tv_nsec = nxt_div.rem;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    54
	    timerValues.it_interval.tv_sec = period_div.quot;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    55
	    timerValues.it_interval.tv_nsec = period_div.rem;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    56
#else
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    57
	    timerValues.it_value.tv_sec = next / 1000000000;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    58
	    timerValues.it_value.tv_nsec = next % 1000000000;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    59
	    timerValues.it_interval.tv_sec = period / 1000000000;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    60
	    timerValues.it_interval.tv_nsec = period % 1000000000;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    61
#endif
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
    62
	}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    63
    timer_settime (PLC_timer, 0, &timerValues, NULL);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    64
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    65
//
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    66
void catch_signal(int sig)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    67
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    68
//  signal(SIGTERM, catch_signal);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    69
  signal(SIGINT, catch_signal);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    70
  printf("Got Signal %d\n",sig);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    71
  exit(0);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    72
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    73
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
    74
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
    75
static unsigned long __debug_tick;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
    76
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    77
pthread_t PLC_thread;
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    78
static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    79
static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER;
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    80
static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
    81
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
    82
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    83
int PLC_shutdown = 0;
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    84
2173
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    85
int ForceSaveRetainReq(void) {
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    86
    return PLC_shutdown;
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    87
}
976841968d74 Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2171
diff changeset
    88
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    89
void PLC_thread_proc(void *arg)
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    90
{
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    91
    while (!PLC_shutdown) {
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    92
        sem_wait(&Run_PLC);
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    93
        __run();
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    94
    }
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    95
    pthread_exit(0);
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    96
}
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
    97
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
    98
#define maxval(a,b) ((a>b)?a:b)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    99
int startPLC(int argc,char **argv)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   100
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   101
    struct sigevent sigev;
568
20a223828a06 Moved locales out of platform agnostic C template
edouard
parents: 522
diff changeset
   102
    setlocale(LC_NUMERIC, "C");
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   103
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   104
    PLC_shutdown = 0;
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   105
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   106
    sem_init(&Run_PLC, 0, 0);
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   107
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   108
    pthread_create(&PLC_thread, NULL, (void*) &PLC_thread_proc, NULL);
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   109
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   110
    memset (&sigev, 0, sizeof (struct sigevent));
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   111
    sigev.sigev_value.sival_int = 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   112
    sigev.sigev_notify = SIGEV_THREAD;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   113
    sigev.sigev_notify_attributes = NULL;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   114
    sigev.sigev_notify_function = PLC_timer_notify;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   115
333
e90aebdd2af1 fixed wrong pthread initialisation
lbessard
parents: 329
diff changeset
   116
    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
   117
    pthread_mutex_init(&debug_mutex, NULL);
333
e90aebdd2af1 fixed wrong pthread initialisation
lbessard
parents: 329
diff changeset
   118
    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
   119
    pthread_mutex_init(&python_mutex, NULL);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   120
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   121
    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
   122
    pthread_mutex_lock(&python_wait_mutex);
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   123
2171
d1536c271866 use CLOCK_MONOTONIC instead of CLOCK_REALTIME for timer setup
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1903
diff changeset
   124
    timer_create (CLOCK_MONOTONIC, &sigev, &PLC_timer);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   125
    if(  __init(argc,argv) == 0 ){
1428
e14003eb4d42 Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents: 985
diff changeset
   126
        PLC_SetTimer(common_ticktime__,common_ticktime__);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   127
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);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   130
    }else{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   131
        return 1;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   132
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   133
    return 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   134
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   135
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   136
int TryEnterDebugSection(void)
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   137
{
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   138
    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
   139
        /* Only enter if debug active */
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   140
        if(__DEBUG){
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   141
            return 1;
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   142
        }
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 462
diff changeset
   143
        pthread_mutex_unlock(&debug_mutex);
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   144
    }
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   145
    return 0;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   146
}
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   147
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   148
void LeaveDebugSection(void)
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   149
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   150
    pthread_mutex_unlock(&debug_mutex);
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   151
}
a66e150f2888 Improved debug data feedback.
etisserant
parents: 227
diff changeset
   152
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   153
int stopPLC()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   154
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   155
    /* Stop the PLC */
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   156
    PLC_shutdown = 1;
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   157
    sem_post(&Run_PLC);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   158
    PLC_SetTimer(0,0);
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   159
	pthread_join(PLC_thread, NULL);
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   160
	sem_destroy(&Run_PLC);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   161
    timer_delete (PLC_timer);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   162
    __cleanup();
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   163
    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
   164
    pthread_mutex_destroy(&debug_mutex);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   165
    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
   166
    pthread_mutex_destroy(&python_mutex);
386
2932b0dd437c Applying patch from Iztok for old gcc versions
laurent
parents: 333
diff changeset
   167
    return 0;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   168
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   169
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
   170
extern unsigned long __tick;
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   171
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   172
int WaitDebugData(unsigned long *tick)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   173
{
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   174
    int res;
876
179d5c455f29 Fix bug python thread blocked sometimes on Linux
Laurent Bessard
parents: 617
diff changeset
   175
    if (PLC_shutdown) return 1;
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   176
    /* Wait signal from PLC thread */
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   177
    res = pthread_mutex_lock(&debug_wait_mutex);
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   178
    *tick = __debug_tick;
617
7c23fac40a2a fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents: 614
diff changeset
   179
    return res;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   180
}
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   181
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   182
/* Called by PLC thread when debug_publish finished
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   183
 * This is supposed to unlock debugger thread in WaitDebugData*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   184
void InitiateDebugTransfer()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   185
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   186
    /* remember tick */
227
48c13b84505c - Some improovements in debug data feedback data
etisserant
parents: 205
diff changeset
   187
    __debug_tick = __tick;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   188
    /* 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
   189
    pthread_mutex_unlock(&debug_wait_mutex);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   190
}
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   191
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 592
diff changeset
   192
int suspendDebug(int disable)
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   193
{
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   194
    /* Prevent PLC to enter debug code */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   195
    pthread_mutex_lock(&debug_mutex);
462
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   196
    /*__DEBUG is protected by this mutex */
274e83a5534e Now debug is not a button anymore
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   197
    __DEBUG = !disable;
485
8b2da4b9d408 Bug that block SetVariableList on Linux fixed
laurent
parents: 483
diff changeset
   198
    if (disable)
8b2da4b9d408 Bug that block SetVariableList on Linux fixed
laurent
parents: 483
diff changeset
   199
    	pthread_mutex_unlock(&debug_mutex);
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 592
diff changeset
   200
    return 0;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   201
}
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   202
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   203
void resumeDebug(void)
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   204
{
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 280
diff changeset
   205
    __DEBUG = 1;
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   206
    /* Let PLC enter debug code */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   207
    pthread_mutex_unlock(&debug_mutex);
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   208
}
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   209
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   210
/* 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
   211
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
   212
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   213
    /* Wait signal from PLC thread */
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   214
    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
   215
}
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   216
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   217
/* 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
   218
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
   219
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   220
    /* signal debugger thread it can read data */
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   221
    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
   222
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   223
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   224
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
   225
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   226
    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
   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
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   229
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
   230
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   231
    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
   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
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   234
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
   235
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 245
diff changeset
   236
    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
   237
}