Merge branch 'charm' of charmgit:charm into charm
[charm.git] / examples / charm++ / threaded_ring / threaded_ring.cc
1 #include "threaded_ring.h"
2
3 Main::Main(CkArgMsg*)
4 {
5     nElems = 10;
6     CProxy_Ring A = CProxy_Ring::ckNew(nElems);
7     CkCallback* cb = new CkCallback(CkCallback::ckExit);
8     A.ckSetReductionClient(cb);
9     A.run();
10 }
11
12 void Ring::run()
13 {
14     CkPrintf("[%d]: nElems = %d\n", thisIndex, nElems);
15     nElems = 10;
16     for (int i=0; i<nElems; ++i) {
17         CkPrintf("[%d] iteration %d\n", thisIndex, i);
18         thisProxy((thisIndex+1) % nElems).getData();
19         waitFor();
20     }
21     CkPrintf("[%d] done\n", thisIndex);
22     contribute(0, 0, CkReduction::concat);
23 }
24
25 void Ring::getData()
26 {
27     CkPrintf("[%d]::getData()\n", thisIndex);
28     if (threadWaiting) {
29         CthAwaken(t);
30         threadWaiting = false;
31     } else {
32         dataHere++;
33     }
34 }
35
36 void Ring::waitFor()
37 {
38     CkPrintf("[%d]::waitFor()\n", thisIndex);
39     if (dataHere) {
40         dataHere--;
41         return;
42     }
43
44     t = CthSelf();
45     threadWaiting = true;
46     CthSuspend();
47 }
48
49 #include "threaded_ring.def.h"