tests/python/c_code@c_ext/cfile.xml
branch1.1 Korean release
changeset 1280 72a826dfcfbb
parent 1125 1b1472e76f07
child 1401 611fded24ce4
equal deleted inserted replaced
977:c8e008b8cefe 1280:72a826dfcfbb
     1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
     1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
     2 <CFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="cext_xsd.xsd">
     2 <CFile>
     3   <includes>
     3   <includes>
     4 <![CDATA[#include "stdio.h"]]>
     4 <![CDATA[
       
     5 ]]>
     5   </includes>
     6   </includes>
     6   <variables>
     7   <variables>
     7     <variable name="TestInput" type="SINT" class="input"/>
     8     <variable name="TestInput" type="SINT" initial="0"/>
     8     <variable name="TestOutput" type="SINT" class="output"/>
     9     <variable name="TestOutput" type="SINT"/>
     9   </variables>
    10   </variables>
    10   <globals>
    11   <globals>
    11 <![CDATA[volatile long Lock=0;
    12 <![CDATA[
       
    13 volatile long Lock=0;
    12 volatile char PtoC=1,CtoP=2;
    14 volatile char PtoC=1,CtoP=2;
       
    15 
       
    16 extern long AtomicCompareExchange(long*,long, long);
    13 
    17 
    14 int Simple_C_Call(int val){
    18 int Simple_C_Call(int val){
    15   return val+1;
    19   return val+1;
    16 }
    20 }
    17 
    21 
    21 
    25 
    22      AtomicCompareExchange comes from 
    26      AtomicCompareExchange comes from 
    23      beremiz' runtime implementation */
    27      beremiz' runtime implementation */
    24 
    28 
    25   int res = 0;
    29   int res = 0;
    26   if(!AtomicCompareExchange(&Lock, 0, 1)){
    30   if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
    27     PtoC=toC;
    31     PtoC=toC;
    28     *fromC=CtoP;
    32     *fromC=CtoP;
    29     AtomicCompareExchange(&Lock, 1, 0);
    33     AtomicCompareExchange((long*)&Lock, 1, 0);
    30     res=1;
    34     res=1;
    31   }
    35   }
    32   printf("C code called by Python: toC %d fromC %d\n",toC,*fromC);
    36   printf("C code called by Python: toC %d fromC %d\n",toC,*fromC);
    33   return res;
    37   return res;
    34 }
    38 }
    35 
    39 
    36 int PLC_C_Call(char fromPLC, char *toPLC){
    40 int PLC_C_Call(char fromPLC, char *toPLC){
    37   /* PLC also have to be realy carefull not to 
    41   /* PLC also have to be realy carefull not to 
    38      conflict with asynchronous python access */
    42      conflict with asynchronous python access */
    39   int res;
    43   if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
    40   if(!AtomicCompareExchange(&Lock, 0, 1)){
       
    41       CtoP = fromPLC;
    44       CtoP = fromPLC;
    42       *toPLC = PtoC;
    45       *toPLC = PtoC;
    43       AtomicCompareExchange(&Lock, 1, 0);
    46       AtomicCompareExchange((long*)&Lock, 1, 0);
    44       return 1;
    47       return 1;
    45   }
    48   }
    46   return 0;
    49   return 0;
    47 }]]>
    50 }
       
    51 ]]>
    48   </globals>
    52   </globals>
    49   <initFunction>
    53   <initFunction>
    50 <![CDATA[]]>
    54 <![CDATA[
       
    55 ]]>
    51   </initFunction>
    56   </initFunction>
    52   <cleanUpFunction>
    57   <cleanUpFunction>
    53 <![CDATA[]]>
    58 <![CDATA[
       
    59 ]]>
    54   </cleanUpFunction>
    60   </cleanUpFunction>
    55   <retrieveFunction>
    61   <retrieveFunction>
    56 <![CDATA[]]>
    62 <![CDATA[
       
    63 ]]>
    57   </retrieveFunction>
    64   </retrieveFunction>
    58   <publishFunction>
    65   <publishFunction>
    59 <![CDATA[if(!AtomicCompareExchange(&Lock, 0, 1)){
    66 <![CDATA[
       
    67 if(!AtomicCompareExchange((long*)&Lock, 0, 1)){
    60     TestInput = CtoP + PtoC + TestOutput;
    68     TestInput = CtoP + PtoC + TestOutput;
    61     AtomicCompareExchange(&Lock, 1, 0);
    69     AtomicCompareExchange((long*)&Lock, 1, 0);
    62 }]]>
    70 }
       
    71 ]]>
    63   </publishFunction>
    72   </publishFunction>
    64 </CFile>
    73 </CFile>