tests/python/c_code@c_ext/cfile.xml
changeset 3351 88dbdebd10fc
parent 3350 0d86fd9691ec
child 3352 3a138ccdfafa
equal deleted inserted replaced
3350:0d86fd9691ec 3351:88dbdebd10fc
     1 <?xml version='1.0' encoding='utf-8'?>
       
     2 <CFile xmlns:xhtml="http://www.w3.org/1999/xhtml">
       
     3   <includes>
       
     4     <xhtml:p><![CDATA[
       
     5 ]]></xhtml:p>
       
     6   </includes>
       
     7   <variables>
       
     8     <variable name="TestInput" type="SINT" initial="0"/>
       
     9     <variable name="TestOutput" type="SINT"/>
       
    10   </variables>
       
    11   <globals>
       
    12     <xhtml:p><![CDATA[
       
    13 volatile long Lock=0;
       
    14 volatile char PtoC=1,CtoP=2;
       
    15 
       
    16 extern long AtomicCompareExchange(long*,long, long);
       
    17 extern char *PLC_ID;
       
    18 
       
    19 int Simple_C_Call(int val){
       
    20   return val+1;
       
    21 }
       
    22 
       
    23 int Python_to_C_Call(char toC, char *fromC){
       
    24   /* Code called by python should never touch to 
       
    25      variables modified by PLC thread directly
       
    26 
       
    27      AtomicCompareExchange comes from 
       
    28      beremiz' runtime implementation */
       
    29 
       
    30   int res = 0;
       
    31   if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
       
    32     PtoC=toC;
       
    33     *fromC=CtoP;
       
    34     AtomicCompareExchange((long*)&Lock, 1, 0);
       
    35     res=1;
       
    36   }
       
    37   printf("C code called by Python: toC %d fromC %d\n",toC,*fromC);
       
    38   printf("PLC_ID id %s\n",PLC_ID);
       
    39   return res;
       
    40 }
       
    41 
       
    42 int PLC_C_Call(char fromPLC, char *toPLC){
       
    43   /* PLC also have to be realy carefull not to 
       
    44      conflict with asynchronous python access */
       
    45   if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
       
    46       CtoP = fromPLC;
       
    47       *toPLC = PtoC;
       
    48       AtomicCompareExchange((long*)&Lock, 1, 0);
       
    49       return 1;
       
    50   }
       
    51   return 0;
       
    52 }
       
    53 ]]></xhtml:p>
       
    54   </globals>
       
    55   <initFunction>
       
    56     <xhtml:p><![CDATA[
       
    57 ]]></xhtml:p>
       
    58   </initFunction>
       
    59   <cleanUpFunction>
       
    60     <xhtml:p><![CDATA[
       
    61 ]]></xhtml:p>
       
    62   </cleanUpFunction>
       
    63   <retrieveFunction>
       
    64     <xhtml:p><![CDATA[
       
    65 ]]></xhtml:p>
       
    66   </retrieveFunction>
       
    67   <publishFunction>
       
    68     <xhtml:p><![CDATA[
       
    69 if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
       
    70     TestInput = CtoP + PtoC + TestOutput;
       
    71     AtomicCompareExchange((long*)&Lock, 1, 0);
       
    72 }
       
    73 ]]></xhtml:p>
       
    74   </publishFunction>
       
    75 </CFile>