Made logging compatible with windows API
authorEdouard Tisserant
Mon, 04 Mar 2013 17:49:12 +0900
changeset 954 ab487d32ce9a
parent 953 1fb6cf5a4c4d
child 955 7ef2f485c28f
Made logging compatible with windows API
targets/Linux/plc_Linux_main.c
targets/Win32/plc_Win32_main.c
targets/Xenomai/plc_Xenomai_main.c
targets/plc_debug.c
--- a/targets/Linux/plc_Linux_main.c	Mon Mar 04 12:22:49 2013 +0900
+++ b/targets/Linux/plc_Linux_main.c	Mon Mar 04 17:49:12 2013 +0900
@@ -18,6 +18,10 @@
 {
     return __sync_val_compare_and_swap(atomicvar, compared, exchange);
 }
+long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange)
+{
+    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
+}
 
 void PLC_GetTime(IEC_TIME *CURRENT_TIME)
 {
--- a/targets/Win32/plc_Win32_main.c	Mon Mar 04 12:22:49 2013 +0900
+++ b/targets/Win32/plc_Win32_main.c	Mon Mar 04 17:49:12 2013 +0900
@@ -15,6 +15,18 @@
 {
     return InterlockedCompareExchange(atomicvar, exchange, compared);
 }
+CRITICAL_SECTION Atomic64CS; 
+long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange)
+{
+    long long res;
+    EnterCriticalSection(&Atomic64CS);
+    res=*atomicvar;
+    if(*atomicvar == compared){
+        *atomicvar = exchange;
+    }
+    LeaveCriticalSection(&Atomic64CS);
+    return res;
+}
 
 struct _timeb timetmp;
 void PLC_GetTime(IEC_TIME *CURRENT_TIME)
@@ -63,10 +75,13 @@
 int startPLC(int argc,char **argv)
 {
 	unsigned long thread_id = 0;
+    BOOL tmp;
     setlocale(LC_NUMERIC, "C");
 	/* Define Ttick to 1ms if common_ticktime not defined */
     Ttick = common_ticktime__?common_ticktime__:1000000;
 
+    InitializeCriticalSection(&Atomic64CS);
+
     debug_sem = CreateSemaphore(
                             NULL,           // default security attributes
                             1,  			// initial count
@@ -159,6 +174,7 @@
     CloseHandle(PLC_timer);
     WaitForSingleObject(PLC_thread, INFINITE);
     __cleanup();
+    DeleteCriticalSection(&Atomic64CS);
     CloseHandle(debug_wait_sem);
     CloseHandle(debug_sem);
     CloseHandle(python_wait_sem);
--- a/targets/Xenomai/plc_Xenomai_main.c	Mon Mar 04 12:22:49 2013 +0900
+++ b/targets/Xenomai/plc_Xenomai_main.c	Mon Mar 04 17:49:12 2013 +0900
@@ -1,5 +1,5 @@
 /**
- * Linux specific code
+ * Xenomai Linux specific code
  **/
 
 #include <stdio.h>
@@ -45,6 +45,10 @@
 {
     return __sync_val_compare_and_swap(atomicvar, compared, exchange);
 }
+long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange)
+{
+    return __sync_val_compare_and_swap(atomicvar, compared, exchange);
+}
 
 void PLC_GetTime(IEC_TIME *CURRENT_TIME)
 {
--- a/targets/plc_debug.c	Mon Mar 04 12:22:49 2013 +0900
+++ b/targets/plc_debug.c	Mon Mar 04 17:49:12 2013 +0900
@@ -184,6 +184,7 @@
 
 extern int TryEnterDebugSection(void);
 extern long AtomicCompareExchange(long*, long, long);
+extern long long AtomicCompareExchange64(long long* , long long , long long);
 extern void LeaveDebugSection(void);
 extern void ValidateRetainBuffer(void);
 extern void InValidateRetainBuffer(void);
@@ -376,7 +377,10 @@
             tail.msgidx = (old_cursor >> 32); 
             new_cursor = ((uint64_t)(tail.msgidx + 1)<<32) 
                          | (uint64_t)((buffpos + size + sizeof(mTail)) & LOG_BUFFER_MASK);
-        }while(!__sync_bool_compare_and_swap(&LogCursor[level],old_cursor,new_cursor));
+        }while(AtomicCompareExchange64(
+            (long long*)&LogCursor[level],
+            (long long)old_cursor,
+            (long long)new_cursor)!=old_cursor);
 
         copy_to_log(level, buffpos, buf, size);
         copy_to_log(level, (buffpos + size) & LOG_BUFFER_MASK, &tail, sizeof(mTail));