targets/Win32/plc_Win32_main.c
changeset 3850 722846bd6680
parent 3731 549763a28934
equal deleted inserted replaced
3849:c3f4e114af38 3850:722846bd6680
    33 
    33 
    34 	(*CURRENT_TIME).tv_sec = timetmp.time;
    34 	(*CURRENT_TIME).tv_sec = timetmp.time;
    35 	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
    35 	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
    36 }
    36 }
    37 
    37 
    38 void PLC_timer_notify()
       
    39 {
       
    40     PLC_GetTime(&__CURRENT_TIME);
       
    41     __run();
       
    42 }
       
    43 
       
    44 HANDLE PLC_timer = NULL;
    38 HANDLE PLC_timer = NULL;
    45 void PLC_SetTimer(unsigned long long next, unsigned long long period)
    39 void PLC_SetTimer(unsigned long long next, unsigned long long period)
    46 {
    40 {
    47 	LARGE_INTEGER liDueTime;
    41 	LARGE_INTEGER liDueTime;
    48 	/* arg 2 of SetWaitableTimer take 100 ns interval*/
    42 	/* arg 2 of SetWaitableTimer take 100 ns interval*/
    63 /* Variable used to stop plcloop thread */
    57 /* Variable used to stop plcloop thread */
    64 void PlcLoop()
    58 void PlcLoop()
    65 {
    59 {
    66     PLC_shutdown = 0;
    60     PLC_shutdown = 0;
    67     while(!PLC_shutdown) {
    61     while(!PLC_shutdown) {
    68         if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
    62         if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0){
    69             PLC_shutdown = 1;
    63             PLC_shutdown = 1;
    70         PLC_timer_notify();
    64             break;
       
    65         }
       
    66         PLC_GetTime(&__CURRENT_TIME);
       
    67         __run();
    71     }
    68     }
    72 }
    69 }
    73 
    70 
    74 HANDLE PLC_thread;
    71 HANDLE PLC_thread;
    75 HANDLE debug_sem;
    72 HANDLE debug_sem;
   170     //printf("LeaveDebugSection\n");
   167     //printf("LeaveDebugSection\n");
   171 }
   168 }
   172 
   169 
   173 int stopPLC()
   170 int stopPLC()
   174 {
   171 {
       
   172  	
       
   173     PLC_shutdown = 1;
       
   174     // force last wakeup of PLC thread
       
   175     SetWaitableTimer(PLC_timer, 0, 0, NULL, NULL, 0);
       
   176     // wait end of PLC thread
       
   177     WaitForSingleObject(PLC_thread, INFINITE);
       
   178 
       
   179     __cleanup();
       
   180 
   175     CloseHandle(PLC_timer);
   181     CloseHandle(PLC_timer);
   176     WaitForSingleObject(PLC_thread, INFINITE);
       
   177     __cleanup();
       
   178     CloseHandle(debug_wait_sem);
   182     CloseHandle(debug_wait_sem);
   179     CloseHandle(debug_sem);
   183     CloseHandle(debug_sem);
   180     CloseHandle(python_wait_sem);
   184     CloseHandle(python_wait_sem);
   181     CloseHandle(python_sem);
   185     CloseHandle(python_sem);
   182     CloseHandle(PLC_thread);
   186     CloseHandle(PLC_thread);