Edouard@1401: <?xml version='1.0' encoding='utf-8'?>
Edouard@1401: <CFile xmlns:xhtml="http://www.w3.org/1999/xhtml">
Edouard@734:   <includes>
Edouard@1401:     <xhtml:p><![CDATA[
Edouard@1401: ]]></xhtml:p>
Edouard@734:   </includes>
Edouard@859:   <variables>
Laurent@1099:     <variable name="TestInput" type="SINT" initial="0"/>
Laurent@1099:     <variable name="TestOutput" type="SINT"/>
Edouard@859:   </variables>
Edouard@734:   <globals>
Edouard@1401:     <xhtml:p><![CDATA[
Laurent@1125: volatile long Lock=0;
Edouard@859: volatile char PtoC=1,CtoP=2;
Edouard@859: 
Laurent@1086: extern long AtomicCompareExchange(long*,long, long);
Edouard@1463: extern char *PLC_ID;
Laurent@1086: 
Edouard@859: int Simple_C_Call(int val){
Edouard@859:   return val+1;
Edouard@734: }
Edouard@859: 
Edouard@862: int Python_to_C_Call(char toC, char *fromC){
Edouard@859:   /* Code called by python should never touch to 
Edouard@859:      variables modified by PLC thread directly
Edouard@859: 
Edouard@859:      AtomicCompareExchange comes from 
Edouard@859:      beremiz' runtime implementation */
Edouard@859: 
Edouard@859:   int res = 0;
Laurent@1086:   if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
Edouard@859:     PtoC=toC;
Edouard@859:     *fromC=CtoP;
Laurent@1086:     AtomicCompareExchange((long*)&Lock, 1, 0);
Edouard@859:     res=1;
Edouard@859:   }
Edouard@859:   printf("C code called by Python: toC %d fromC %d\n",toC,*fromC);
Edouard@1463:   printf("PLC_ID id %s\n",PLC_ID);
Edouard@859:   return res;
Edouard@859: }
Edouard@859: 
Edouard@862: int PLC_C_Call(char fromPLC, char *toPLC){
Edouard@859:   /* PLC also have to be realy carefull not to 
Edouard@859:      conflict with asynchronous python access */
Laurent@1086:   if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
Edouard@859:       CtoP = fromPLC;
Edouard@859:       *toPLC = PtoC;
Laurent@1086:       AtomicCompareExchange((long*)&Lock, 1, 0);
Edouard@859:       return 1;
Edouard@859:   }
Edouard@859:   return 0;
Laurent@1125: }
Edouard@1401: ]]></xhtml:p>
Edouard@734:   </globals>
Edouard@734:   <initFunction>
Edouard@1401:     <xhtml:p><![CDATA[
Edouard@1401: ]]></xhtml:p>
Edouard@734:   </initFunction>
Edouard@734:   <cleanUpFunction>
Edouard@1401:     <xhtml:p><![CDATA[
Edouard@1401: ]]></xhtml:p>
Edouard@734:   </cleanUpFunction>
Edouard@734:   <retrieveFunction>
Edouard@1401:     <xhtml:p><![CDATA[
Edouard@1401: ]]></xhtml:p>
Edouard@734:   </retrieveFunction>
Edouard@734:   <publishFunction>
Edouard@1401:     <xhtml:p><![CDATA[
Laurent@1125: if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
Edouard@860:     TestInput = CtoP + PtoC + TestOutput;
Laurent@1086:     AtomicCompareExchange((long*)&Lock, 1, 0);
Laurent@1125: }
Edouard@1401: ]]></xhtml:p>
Edouard@734:   </publishFunction>
Laurent@1125: </CFile>