targets/Win32/plc_Win32_main.c
changeset 244 85e92d9e34a8
parent 229 8bc65076e290
child 247 655d5fef0204
equal deleted inserted replaced
243:90db933fe956 244:85e92d9e34a8
     1 #include <stdio.h>
     1 #include <stdio.h>
     2 #include <sys/timeb.h>
     2 #include <sys/timeb.h>
     3 #include <time.h>
     3 #include <time.h>
     4 #include <windows.h>
     4 #include <windows.h>
     5 
     5 
     6 long AtomicCompareExchange(long* atomicvar,long exchange, long compared)
     6 long AtomicCompareExchange(long* atomicvar, long compared, long exchange)
     7 {
     7 {
     8     return InterlockedCompareExchange(atomicvar, exchange, compared);
     8     return InterlockedCompareExchange(atomicvar, exchange, compared);
     9 }
     9 }
    10 
    10 
    11 //long AtomicExchange(long* atomicvar,long exchange)
    11 //long AtomicExchange(long* atomicvar,long exchange)
    60 	}
    60 	}
    61 	PLC_timer_notify();
    61 	PLC_timer_notify();
    62 	}
    62 	}
    63 }
    63 }
    64 
    64 
    65 HANDLE DebugLock;
       
    66 HANDLE PLC_thread;
    65 HANDLE PLC_thread;
       
    66 HANDLE debug_sem;
       
    67 HANDLE wait_sem; 
       
    68 #define MAX_SEM_COUNT 10
    67 
    69 
    68 int startPLC(int argc,char **argv)
    70 int startPLC(int argc,char **argv)
    69 {
    71 {
    70 	unsigned long thread_id = 0;
    72 	unsigned long thread_id = 0;
    71 	/* Translate PLC's microseconds to Ttick nanoseconds */
    73 	/* Translate PLC's microseconds to Ttick nanoseconds */
    72 	Ttick = 1000000 * maxval(common_ticktime__,1);
    74 	Ttick = 1000000 * maxval(common_ticktime__,1);
    73 
    75 
    74 	DebugLock = CreateMutex( 
    76 	debug_sem = CreateSemaphore( 
    75 	        	NULL,              // default security attributes
    77 							NULL,           // default security attributes
    76 	        	FALSE,             // initially not owned
    78 					        1,  			// initial count
    77 	        	NULL);             // unnamed mutex
    79 					        MAX_SEM_COUNT,  // maximum count
       
    80 					        NULL);          // unnamed semaphore
       
    81     if (debug_sem == NULL) 
       
    82     {
       
    83         printf("CreateMutex error: %d\n", GetLastError());
       
    84         return;
       
    85     }
       
    86     
       
    87 	wait_sem = CreateSemaphore( 
       
    88 					        NULL,           // default security attributes
       
    89 					        0,  			// initial count
       
    90 					        MAX_SEM_COUNT,  // maximum count
       
    91 					        NULL);          // unnamed semaphore
    78 
    92 
    79     if (DebugLock == NULL) 
    93     if (wait_sem == NULL) 
    80     {
    94     {
    81         printf("CreateMutex error: %d\n", GetLastError());
    95         printf("CreateMutex error: %d\n", GetLastError());
    82         return;
    96         return;
    83     }
    97     }
    84 	
    98 	
    97     else{
   111     else{
    98     	return 1;
   112     	return 1;
    99     }
   113     }
   100     return 0;
   114     return 0;
   101 }
   115 }
       
   116 static int __debug_tick;
       
   117 
       
   118 int TryEnterDebugSection(void)
       
   119 {
       
   120 	//printf("TryEnterDebugSection\n");
       
   121 	return WaitForSingleObject(debug_sem, 0) == WAIT_OBJECT_0;
       
   122 }
       
   123 
       
   124 void LeaveDebugSection(void)
       
   125 {
       
   126 	ReleaseSemaphore(debug_sem, 1, NULL);
       
   127     //printf("LeaveDebugSection\n");
       
   128 }
   102 
   129 
   103 int stopPLC()
   130 int stopPLC()
   104 {
   131 {
   105 	runplcloop = 0;
   132 	runplcloop = 0;
   106 	WaitForSingleObject(PLC_thread, INFINITE);
   133 	WaitForSingleObject(PLC_thread, INFINITE);
       
   134 	__cleanup();
       
   135 	__debug_tick = -1;
       
   136 	ReleaseSemaphore(wait_sem, 1, NULL);
       
   137 	CloseHandle(debug_sem);
       
   138 	CloseHandle(wait_sem);
       
   139 	CloseHandle(PLC_timer);
   107 	CloseHandle(PLC_thread);
   140 	CloseHandle(PLC_thread);
   108 	CloseHandle(DebugLock);
       
   109     CloseHandle(PLC_timer);
       
   110     __cleanup();
       
   111 }
   141 }
   112 
   142 
   113 /* from plc_debugger.c */
   143 /* from plc_debugger.c */
   114 void WaitDebugData()
   144 int WaitDebugData()
   115 {
   145 {
   116 	DWORD dwWaitResult;
   146 	WaitForSingleObject(wait_sem, INFINITE);
   117 	dwWaitResult = WaitForSingleObject( 
   147 	return __debug_tick;
   118 					DebugLock,  // handle to mutex
       
   119 					INFINITE);  // no time-out interval
       
   120 }
   148 }
   121  
   149  
   122 /* Called by PLC thread when debug_publish finished
   150 /* Called by PLC thread when debug_publish finished
   123  * This is supposed to unlock debugger thread in WaitDebugData*/
   151  * This is supposed to unlock debugger thread in WaitDebugData*/
   124 void InitiateDebugTransfer()
   152 void InitiateDebugTransfer()
   125 {
   153 {
   126     /* signal debugger thread to continue*/
   154 	/* Leave debugger section */
   127 	ReleaseMutex(DebugLock);
   155 	ReleaseSemaphore(debug_sem, 1, NULL);
       
   156     /* remember tick */
       
   157     __debug_tick = __tick;
       
   158     /* signal debugger thread it can read data */
       
   159     ReleaseSemaphore(wait_sem, 1, NULL);
   128 }
   160 }
       
   161 
       
   162 void suspendDebug()
       
   163 {
       
   164     /* Prevent PLC to enter debug code */
       
   165 	WaitForSingleObject(debug_sem, INFINITE);  
       
   166 }
       
   167 
       
   168 void resumeDebug()
       
   169 {
       
   170     /* Let PLC enter debug code */
       
   171 	ReleaseSemaphore(debug_sem, 1, NULL);
       
   172 }