XK-1A thread sync

Technical questions regarding the XTC tools and programming with XMOS.
cicga
Active Member
Posts: 51
Joined: Tue Oct 11, 2011 4:48 pm

XK-1A thread sync

Post by cicga »

Hi all,

Any idea how to implement process like:

main ( master?) thread preparing data ( other threads waiting for it.

when data is ready main thread create some ready signal(?)

based on it secondary (slave?)threads reading data and send it into ports.

cicga


MaxFlashrom
Experienced Member
Posts: 82
Joined: Fri Nov 05, 2010 2:59 pm

Post by MaxFlashrom »

Example edited to take account of Al's suggested corrections:

Do something like:

Code: Select all

// prototypes for forward reference
void masterFunk(chanend cout);
void slaveFunk(chanend cin);
out port myport = XS1_PORT_32A;

channel chan;

int main()
{
    par {
         masterFunk(chan);
         slaveFunk(chan);
    }
 
}

void masterFunk(chanend cout)
{
   int result;
   while(1) {
// do work
   cout <: result;
   }
}

void slaveFunk(chanend cin)
{
    int result;

    while(1) {
       cin :> result;
      myport <: result;
   }
}

Max
Last edited by MaxFlashrom on Mon Oct 17, 2011 8:20 pm, edited 1 time in total.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Be careful about using 'master' and 'slave' as these are keywords in XC for Transactions. see the Programming XC PDF - http://www.xmos.com/published/programmi ... os-devices section 3.4. For your example maybe renaming the functions slavefunk and masterfunk so as not to confuse ;-)

regards
Al
MaxFlashrom
Experienced Member
Posts: 82
Joined: Fri Nov 05, 2010 2:59 pm

Post by MaxFlashrom »

Folknology wrote:Be careful about using 'master' and 'slave' as these are keywords in XC for Transactions. see the Programming XC PDF - http://www.xmos.com/published/programmi ... os-devices section 3.4. For your example maybe renaming the functions slavefunk and masterfunk so as not to confuse ;-)

regards
Al
That'll teach me to just make stuff up without trying it in the XDE. The XDE syntax highlighter would have alerted me. I've not played with transactions much, so their reserved-word status didn't stick - either that or the mental FIFO just lost that detail after cramming in some new arcane fact. I did say "something like" the code above. One has allow some work for the reader although perhaps the resulting error would have been somewhat cryptic ;-)

Max.
Last edited by MaxFlashrom on Mon Oct 17, 2011 7:24 pm, edited 1 time in total.
cicga
Active Member
Posts: 51
Joined: Tue Oct 11, 2011 4:48 pm

Post by cicga »

MaxFlashrom,

this code is fine for single thread, but a problem is that I need several threads working from one "trigger" signal...

thanks
cicga
MaxFlashrom
Experienced Member
Posts: 82
Joined: Fri Nov 05, 2010 2:59 pm

Post by MaxFlashrom »

Are the slave threads to receive different data from the master, or all the same data?
You can just extend the idea.

Code: Select all

int main()
{
    par {
         mThread(chan1, chan2, chan3, chan4);
         sThread1(chan1);
         sThread2(chan2);
         slhread3(chan3);
         slhread4(chan4);
    }

}
One can use arrays of channels and a replicator syntax to make this more elegant, but you get the idea, I hope. There is no "trigger signal" required. XMOS works on events. The threads sleep automatically until they receive data.

Max.
cicga
Active Member
Posts: 51
Joined: Tue Oct 11, 2011 4:48 pm

Post by cicga »

MaxFlashrom,

ideally threads should work with different arrays generated by one master ]
something like this:

master{
a[x]=x;
b[x]=x+2;
}

svale1{
y = a[x];
bla,bla...
}

slave2{
y = b[x];
bla bla...
}

:shock:
cicga
Last edited by cicga on Mon Oct 17, 2011 7:53 pm, edited 1 time in total.
MaxFlashrom
Experienced Member
Posts: 82
Joined: Fri Nov 05, 2010 2:59 pm

Post by MaxFlashrom »

cicga wrote:MaxFlashrom,

threads should work with different arrays generated by one master

:shock:
cicga
Well, in your master thread send each chan a different piece of data, then

Code: Select all

for (n=0; n<10;  n++) {
  chan1 <: data[0][n];
  chan2 <: data[1][n];
  chan3 <: data[2][n];
  chan4 <: data[3][n];
}
Max
cicga
Active Member
Posts: 51
Joined: Tue Oct 11, 2011 4:48 pm

Post by cicga »

Max
thanks a lot - its working :D

cicga