tests/python/c_code@c_ext/cfile.xml
changeset 1086 b566ab5556ab
parent 953 1fb6cf5a4c4d
child 1099 d9c28879e55b
equal deleted inserted replaced
1085:906fe7f2c052 1086:b566ab5556ab
     9   </variables>
     9   </variables>
    10   <globals>
    10   <globals>
    11 <![CDATA[volatile long Lock=0;
    11 <![CDATA[volatile long Lock=0;
    12 volatile char PtoC=1,CtoP=2;
    12 volatile char PtoC=1,CtoP=2;
    13 
    13 
       
    14 extern long AtomicCompareExchange(long*,long, long);
       
    15 
    14 int Simple_C_Call(int val){
    16 int Simple_C_Call(int val){
    15   return val+1;
    17   return val+1;
    16 }
    18 }
    17 
    19 
    18 int Python_to_C_Call(char toC, char *fromC){
    20 int Python_to_C_Call(char toC, char *fromC){
    21 
    23 
    22      AtomicCompareExchange comes from 
    24      AtomicCompareExchange comes from 
    23      beremiz' runtime implementation */
    25      beremiz' runtime implementation */
    24 
    26 
    25   int res = 0;
    27   int res = 0;
    26   if(!AtomicCompareExchange(&Lock, 0, 1)){
    28   if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
    27     PtoC=toC;
    29     PtoC=toC;
    28     *fromC=CtoP;
    30     *fromC=CtoP;
    29     AtomicCompareExchange(&Lock, 1, 0);
    31     AtomicCompareExchange((long*)&Lock, 1, 0);
    30     res=1;
    32     res=1;
    31   }
    33   }
    32   printf("C code called by Python: toC %d fromC %d\n",toC,*fromC);
    34   printf("C code called by Python: toC %d fromC %d\n",toC,*fromC);
    33   return res;
    35   return res;
    34 }
    36 }
    35 
    37 
    36 int PLC_C_Call(char fromPLC, char *toPLC){
    38 int PLC_C_Call(char fromPLC, char *toPLC){
    37   /* PLC also have to be realy carefull not to 
    39   /* PLC also have to be realy carefull not to 
    38      conflict with asynchronous python access */
    40      conflict with asynchronous python access */
    39   int res;
    41   if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
    40   if(!AtomicCompareExchange(&Lock, 0, 1)){
       
    41       CtoP = fromPLC;
    42       CtoP = fromPLC;
    42       *toPLC = PtoC;
    43       *toPLC = PtoC;
    43       AtomicCompareExchange(&Lock, 1, 0);
    44       AtomicCompareExchange((long*)&Lock, 1, 0);
    44       return 1;
    45       return 1;
    45   }
    46   }
    46   return 0;
    47   return 0;
    47 }]]>
    48 }]]>
    48   </globals>
    49   </globals>
    54   </cleanUpFunction>
    55   </cleanUpFunction>
    55   <retrieveFunction>
    56   <retrieveFunction>
    56 <![CDATA[]]>
    57 <![CDATA[]]>
    57   </retrieveFunction>
    58   </retrieveFunction>
    58   <publishFunction>
    59   <publishFunction>
    59 <![CDATA[if(!AtomicCompareExchange(&Lock, 0, 1)){
    60 <![CDATA[if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
    60     TestInput = CtoP + PtoC + TestOutput;
    61     TestInput = CtoP + PtoC + TestOutput;
    61     AtomicCompareExchange(&Lock, 1, 0);
    62     AtomicCompareExchange((long*)&Lock, 1, 0);
    62 }]]>
    63 }]]>
    63   </publishFunction>
    64   </publishFunction>
    64 </CFile>
    65 </CFile>