targets/Win32/plc_Win32_main.c
author Edouard Tisserant
Mon, 04 Mar 2013 17:49:12 +0900
changeset 954 ab487d32ce9a
parent 709 fe65601bd983
child 985 cd8dadcef426
permissions -rw-r--r--
Made logging compatible with windows API
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
     1
/**
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
     2
 * Win32 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: 276
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 <sys/timeb.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 <windows.h>
568
20a223828a06 Moved locales out of platform agnostic C template
edouard
parents: 521
diff changeset
     9
#include <locale.h>
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    10
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
    11
/* provided by POUS.C */
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 484
diff changeset
    12
extern unsigned long long common_ticktime__;
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
    13
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    14
long AtomicCompareExchange(long* atomicvar, long compared, long exchange)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    15
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    16
    return InterlockedCompareExchange(atomicvar, exchange, compared);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    17
}
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    18
CRITICAL_SECTION Atomic64CS; 
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    19
long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange)
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    20
{
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    21
    long long res;
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    22
    EnterCriticalSection(&Atomic64CS);
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    23
    res=*atomicvar;
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    24
    if(*atomicvar == compared){
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    25
        *atomicvar = exchange;
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    26
    }
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    27
    LeaveCriticalSection(&Atomic64CS);
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    28
    return res;
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    29
}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    30
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    31
struct _timeb timetmp;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    32
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
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
	_ftime(&timetmp);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
    35
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    36
	(*CURRENT_TIME).tv_sec = timetmp.time;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    37
	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
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
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    40
void PLC_timer_notify()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    41
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    42
    PLC_GetTime(&__CURRENT_TIME);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    43
    __run();
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
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    46
HANDLE PLC_timer = NULL;
518
8e61b0066859 Fixed confusion about __common_ticktime type, redesigned LPC PLC timer support
edouard
parents: 484
diff changeset
    47
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
    48
{
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    49
	LARGE_INTEGER liDueTime;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    50
	/* arg 2 of SetWaitableTimer take 100 ns interval*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    51
	liDueTime.QuadPart =  next / (-100);
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
    52
685
2db8b87016a0 Fixed Win32 timer up to 1ms
Edouard Tisserant
parents: 681
diff changeset
    53
	if (!SetWaitableTimer(PLC_timer, &liDueTime, period<1000000?1:period/1000000, NULL, NULL, 0))
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    54
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    55
        printf("SetWaitableTimer failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    56
    }
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    57
}
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    58
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    59
/* Variable used to stop plcloop thread */
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    60
void PlcLoop()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    61
{
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    62
    while(WaitForSingleObject(PLC_timer, INFINITE) == WAIT_OBJECT_0)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    63
    {
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    64
        PLC_timer_notify();
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    65
    }
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    66
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    67
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    68
HANDLE PLC_thread;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    69
HANDLE debug_sem;
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
    70
HANDLE debug_wait_sem;
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
    71
HANDLE python_sem;
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
    72
HANDLE python_wait_sem;
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
    73
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
    74
#define maxval(a,b) ((a>b)?a:b)
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    75
int startPLC(int argc,char **argv)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    76
{
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    77
	unsigned long thread_id = 0;
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    78
    BOOL tmp;
568
20a223828a06 Moved locales out of platform agnostic C template
edouard
parents: 521
diff changeset
    79
    setlocale(LC_NUMERIC, "C");
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 329
diff changeset
    80
	/* Define Ttick to 1ms if common_ticktime not defined */
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 329
diff changeset
    81
    Ttick = common_ticktime__?common_ticktime__:1000000;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    82
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    83
    InitializeCriticalSection(&Atomic64CS);
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
    84
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    85
    debug_sem = CreateSemaphore(
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    86
                            NULL,           // default security attributes
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    87
                            1,  			// initial count
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    88
                            1,  			// maximum count
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    89
                            NULL);          // unnamed semaphore
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
    90
    if (debug_sem == NULL)
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    91
    {
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
    92
        printf("startPLC CreateSemaphore debug_sem error: %d\n", GetLastError());
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    93
        return;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    94
    }
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
    95
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    96
    debug_wait_sem = CreateSemaphore(
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    97
                            NULL,           // default security attributes
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    98
                            0,  			// initial count
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
    99
                            1,  			// maximum count
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   100
                            NULL);          // unnamed semaphore
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   101
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   102
    if (debug_wait_sem == NULL)
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   103
    {
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   104
        printf("startPLC CreateSemaphore debug_wait_sem error: %d\n", GetLastError());
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   105
        return;
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   106
    }
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   107
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   108
    python_sem = CreateSemaphore(
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   109
                            NULL,           // default security attributes
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   110
                            1,  			// initial count
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   111
                            1,  			// maximum count
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   112
                            NULL);          // unnamed semaphore
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   113
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   114
    if (python_sem == NULL)
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   115
    {
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   116
        printf("startPLC CreateSemaphore python_sem error: %d\n", GetLastError());
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   117
        return;
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   118
    }
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   119
    python_wait_sem = CreateSemaphore(
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   120
                            NULL,           // default security attributes
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   121
                            0,  			// initial count
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   122
                            1,  			// maximum count
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   123
                            NULL);          // unnamed semaphore
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   124
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   125
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   126
    if (python_wait_sem == NULL)
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   127
    {
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   128
        printf("startPLC CreateSemaphore python_wait_sem error: %d\n", GetLastError());
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   129
        return;
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   130
    }
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   131
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   132
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   133
    /* Create a waitable timer */
685
2db8b87016a0 Fixed Win32 timer up to 1ms
Edouard Tisserant
parents: 681
diff changeset
   134
    timeBeginPeriod(1);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   135
    PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   136
    if(NULL == PLC_timer)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   137
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   138
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   139
        return 1;
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
    if( __init(argc,argv) == 0 )
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   142
    {
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   143
        PLC_SetTimer(Ttick,Ttick);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   144
        PLC_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlcLoop, NULL, 0, &thread_id);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   145
    }
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   146
    else{
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   147
        return 1;
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   148
    }
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   149
    return 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   150
}
397
6a7ff66a811d Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents: 329
diff changeset
   151
static unsigned long __debug_tick;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   152
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   153
int TryEnterDebugSection(void)
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   154
{
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   155
	//printf("TryEnterDebugSection\n");
469
a6a9b59636d8 Propagated new runtime changes to win32 target
edouard
parents: 446
diff changeset
   156
    if(WaitForSingleObject(debug_sem, 0) == WAIT_OBJECT_0){
a6a9b59636d8 Propagated new runtime changes to win32 target
edouard
parents: 446
diff changeset
   157
        /* Only enter if debug active */
a6a9b59636d8 Propagated new runtime changes to win32 target
edouard
parents: 446
diff changeset
   158
        if(__DEBUG){
a6a9b59636d8 Propagated new runtime changes to win32 target
edouard
parents: 446
diff changeset
   159
            return 1;
a6a9b59636d8 Propagated new runtime changes to win32 target
edouard
parents: 446
diff changeset
   160
        }
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   161
        ReleaseSemaphore(debug_sem, 1, NULL);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   162
    }
469
a6a9b59636d8 Propagated new runtime changes to win32 target
edouard
parents: 446
diff changeset
   163
    return 0;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   164
}
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   165
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   166
void LeaveDebugSection(void)
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   167
{
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   168
	ReleaseSemaphore(debug_sem, 1, NULL);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   169
    //printf("LeaveDebugSection\n");
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   170
}
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   171
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   172
int stopPLC()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   173
{
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   174
    CloseHandle(PLC_timer);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   175
    WaitForSingleObject(PLC_thread, INFINITE);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   176
    __cleanup();
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 709
diff changeset
   177
    DeleteCriticalSection(&Atomic64CS);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   178
    CloseHandle(debug_wait_sem);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   179
    CloseHandle(debug_sem);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   180
    CloseHandle(python_wait_sem);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   181
    CloseHandle(python_sem);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   182
    CloseHandle(PLC_thread);
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   183
}
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   184
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   185
/* from plc_debugger.c */
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   186
int WaitDebugData(unsigned long *tick)
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   187
{
709
fe65601bd983 Fixing bug in debugger sending wrong tick with values
laurent
parents: 685
diff changeset
   188
	DWORD res;
fe65601bd983 Fixing bug in debugger sending wrong tick with values
laurent
parents: 685
diff changeset
   189
	res = WaitForSingleObject(debug_wait_sem, INFINITE);
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   190
    *tick = __debug_tick;
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 397
diff changeset
   191
    /* Wait signal from PLC thread */
709
fe65601bd983 Fixing bug in debugger sending wrong tick with values
laurent
parents: 685
diff changeset
   192
	return res != WAIT_OBJECT_0;
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   193
}
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   194
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   195
/* Called by PLC thread when debug_publish finished
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   196
 * This is supposed to unlock debugger thread in WaitDebugData*/
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   197
void InitiateDebugTransfer()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   198
{
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   199
    /* remember tick */
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   200
    __debug_tick = __tick;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   201
    /* 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: 276
diff changeset
   202
    ReleaseSemaphore(debug_wait_sem, 1, NULL);
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   203
}
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   204
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 580
diff changeset
   205
int suspendDebug(int disable)
469
a6a9b59636d8 Propagated new runtime changes to win32 target
edouard
parents: 446
diff changeset
   206
{
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   207
    /* Prevent PLC to enter debug code */
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   208
    WaitForSingleObject(debug_sem, INFINITE);
469
a6a9b59636d8 Propagated new runtime changes to win32 target
edouard
parents: 446
diff changeset
   209
    __DEBUG = !disable;
484
3d0c06e2648c Fixed freeze on debug suspend.
Lolitech
parents: 483
diff changeset
   210
    if(disable)
3d0c06e2648c Fixed freeze on debug suspend.
Lolitech
parents: 483
diff changeset
   211
        ReleaseSemaphore(debug_sem, 1, NULL);
614
eed1dcf311a1 added return type to suspendDebug
Edouard Tisserant
parents: 580
diff changeset
   212
    return 0;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   213
}
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   214
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   215
void resumeDebug()
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   216
{
290
3bd617ae7a05 Local Runtime (LOCAL://) now launched "on demand"
etisserant
parents: 280
diff changeset
   217
	__DEBUG = 1;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   218
    /* Let PLC enter debug code */
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   219
	ReleaseSemaphore(debug_sem, 1, NULL);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   220
}
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   221
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   222
/* from plc_python.c */
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   223
int WaitPythonCommands(void)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   224
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   225
    /* Wait signal from PLC thread */
329
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   226
	return WaitForSingleObject(python_wait_sem, INFINITE);
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   227
}
22e65b8e20f4 add autostart plc feature for beremiz_service
greg
parents: 290
diff changeset
   228
280
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   229
/* 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: 276
diff changeset
   230
void UnBlockPythonCommands(void)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   231
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   232
    /* 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: 276
diff changeset
   233
	ReleaseSemaphore(python_wait_sem, 1, NULL);
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   234
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   235
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   236
int TryLockPython(void)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   237
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   238
	return WaitForSingleObject(python_sem, 0) == WAIT_OBJECT_0;
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   239
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   240
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   241
void UnLockPython(void)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   242
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   243
	ReleaseSemaphore(python_sem, 1, NULL);
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   244
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   245
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   246
void LockPython(void)
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   247
{
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   248
	WaitForSingleObject(python_sem, INFINITE);
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   249
}
f2ef79f3dba0 Added native (not a plugin) asynchronous python eval function block - Beta. Code cleanup in C code templates.
etisserant
parents: 276
diff changeset
   250
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 518
diff changeset
   251
int CheckRetainBuffer(void)
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 518
diff changeset
   252
{
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 518
diff changeset
   253
	return 1;
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 518
diff changeset
   254
}
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 518
diff changeset
   255
580
9dd978e6537c More robust retain buffer validity management
edouard
parents: 568
diff changeset
   256
void ValidateRetainBuffer(void)
9dd978e6537c More robust retain buffer validity management
edouard
parents: 568
diff changeset
   257
{
9dd978e6537c More robust retain buffer validity management
edouard
parents: 568
diff changeset
   258
}
9dd978e6537c More robust retain buffer validity management
edouard
parents: 568
diff changeset
   259
9dd978e6537c More robust retain buffer validity management
edouard
parents: 568
diff changeset
   260
void InValidateRetainBuffer(void)
9dd978e6537c More robust retain buffer validity management
edouard
parents: 568
diff changeset
   261
{
9dd978e6537c More robust retain buffer validity management
edouard
parents: 568
diff changeset
   262
}
9dd978e6537c More robust retain buffer validity management
edouard
parents: 568
diff changeset
   263
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   264
void Retain(unsigned int offset, unsigned int count, void * p)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   265
{
681
383864958dac commented out noisy printf in Win32 target
Edouard Tisserant
parents: 614
diff changeset
   266
    /*
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   267
    unsigned int position;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   268
    for(position=0; position<count; position++ ){
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   269
        printf("%d : 0x%2.2x\n", offset+position, ((char*)p)[position]);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   270
    }
681
383864958dac commented out noisy printf in Win32 target
Edouard Tisserant
parents: 614
diff changeset
   271
    */
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   272
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   273
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   274
void Remind(unsigned int offset, unsigned int count, void *p)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   275
{
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   276
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 469
diff changeset
   277