#include "charm++.h"

extern "C" {
#include "simplemsg.h"
};

#include "testthreads.h"

void threadFunc(void *);

Testthreads::Testthreads(Msg *msg)
{
  CthThread thread = CthCreate((CthVoidFn)&(threadFunc),(void*)0,0);
  CthSetStrategyDefault(thread);
  CthAwaken(thread);
  thread = CthCreate((CthVoidFn)&(threadFunc),(void*)1,0);
  CthSetStrategyDefault(thread);
  CthAwaken(thread);
}

void threadFunc(void *arg)
{
  int i, rtag, stag;
  SMsg *smsg = new SMsg;
  if(arg==0) {
    stag = 1; rtag=0;
    for(i=0;i<NMSGS;i++) {
      CsmTVSend(0, 1, &stag, (void *)smsg, sizeof(SMsg));
      CsmTVRecv(1, &rtag, (void *)smsg, sizeof(SMsg), &rtag);
    }
    Msg *msg = new Msg;
    msg->data=3;
    CProxy_main ptm(mainhandle);
    ptm.testend(msg);
  } else {
    stag = 0; rtag=1;
    for(i=0;i<NMSGS;i++) {
      CsmTVRecv(1, &rtag, (void *)smsg, sizeof(SMsg), &rtag);
      CsmTVSend(0, 1, &stag, (void *)smsg, sizeof(SMsg));
    }
  }
}

#include "threads.def.h"

